/
sketch_190309a.pyde
143 lines (122 loc) · 4.72 KB
/
sketch_190309a.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
# Alexandre B A Villares - https://abav.lugaralgum.com/sketch-a-day
SKETCH_NAME = "sketch_190309a"
# An invisible graph is behind this rounded poly :)
add_library('GifAnimation')
add_library('peasycam')
from collections import deque
from gif_exporter import gif_export
from graphs import *
from inputs import Input
from arcs import poly_rounded2
history = deque(maxlen=40)
def setup():
global cam, input, GIF_EXPORT
size(600, 600, P3D)
frameRate(30)
GIF_EXPORT = False
# Ask user for Arduino port, uses slider if none is selected`
input = Input()
cam = PeasyCam(this, 660)
Ponto.SET = set()
NUM_PONTOS = int(input.analog(2) / 4)
for _ in range(NUM_PONTOS):
Ponto.SET.add(Ponto(width / 2, height / 2))
def draw():
background(200)
translate(-width/2, -height/2, 30*7)
# fill(200, 64)
# rect(0, 0, width, height)
TAM_ARESTA = input.analog(1) / 4
NUM_PONTOS = int(input.analog(2) / 4)
VEL_MAX = input.analog(3) / 128
CONNECT_RATE = 0.5 + input.analog(4) / 256 # % of connections
# para cada ponto
p_list, r_list = [], []
for ponto in Ponto.SET:
ponto.move(VEL_MAX) # atualiza posição
p_list.append(PVector(ponto.x, ponto.y))
r_list.append(ponto.r)
noFill()
#strokeWeight(3)
history.append((p_list, r_list))
for p_l, r_l in history:
poly_rounded2(p_l, r_l)
translate(0, 0, -10)
# checa arestas, se OK desenhar, se nãotem pontos removidos ou iguais
pontos_com_arestas = set() # para guardar pontos com aresta
for aresta in Aresta.ARESTAS:
if (aresta.p1 not in Ponto.SET) or (aresta.p2 not in Ponto.SET)\
or (aresta.p1 is aresta.p2): # arestas degeneradas
Aresta.ARESTAS.remove(aresta) # remove a aresta
else: # senão, tudo OK!
#aresta.desenha() # desenha a linha
aresta.puxa_empurra(TAM_ARESTA) # altera a velocidade dos pontos
# Adiciona ao conjunto de pontos com aresta
pontos_com_arestas.update([aresta.p1, aresta.p2])
pontos_sem_arestas = Ponto.SET - pontos_com_arestas
# print(len(Ponto.SET), len(pontos_sem_arestas), len(pontos_com_arestas))
# atualiza número de pontos
quantidade_atual_de_pontos = len(Ponto.SET)
if NUM_PONTOS > quantidade_atual_de_pontos:
Ponto.SET.add(Ponto(random(width), random(height)))
elif NUM_PONTOS < quantidade_atual_de_pontos - 2:
if pontos_sem_arestas:
# remove um ponto sem aresta
Ponto.SET.remove(pontos_sem_arestas.pop())
else:
Ponto.SET.pop() # remove um ponto qualquer
# outra maneira de eliminar pontos solitários é criando arestas
if pontos_sem_arestas:
for ponto in pontos_sem_arestas:
ponto.cria_arestas()
# atualiza número de arestas
if int((NUM_PONTOS) * CONNECT_RATE) > len(Aresta.ARESTAS) + 1:
if pontos_sem_arestas: # preferência por pontos solitários
choice(list(pontos_sem_arestas)).cria_arestas()
else:
choice(list(Ponto.SET)).cria_arestas()
elif int(NUM_PONTOS * CONNECT_RATE) < len(Aresta.ARESTAS) - 1:
Aresta.ARESTAS.remove(choice(Aresta.ARESTAS))
if input.digital(13):
# Ponto.reset(int(input.analog(2) / 4))
Ponto.SET = set()
for _ in range(NUM_PONTOS):
Ponto.SET.add(Ponto(width / 2, height / 2))
# uncomment next lines to export GIF
global GIF_EXPORT
if not frameCount % 5 and GIF_EXPORT:
GIF_EXPORT = gif_export(GifMaker,
frames=1000,
delay=300,
filename=SKETCH_NAME)
# read & draw sliders & checks mouse dragging / keystrokes
cam.beginHUD()
input.update()
cam.endHUD()
def mouseDragged(): # quando o mouse é arrastado
for ponto in Ponto.SET: # para cada Ponto checa distância do mouse
if dist(mouseX, mouseY, ponto.x, ponto.y) < TAM_PONTO / 2:
# move o Ponto para posição do mouse
ponto.x, ponto.y = mouseX, mouseY
ponto.vx = 0
ponto.vy = 0
def keyPressed():
global GIF_EXPORT
if key == 'p': # save PNG
saveFrame("####.png")
if key == 'g': # save GIF
GIF_EXPORT = True
if key == 'h':
input.help()
input.keyPressed()
def keyReleased():
input.keyReleased()
# print text to add to the project's README.md
def settings():
OUTPUT = ".gif"
println(
"""
![{0}](2019/{0}/{0}{1})
[{0}](https://github.com/villares/sketch-a-day/tree/master/2019/{0}) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]
""".format(SKETCH_NAME, OUTPUT)
)