-
Notifications
You must be signed in to change notification settings - Fork 1
/
__main__.py
150 lines (123 loc) · 4.29 KB
/
__main__.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
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
"""2024-09-28
Mondrian Redux 05
Releitura 2D de Piet Mondrian
png
Sketch,py5,CreativeCoding
"""
import numpy as np
import py5
from utils import helpers
sketch = helpers.info_for_sketch(__file__, __doc__)
PALETA = [
"#F7D744",
"#D0341E",
"#D0341E",
"#120D2D",
"#425AC6",
"#CAC9D1",
]
class Circle:
"""A little class representing an SVG circle."""
def __init__(self, cx, cy, r, color=None):
"""Initialize the circle with its centre, (cx,cy) and radius, r.
icolor is the index of the circle's color.
"""
self.cx, self.cy, self.r = cx, cy, r
self.color = color
def overlap_with(self, cx, cy, r):
"""Does the circle overlap with another of radius r at (cx, cy)?"""
d = np.hypot(cx - self.cx, cy - self.cy)
return d < r + self.r
class Circles:
"""A class for drawing circles-inside-a-circle."""
def __init__(
self,
width=800,
height=800,
R=400,
n=800,
rho_min=0.005,
rho_max=0.05,
colors=None,
):
"""Initialize the Circles object.
width, height are the SVG canvas dimensions
R is the radius of the large circle within which the small circles are
to fit.
n is the maximum number of circles to pack inside the large circle.
rho_min is rmin/R, giving the minimum packing circle radius.
rho_max is rmax/R, giving the maximum packing circle radius.
colors is a list of SVG fill color specifiers to be referenced by
the class identifiers c<i>. If None, a default palette is set.
"""
self.width, self.height = width, height
self.R, self.n = R, n
# The centre of the canvas
self.CX, self.CY = self.width // 2, self.height // 2
self.rmin, self.rmax = R * rho_min, R * rho_max
self.colors = colors or ["#993300", "#a5c916", "#00AA66", "#FF9900"]
def _place_circle(self, r):
# The guard number: if we don't place a circle within this number
# of trials, we give up.
guard = 500
while guard:
# Pick a random position, uniformly on the larger circle's interior
cr, cphi = (
self.R * np.sqrt(np.random.random()),
2 * np.pi * np.random.random(),
)
cx, cy = cr * np.cos(cphi), cr * np.sin(cphi)
if cr + r < self.R:
# The circle fits inside the larger circle.
if not any(
circle.overlap_with(self.CX + cx, self.CY + cy, r)
for circle in self.circles
):
# The circle doesn't overlap any other circle: place it.
circle = Circle(
cx + self.CX,
cy + self.CY,
r,
color=self.colors[np.random.randint(len(self.colors))],
)
self.circles.append(circle)
return
guard -= 1
def __call__(self) -> list[Circle]:
"""Place the little circles inside the big one."""
# First choose a set of n random radii and sort them. We use
# random.random() * random.random() to favour small circles.
self.circles = []
r = self.rmin + (self.rmax - self.rmin) * np.random.random(
self.n
) * np.random.random(self.n)
r[::-1].sort()
# Do our best to place the circles, larger ones first.
for i in range(self.n):
self._place_circle(r[i])
return self.circles
def setup():
py5.size(helpers.LARGURA, helpers.ALTURA, py5.P3D)
py5.background(0)
py5.color_mode(py5.HSB, 360, 100, 100)
circles = Circles(width=800, height=800, R=400, n=2000, colors=PALETA)
for circle in circles():
x = circle.cx
y = circle.cy
r = circle.r
color = circle.color
with py5.push_style():
py5.stroke(color)
py5.fill(color)
py5.circle(x, y, r)
helpers.write_legend(sketch=sketch)
def key_pressed():
key = py5.key
if key == " ":
save_and_close()
def save_and_close():
py5.no_loop()
helpers.save_sketch_image(sketch)
py5.exit_sketch()
if __name__ == "__main__":
py5.run_sketch()