added polyline instead of line

This commit is contained in:
Bachir Soussi Chiadmi
2017-03-15 19:43:13 +01:00
parent a468e9d346
commit 564e496f93
+76 -45
View File
@@ -1,8 +1,6 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# http://stackoverflow.com/questions/8997099/algorithm-to-generate-random-2d-polygon
import os, glob, errno import os, glob, errno
import math, random import math, random
from PIL import Image, ImageDraw from PIL import Image, ImageDraw
@@ -11,7 +9,7 @@ import datetime
from sympy.geometry import Circle, Point 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 ) : def generatePolygon( ctrX, ctrY, aveRadius, irregularity, spikeyness, numVerts ) :
'''Start with the centre of the polygon at ctrX, ctrY, '''Start with the centre of the polygon at ctrX, ctrY,
then creates the polygon by sampling points on a circle around the centre. 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] angle = angle + angleSteps[i]
for i in range(10): lines = fractalize(points)
points = fractalize(points)
return points return lines
def clip(x, min, max) : def clip(x, min, max) :
if( min > max ) : return x if( min > max ) : return x
@@ -69,26 +66,46 @@ def clip(x, min, max) :
else : return x else : return x
def fractalize(pts) : def fractalize(points) :
print("Fractalize") # print("Fractalize")
print(pts) # print(points)
points = [] lines = []
for v in range(0, len(pts)): for p in range(0, len(points)):
p1 = pts[v] p1 = points[p]
print(p1) # print(p1)
points.append(p1) # points.append(p1)
p2 = pts[0] if v >= len(pts)-1 else pts[v+1]; p2 = points[0] if p >= len(points)-1 else points[p+1];
d = distance(p1,p2) pts = [p1,p2]
# print(d)
r = d*0.52 # fractalize the line
ps1,ps2 = circle_intersection((p1[0],p1[1],r), (p2[0],p2[1],r)) if random.randint(1,10) > 3:
print(ps1) # number of fracatllization
# print(ps2) for i in range(10):
if random.randint(1,2) == 2: # loop throug points to divide then
points.append(ps2) fpts = []
else: for v in range(0, len(pts)-1):
points.append(ps1) fp1 = pts[v]
return points 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 # Distance function
def distance(p1,p2): def distance(p1,p2):
@@ -143,10 +160,11 @@ except OSError as exception:
raise raise
for i in range(0, 1): for i in range(0, 99):
print(i)
nv = random.randint(5,30) 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) 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")) svg = svgwrite.Drawing(filename = directory+"/map"+index+".svg",size = ("1000px", "1000px"))
# print(len(verts)) # polygone (white background)
for v in range(0, len(verts)): polygone = []
# print('---- '+str(v)+' ----') for l in range(0, len(lines)):
p1 = verts[v] for p in range(0, len(lines[l])):
p2 = verts[0] if v >= len(verts)-1 else verts[v+1]; polygone.append(lines[l][p])
# print(p1)
# print(p2)
# if random.randint(0,10) > 8: bgline = svg.polyline(polygone,
# sda = "4 4" stroke = "white",
# sdo = "5" stroke_width = "30",
# else: stroke_linejoin= "round",
sda = "0 0" stroke_linecap = "round",
sdo = "0" fill = "white")
svg.add(bgline)
line = svg.line(p1, p2, # strokes
stroke_width = "1", 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 = "black",
stroke_width = sw,
stroke_linejoin= "round",
stroke_linecap = "round", stroke_linecap = "round",
stroke_dasharray = sda, stroke_dasharray = sda,
stroke_dashoffset = sdo) stroke_dashoffset = sdo,
fill = "none")
svg.add(line) svg.add(line)
svg.save() svg.save()