diff --git a/mapgen.py b/mapgen.py index ae6166d..8c11cbd 100755 --- a/mapgen.py +++ b/mapgen.py @@ -1,8 +1,6 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -# http://stackoverflow.com/questions/8997099/algorithm-to-generate-random-2d-polygon - import os, glob, errno import math, random from PIL import Image, ImageDraw @@ -11,7 +9,7 @@ import datetime from sympy.geometry import Circle, Point - +# http://stackoverflow.com/questions/8997099/algorithm-to-generate-random-2d-polygon def generatePolygon( ctrX, ctrY, aveRadius, irregularity, spikeyness, numVerts ) : '''Start with the centre of the polygon at ctrX, ctrY, then creates the polygon by sampling points on a circle around the centre. @@ -57,10 +55,9 @@ def generatePolygon( ctrX, ctrY, aveRadius, irregularity, spikeyness, numVerts ) angle = angle + angleSteps[i] - for i in range(10): - points = fractalize(points) + lines = fractalize(points) - return points + return lines def clip(x, min, max) : if( min > max ) : return x @@ -69,26 +66,46 @@ 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 +def fractalize(points) : + # print("Fractalize") + # print(points) + lines = [] + for p in range(0, len(points)): + p1 = points[p] + # print(p1) + # points.append(p1) + p2 = points[0] if p >= len(points)-1 else points[p+1]; + pts = [p1,p2] + + # fractalize the line + if random.randint(1,10) > 3: + # number of fracatllization + for i in range(10): + # loop throug points to divide then + fpts = [] + for v in range(0, len(pts)-1): + fp1 = pts[v] + fpts.append(fp1) + fp2 = pts[v+1]; + d = distance(fp1,fp2) + # print(d) + r = d*0.52 + fps1,fps2 = circle_intersection((fp1[0],fp1[1],r), (fp2[0],fp2[1],r)) + # print(fps1) + # print(ps2) + if random.randint(1,2) == 2: + fpts.append(fps2) + else: + fpts.append(fps1) + + # add the last point + fpts.append(fp2) + pts = fpts + + # add the line, fractalized or not + lines.append(pts) + + return lines # Distance function def distance(p1,p2): @@ -143,10 +160,11 @@ except OSError as exception: raise -for i in range(0, 1): - +for i in range(0, 99): + print(i) nv = random.randint(5,30) - verts = generatePolygon( ctrX=500, ctrY=500, aveRadius=300, irregularity=1, spikeyness=0.4, numVerts=nv ) + lines = generatePolygon( ctrX=500, ctrY=500, aveRadius=300, irregularity=1, spikeyness=0.4, numVerts=nv ) + # print(lines) index = str(i) if i > 9 else '0'+str(i) @@ -158,27 +176,40 @@ for i in range(0, 1): svg = svgwrite.Drawing(filename = directory+"/map"+index+".svg",size = ("1000px", "1000px")) - # print(len(verts)) - for v in range(0, len(verts)): - # print('---- '+str(v)+' ----') - p1 = verts[v] - p2 = verts[0] if v >= len(verts)-1 else verts[v+1]; - # print(p1) - # print(p2) + # polygone (white background) + polygone = [] + for l in range(0, len(lines)): + for p in range(0, len(lines[l])): + polygone.append(lines[l][p]) - # if random.randint(0,10) > 8: - # sda = "4 4" - # sdo = "5" - # else: - sda = "0 0" - sdo = "0" + bgline = svg.polyline(polygone, + stroke = "white", + stroke_width = "30", + stroke_linejoin= "round", + stroke_linecap = "round", + fill = "white") + svg.add(bgline) - line = svg.line(p1, p2, - stroke_width = "1", + # strokes + for l in range(0, len(lines)): + # change randomly stroke attributes + if random.randint(0,10) > 8: # and len(lines[L]) < 3: + sw = "1" + sda = "4 4" + sdo = "5" + else: + sw = "1" + sda = "0 0" + sdo = "0" + + line = svg.polyline(lines[l], stroke = "black", + stroke_width = sw, + stroke_linejoin= "round", stroke_linecap = "round", stroke_dasharray = sda, - stroke_dashoffset = sdo) + stroke_dashoffset = sdo, + fill = "none") svg.add(line) svg.save()