Package MeatEngine :: Package Math :: Package Voronoi :: Module test
[hide private]
[frames] | no frames]

Source Code for Module MeatEngine.Math.Voronoi.test

  1  # 
  2  # mapmaker.py 
  3  # 
  4  # What if we made a Poisson Disc Distribution of positions on the 
  5  # board, and then proceeded to triangulate those positions (somehow) 
  6  # and then drew the dual graph of the resultant network? Would that be 
  7  # an interesting gameboard? 
  8   
  9  import Image, ImageDraw 
 10  import random 
 11  import math 
 12   
 13  import subdivision 
 14  import sys 
 15  sys.path.append(r"e:\dev") 
 16  from MeatEngine.Math.vector import Vec2f 
 17   
 18  EPSILON=0.001 
 19  imX=2400 
 20  imY=3000 
 21   
 22  greenGrass=(128,255,128) 
 23  greyGreen=(100,240,100) 
 24   
 25  backgroundColor=(255,255,255) 
 26  adjacencyColor=(180,240,240) 
 27   
 28   
29 -def findCircumCenter(p1, p2, p3):
30 """ 31 From a post by Dave Watson: 32 33 This approach uses Cramer's Rule to find the intersection of two 34 perpendicular bisectors of triangle edges 35 36 37 p_0 = (((a_0 - c_0) * (a_0 + c_0) + (a_1 - c_1) * (a_1 + c_1)) / 2 * (b_1 - c_1) 38 - ((b_0 - c_0) * (b_0 + c_0) + (b_1 - c_1) * (b_1 + c_1)) / 2 * (a_1 - c_1)) 39 / D 40 41 p_1 = (((b_0 - c_0) * (b_0 + c_0) + (b_1 - c_1) * (b_1 + c_1)) / 2 * (a_0 - c_0) 42 - ((a_0 - c_0) * (a_0 + c_0) + (a_1 - c_1) * (a_1 + c_1)) / 2 * (b_0 - c_0)) 43 / D 44 45 where D = (a_0 - c_0) * (b_1 - c_1) - (b_0 - c_0) * (a_1 - c_1) 46 47 The _squared_ circumradius is then: 48 49 r^2 = (c_0 - p_0)^2 + (c_1 - p_1)^2 50 """ 51 52 d = (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y) 53 54 if abs(d) < EPSILON: 55 return ((p1.x+p2.x+p3.x)/3, 56 (p1.y+p2.y+p3.y)/3) 57 58 x = (((p1.x - p3.x) * (p1.x + p3.x) + (p1.y - p3.y) * (p1.y + p3.y)) / 2 * 59 (p2.y - p3.y) - 60 ((p2.x - p3.x) * (p2.x + p3.x) + (p2.y - p3.y) * (p2.y + p3.y)) / 2 * 61 (p1.y - p3.y)) / d 62 63 y = (((p2.x - p3.x) * (p2.x + p3.x) + (p2.y - p3.y) * (p2.y + p3.y)) / 2 * (p1.x - p3.x) 64 - ((p1.x - p3.x) * (p1.x + p3.x) + (p1.y - p3.y) * (p1.y + p3.y)) / 2 * (p2.x - p3.x))/ d 65 66 return x,y
67 68 69 70
71 -def pointIsValid(p, rSqr):
72 for p1 in points: 73 dx=p1[0]-p[0] 74 dy=p1[1]-p[1] 75 76 distSqr=dx*dx+dy*dy 77 if distSqr<rSqr: 78 return False 79 return True
80 81 82 if __name__=="__main__": 83 im=Image.new("RGB",(imX,imY),backgroundColor) 84 draw=ImageDraw.ImageDraw(im) 85 86 points=[] 87 88 radius=60 89 numPts=15000 90 91 92 p1=Vec2f(-100.0,-100.0) 93 p2=Vec2f(3.0*imX,0.0) 94 p3=Vec2f(0.0,3.0*imY) 95 96 s=subdivision.Subdivision(p1,p2,p3) 97 98 for i in range(numPts): 99 print i 100 x=float(random.randrange(imX)) 101 y=float(random.randrange(imY)) 102 103 rSqr=radius*radius 104 105 p=(x,y) 106 if pointIsValid(p,rSqr): 107 points.append(p) 108 s.insertSite(Vec2f(x,y)) 109 im.putpixel(p,(0,0,0)) 110 111 112 e=s.dumpEdges() 113 for edge in e: 114 o=edge.org() 115 d=edge.dest() 116 q1=edge.lNext().dest() 117 q2=edge.sym.lNext().dest() 118 119 x1,y1=findCircumCenter(o,d,q1) 120 x2,y2=findCircumCenter(o,d,q2) 121 122 draw.line((o.x, o.y, d.x, d.y), adjacencyColor) 123 draw.line((x1, y1, x2, y2), (0,0,0)) 124 125 del draw 126 127 im.save("map.jpg") 128