-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflower_packing.pyde
179 lines (149 loc) · 4.69 KB
/
flower_packing.pyde
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from export import mousePressed
# parameters
accent = ["ffe045","dd2a3b","7621d9","2D27E3","0de0a8"]
colors = [accent[int(random(len(accent)))]] + ["ffffff","222222","AAAAAA"]
w = 1080
h = 1080
margin_h = 270
margin_v = 170
max_instances = 2000
min_radius = 5
max_radius = 50
precision = 10 # the smaller the more filled (also takes more time)
# outer frame
framing = True
frame_size = 35
##
b = int(random(len(colors)))
bg = "#" + colors.pop(b)
col = "#" + colors[int(random(len(colors)))]
flowers = []
def setup():
size(w,h)
background(bg)
noLoop()
noFill()
flowers_packing(margin_h, margin_v, w-margin_h, h-margin_v)
def draw():
# main drawing with random choices of shapes
d = random(1)
# 1/4 chance of drawing flowers
if d < 0.25 :
for f in flowers:
f.flower()
# 1/2 chance of adding stars
if random(1) < 0.5 :
for f in flowers:
f.star()
# 1/2 chance of adding polygons
if random(1) < 0.5 :
for f in flowers:
f.polygon()
# 1/4 chance of drawing polygons
elif d < 0.5 :
for f in flowers:
f.polygon()
# 1/2 chance of adding stars
if random(1) < 0.5 :
for f in flowers:
f.star()
# 1/4 chance of drawing stars
elif d < 0.75 :
for f in flowers:
f.star()
# 1/4 of each instance having its own shape
else :
for f in flowers:
f.random_shape()
# drawing the outer frame
frame(frame_size, 255)
def flowers_packing(x,y,w,h):
# safety variables
max_iterations = (w*h)//precision
stop = 0
# create Flower list with collision check
while len(flowers) < max_instances:
f = Flower(x,y,w,h,col)
flowers.append(f)
if len(flowers) > 0:
for i in range(len(flowers[:-1])):
# if last created collide with existing, pop and repeat
if flowers[i].intersects(flowers[-1]):
flowers.pop()
break
stop += 1
# safety break if too much iterations and list not full
if stop > max_iterations:
break
class Circle(object):
def __init__(self,x,y,w,h):
self.radius = random(min_radius, max_radius)
self.x = random(self.radius+x, w-self.radius)
self.y = random(self.radius+y, h-self.radius)
def intersects(self, c):
''' checks if this Circle intersects with a given circle
c : Circle instance '''
return dist(self.x, self.y, c.x, c.y) < self.radius + c.radius
def show(self):
circle(self.x, self.y, self.radius*2)
class Flower(Circle):
def __init__(self,x,y,w,h,col):
Circle.__init__(self,x,y,w,h)
self.petals = int(random(5+self.radius/10, self.radius/5))
self.angle = TAU/self.petals
self.col = col
stroke(self.col)
self.rotation = random(PI)
def flower(self):
r = random(self.radius/3,self.radius/1.5)
i = 0
pushMatrix()
translate(self.x, self.y)
rotate(self.rotation)
while i < TAU:
circle(cos(i)*r,sin(i)*r,self.radius*2-r*2-2)
i += self.angle
popMatrix()
def polygon(self):
r = self.radius
i = 0
pushMatrix()
translate(self.x, self.y)
rotate(self.rotation)
beginShape()
while i < TAU:
vertex(cos(i)*r,sin(i)*r)
i += self.angle
vertex(cos(0)*r,sin(0)*r)
endShape()
popMatrix()
def star(self):
r = self.radius-1
i = 0
pushMatrix()
translate(self.x, self.y)
rotate(self.rotation)
while i < TAU:
line(0,0,cos(i)*r,sin(i)*r)
i += self.angle
popMatrix()
def random_shape(self):
''' Returns a random shape method from Flower class '''
d = random(3)
if d < 1:
return self.flower()
elif d < 2:
return self.polygon()
else:
return self.star()
def frame(frame_size, col):
''' Draws 4 rectangles to create an outer frame on the canvas.
frame_size : int thickness of canvas in pixels
col : color of the frame '''
if framing:
fill(col)
noStroke()
rect(0,0,width,frame_size)
rect(0,height-frame_size,width,frame_size)
rect(0,0,frame_size,height)
rect(width-frame_size,0,frame_size,height)