-
Notifications
You must be signed in to change notification settings - Fork 0
/
animate.py
78 lines (64 loc) · 2.98 KB
/
animate.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
import tkinter as tk
import numpy as np
class MovementAnimation:
"""
This class represents on a canvas the movement of particles according to different models.
:param cl: model that is represented in the canvas
:type cl: class
:param side: length of the square canvas
:type side: float
"""
def __init__(self, cl, side=1000):
self.cl = cl
self.side, real_side = side, self.cl.get_side()
self.ratio = self.side / real_side
self.radius = self.cl.get_radius() * self.ratio
self.janus = self.cl.get_janus()
self.step = 0
self.window = tk.Tk()
button = tk.Button(self.window, text="X", command=self.window.destroy)
button.pack()
self.canvas = tk.Canvas(self.window, width=self.side, height=self.side, bg='white')
self.canvas.pack()
self.animation_movement()
self.window.mainloop()
def animation_movement(self):
"""
This function is the one that draws at each iteration the new position of the particles.
"""
position_array = self.cl.get_position()
#velocities_array = self.cl.get_velocities()
if self.janus:
velocities_array = self.cl.get_velocities()
#a=1
else:
self.canvas.delete("all")
for i, elt in enumerate(position_array):
x, y = elt[0] * self.ratio, elt[1] * self.ratio
#v = velocities_array[i]
if self.janus:
v = velocities_array[i]
if np.any(v):
self.canvas.delete(str(i)+'a')
v_per = np.array([-v[1], v[0]])
angle = 360 - np.angle(np.complex(v_per[0], v_per[1]), deg=True)
if angle < 0:
angle += 360
self.canvas.create_arc(x - self.radius, y + self.radius, x + self.radius, y - self.radius,
start=angle, extent=180, fill='blue', tag=str(i)+'a')
if angle < 180:
new_angle = 180 + angle
else:
new_angle = angle - 180
self.canvas.create_arc(x - self.radius, y + self.radius, x + self.radius, y - self.radius,
start=new_angle, extent=180, fill='red', tag=str(i)+'a')
else:
self.canvas.create_oval(x - self.radius, y + self.radius, x + self.radius, y - self.radius, fill='blue')
#self.canvas.create_text(x-self.radius/2, y, text=str(i), fill='red')
#target = self.cl.get_target()
#self.canvas.create_text(x+self.radius/2, y, text=str(target[i]), fill='green')
#self.canvas.create_text(x + 3 * self.radius, y, text=str(np.linalg.norm(v))[:6], fill='green')
self.step += 1
self.window.update()
self.cl.iter_movement(self.step, animation=True)
self.window.after(10, self.animation_movement)