-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualiser.py
More file actions
59 lines (47 loc) · 1.99 KB
/
Visualiser.py
File metadata and controls
59 lines (47 loc) · 1.99 KB
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
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.patches import Circle
class Visualiser(object):
def __init__(self, physics, planets):
self.planets = planets
self.physics = physics
def run(self):
# Initialize figure
figure = plt.figure(figsize=(7, 7))
axes = figure.add_subplot(111, aspect='equal')
axes.set_xlim(-2, 2)
axes.set_ylim(-2, 2)
# Initialize graphics objects
self.circles = []
for planet in self.planets:
p = Circle((planet.position[0], planet.position[1]),
planet.dimension, color=planet.color)
p.set_visible(False)
axes.add_patch(p)
self.circles.append(p)
def animate(frameNumber):
# Without this, a copy of the figure will always be shown at its
# original position.
# Probably an easier way to do this, but the workaround is fine
if frameNumber == 1:
for c in self.circles:
c.set_visible(True)
# Call the user updateFunc
self.physics.timestep(self.planets)
for c, p in zip(self.circles, self.planets):
if p.name == "Moon":
for p2 in self.planets:
if p2.name == "Earth":
earth = p2
break
c.center = (earth.position[0] + (p.position[0] - earth.position[0])*50, earth.position[1] + (p.position[1] - earth.position[1])*50)
else:
c.center = (p.position[0], p.position[1])
return self.circles
ani = animation.FuncAnimation(figure,
animate,
frames=1000,
interval=25,
repeat=False,
blit=True)
plt.show()