Browse Source

added fractals

Bachir Soussi Chiadmi 7 years ago
parent
commit
a468e9d346
1 changed files with 74 additions and 13 deletions
  1. 74 13
      mapgen.py

+ 74 - 13
mapgen.py

@@ -8,6 +8,7 @@ import math, random
 from PIL import Image, ImageDraw
 import svgwrite
 import datetime
+from sympy.geometry import Circle, Point
 
 
 
@@ -56,6 +57,9 @@ def generatePolygon( ctrX, ctrY, aveRadius, irregularity, spikeyness, numVerts )
 
      angle = angle + angleSteps[i]
 
+   for i in range(10):
+      points = fractalize(points)
+
    return points
 
 def clip(x, min, max) :
@@ -65,6 +69,66 @@ def clip(x, min, max) :
    else :             return x
 
 
+def fractalize(pts) :
+   print("Fractalize")
+   print(pts)
+   points = []
+   for v in range(0, len(pts)):
+      p1 = pts[v]
+      print(p1)
+      points.append(p1)
+      p2 = pts[0] if v >= len(pts)-1 else pts[v+1];
+      d = distance(p1,p2)
+      # print(d)
+      r = d*0.52
+      ps1,ps2 = circle_intersection((p1[0],p1[1],r), (p2[0],p2[1],r))
+      print(ps1)
+      # print(ps2)
+      if random.randint(1,2) == 2:
+         points.append(ps2)
+      else:
+         points.append(ps1)
+   return points
+
+# Distance function
+def distance(p1,p2):
+   sq1 = (p1[0]-p2[0])*(p1[0]-p2[0])
+   sq2 = (p1[1]-p2[1])*(p1[1]-p2[1])
+   return math.sqrt(sq1 + sq2)
+
+def circle_intersection(circle1, circle2):
+   '''
+   @summary: calculates intersection points of two circles
+   @param circle1: tuple(x,y,radius)
+   @param circle2: tuple(x,y,radius)
+   @result: tuple of intersection points (which are (x,y) tuple)
+   '''
+   # return self.circle_intersection_sympy(circle1,circle2)
+   x1,y1,r1 = circle1
+   x2,y2,r2 = circle2
+   # http://stackoverflow.com/a/3349134/798588
+   dx,dy = x2-x1,y2-y1
+   d = math.sqrt(dx*dx+dy*dy)
+   if d > r1+r2:
+      print("#1")
+      return None # no solutions, the circles are separate
+   if d < abs(r1-r2):
+      print("#2")
+      return None # no solutions because one circle is contained within the other
+   if d == 0 and r1 == r2:
+      print("#3")
+      return None # circles are coincident and there are an infinite number of solutions
+
+   a = (r1*r1-r2*r2+d*d)/(2*d)
+   h = math.sqrt(r1*r1-a*a)
+   xm = x1 + a*dx/d
+   ym = y1 + a*dy/d
+   xs1 = xm + h*dy/d
+   xs2 = xm - h*dy/d
+   ys1 = ym - h*dx/d
+   ys2 = ym + h*dx/d
+
+   return (xs1,ys1),(xs2,ys2)
 
 # create maps export folder
 base = "maps"
@@ -79,13 +143,10 @@ except OSError as exception:
       raise
 
 
+for i in range(0, 1):
 
-
-
-for i in range(0, 99):
-
-   nv = random.randint(10,20)
-   verts = generatePolygon( ctrX=500, ctrY=500, aveRadius=300, irregularity=1, spikeyness=0.15, numVerts=nv )
+   nv = random.randint(5,30)
+   verts = generatePolygon( ctrX=500, ctrY=500, aveRadius=300, irregularity=1, spikeyness=0.4, numVerts=nv )
 
    index = str(i) if i > 9 else '0'+str(i)
 
@@ -105,15 +166,15 @@ for i in range(0, 99):
       # print(p1)
       # print(p2)
 
-      if random.randint(0,10) > 8:
-         sda = "4 4"
-         sdo = "5"
-      else:
-         sda = "0 0"
-         sdo = "0"
+      # if random.randint(0,10) > 8:
+      #    sda = "4 4"
+      #    sdo = "5"
+      # else:
+      sda = "0 0"
+      sdo = "0"
 
       line = svg.line(p1, p2,
-         stroke_width = "2",
+         stroke_width = "1",
          stroke = "black",
          stroke_linecap = "round",
          stroke_dasharray = sda,