-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimation.py
219 lines (160 loc) · 4.67 KB
/
Animation.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"""
Para hacer:
1) Leer archivo txt
2) Generamos particulas
3) Dibujamos particulas
"""
from p5 import *
from asyncore import read
import math
def readInfo():
with open('./output/particles.txt') as info:
for line in info:
yield line
def readNeighbours():
with open('./output/neighbours.txt') as info:
for line in info:
yield line
RESIZE_FACTOR = 30
neighbours = readNeighbours()
# Salto 1 lineas vacias
neighbours.__next__()
#
info = readInfo()
cantParticles = int(info.__next__())
canvasSize = float(info.__next__()) * RESIZE_FACTOR
criticalRadio = float(info.__next__()) * RESIZE_FACTOR
blancLine = info.__next__()
radios = []
selectedColor = "#DA2929"
noSelectedColor = "#3E3B3B"
backgroundColor = "#212121"
neighbourColor = "#29ACDA"
L = canvasSize
M = 10 * RESIZE_FACTOR
RC = criticalRadio
# PROVISIONAL
# for radio in range(cantParticles):
# radios.append(10)
#
# print(radios)
def getParticlesStaticInfo():
index = 0
radiosLine = next(info).split(' ')
for index in range(cantParticles):
radios.append(float(radiosLine[index]))
# getParticlesStaticInfo() POR AHORA NO DEVOLVEMOS RADIOS
def stringCompatible(str):
if (len(str) > 0 and str != '\n'):
return int(str)
return -1
def initializeParticle():
for index in range(cantParticles):
infoLine = info.__next__().split(' ')
neighLine = neighbours.__next__().split(' ')
numInfoLine = list(map(lambda str: float(str), infoLine))
numNeighLine = list(map(lambda str: stringCompatible(str), neighLine))
# Aca tambien estaria la velocidad y eso cuando haga falta
position = [numInfoLine[1], numInfoLine[2]]
radio = numInfoLine[0]
velocity = [0, 0]
yield Particle(index, position[0], position[1], velocity[0],
velocity[1], 2 * radio, noSelectedColor, False,
numNeighLine, False)
class Particle:
def __init__(self, id, x, y, vx, vy, d, color, active, neighbours,
isNeighbour):
self.id = id
self.x = x * RESIZE_FACTOR
self.y = y * RESIZE_FACTOR
self.vx = vx
self.vy = vy
self.d = d * RESIZE_FACTOR
self.color = color
self.active = active
self.neighbours = neighbours
self.isNeighbour = isNeighbour
def drawWithColor(self, color):
no_stroke()
fill(color)
circle(self.x, self.y, self.d)
fill('#ffffff')
text(
str(self.id),
self.x,
self.y,
)
def draw(self):
if (self.active):
self.color = selectedColor
drawCriticalRadio(self.x, self.y, self.d / 2)
else:
if (self.isNeighbour):
self.color = neighbourColor
else:
self.color = noSelectedColor
self.drawWithColor(self.color)
def activate(self):
self.active = True
def deactivate(self):
self.active = False
def activateNeighbour(self):
self.isNeighbour = True
def deActivateNeighbour(self):
self.isNeighbour = False
particles = []
for particle in initializeParticle():
particles.append(particle)
def drawParticles():
for particle in particles:
particle.draw()
def drawNeighbours(particleNeighbours):
print(particleNeighbours)
for particle in particles:
if particle.id in particleNeighbours:
# particle.drawWithColor(neighbourColor)
particle.activateNeighbour()
else:
particle.deActivateNeighbour()
def deactivateAll():
for particle in particles:
particle.deActivateNeighbour()
def setup():
size(canvasSize, canvasSize)
no_stroke()
no_loop()
def draw():
background(backgroundColor)
drawParticles()
drawGrid()
def mouse_pressed(event):
deactivateAll()
for particle in particles:
if (math.sqrt((event.x - particle.x)**2 +
(event.y - particle.y)**2) < particle.d / 2):
particle.activate()
drawNeighbours(particle.neighbours)
else:
particle.deactivate()
redraw()
def drawCriticalRadio(x, y, particle_radius):
no_fill()
stroke(selectedColor)
stroke_weight(1)
circle(x, y, 2 * (RC + particle_radius))
def drawGrid():
stroke(90, 90, 90, 63)
stroke_weight(0.1)
begin_shape()
width = int(L / M)
height = int(L / M)
for i in range(height):
vertex(0, i * M)
vertex(L, i * M)
vertex(0, i * M)
for j in range(width):
vertex(j * M, 0)
vertex(j * M, L)
vertex(j * M, 0)
end_shape()
run()