-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneratePolys.py
executable file
·74 lines (59 loc) · 2.01 KB
/
generatePolys.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import math, random, sys
from PIL import Image, ImageDraw
first_arg = sys.argv[1]
second_arg = sys.argv[2]
third_arg = sys.argv[3]
fourth_arg = sys.argv[4]
fifth_arg = sys.argv[5]
sixth_arg = sys.argv[6]
def generatePolygon(ctrX=first_arg, ctrY=second_arg, aveRadius=third_arg, irregularity=fourth_arg, spikeyness=fifth_arg, numVerts=sixth_arg) :
irregularity = int(irregularity)
ctrX = int(ctrX)
ctrY = int(ctrY)
aveRadius = int(aveRadius)
spikeyness = float(spikeyness)
numVerts = int(numVerts)
irregularity = clip( irregularity, 0,1 ) * 2*math.pi / numVerts
spikeyness = clip( spikeyness, 0,1 ) * aveRadius
# generate n angle steps
angleSteps = []
lower = (2*math.pi / numVerts) - irregularity
upper = (2*math.pi / numVerts) + irregularity
sum = 0
for i in range(numVerts) :
tmp = random.uniform(lower, upper)
angleSteps.append( tmp )
sum = sum + tmp
# normalize the steps so that point 0 and point n+1 are the same
k = sum / (2*math.pi)
for i in range(numVerts) :
angleSteps[i] = angleSteps[i] / k
# now generate the points
points = []
angle = random.uniform(0, 2*math.pi)
for i in range(numVerts) :
r_i = clip( random.gauss(aveRadius, spikeyness), 0, 2*aveRadius )
x = ctrX + r_i*math.cos(angle)
y = ctrY + r_i*math.sin(angle)
points.append( (int(x),int(y)) )
angle = angle + angleSteps[i]
print("Points: {}" .format(points))
return points
def draw(points):
black = (0,0,0)
white=(255,255,255)
im = Image.new('RGB', (500, 500), white)
imPxAccess = im.load()
draw = ImageDraw.Draw(im)
tupVerts = list(map(tuple,points))
draw.line( tupVerts+[tupVerts[0]], width=1, fill=black )
im.show()
def clip(x, min, max) :
if( min > max ) : return x
elif( x < min ) : return min
elif( x > max ) : return max
else : return x
if __name__ == "__main__":
generatePolygon()
#poly = generatePolygon(600, 600, 400, 4, 0.1, 30)
#print(poly)