-
Notifications
You must be signed in to change notification settings - Fork 1
/
__main__.py
143 lines (121 loc) · 3.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
"""2024-10-30
Choque de realidade 03
A partir de uma grade, colocamos quadrados com pequenas imperfeições
png
Sketch,py5,CreativeCoding
"""
from pathlib import Path
from random import choice, shuffle
import numpy as np
import py5
from PIL import Image
from utils import helpers
sketch = helpers.info_for_sketch(__file__, __doc__)
PASTA = Path(__file__).parent
MASCARA = PASTA / "sampa.png"
PALETA_SP = [
"#CCC",
"#BBB",
"#AAA",
"#999",
]
PALETA = [
"#E2001A",
"#19171B",
"#009036",
]
def carregar_mascara(imagem: Path, escala: float = 1):
"""Carrega imagem e retorna a máscara."""
img = Image.open(imagem)
img_ = np.array(img.getdata()).reshape(img.height, img.width, 4)
mask = []
for y in img_:
for x in y:
mask.append(x[3])
img_ = np.array(mask).reshape(img.height, img.width).transpose()
return img_
def dentro_mascara(x: float, y: float, mascara: np.array, centrado: bool = True):
if centrado:
x += py5.width // 2
y += py5.height // 2
x = int(x)
y = int(y)
try:
dentro = bool(mascara[x][y])
except IndexError:
dentro = False
return dentro
def cria_grade(
xi: int,
xf: int,
yi: int,
yf: int,
celula_x: int,
celula_y: int,
alternada: bool = True,
):
"""Cria uma grade."""
pontos = []
celula_x = int(celula_x)
celula_y = int(celula_y)
for idy, y in enumerate(range(yi, yf + 1, celula_y)):
if (y + celula_y) > yf:
break
buffer = int(celula_x / 2) if (alternada and idy % 2) else 0
for x in range(xi - buffer, xf + 1, celula_x):
pontos.append((x, y))
return pontos
def setup():
py5.size(helpers.LARGURA, helpers.ALTURA, py5.P3D)
py5.background(248, 241, 219)
py5.rect_mode(py5.CORNER)
mascara = carregar_mascara(MASCARA)
shuffle(PALETA)
margem_x = -200
margem_y = -200
largura_base = 5
altura_base = 5
grade = cria_grade(
margem_x,
py5.width - margem_x,
margem_y,
py5.height - margem_y,
largura_base,
altura_base,
True,
)
# shuffle(grade)
for x, y in grade:
py5.no_stroke()
mult_l = py5.random(1.2, 1.7)
mult_a = py5.random(1.2, 1.7)
largura = largura_base * mult_l
altura = altura_base * mult_a
forma = py5.create_shape(py5.RECT, 0, 0, largura, altura)
rotacao_max = abs(
py5.remap(y, -200, 1000, 0, 9) * py5.remap(x, -200, 1000, 0, 5)
)
if dentro_mascara(x, y, mascara, False):
cor_interna = choice(PALETA_SP)
else:
cor_interna = choice(PALETA)
py5.fill(cor_interna)
forma.rotate(py5.radians(py5.random(-rotacao_max, rotacao_max)))
py5.shape(forma, x, y)
with py5.push_style():
py5.stroke_weight(1)
py5.stroke("#000")
py5.rect(x, y, largura_base, altura_base)
with py5.push_matrix():
py5.translate(0, 0, 10)
helpers.write_legend(sketch=sketch, frame="#000", cor="#FFF")
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()