-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlanetAnimator.py
128 lines (106 loc) · 3.8 KB
/
PlanetAnimator.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
"""
File: animate_exact.py
Author: Halvard Sutterud
Email: [email protected]
Github: https://github.com/halvarsu
Description: Arguments should be indexes of wanted planets
"""
# coding: utf-8
#import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
from MySolarSystem import MySolarSystem
from matplotlib.animation import FuncAnimation
import sys
from time import sleep
try:
if len(sys.argv) ==1:
raise IndexError
elif len(sys.argv) > 8:
raise ValueError
planets = []
for p in sys.argv[1:]:
planets.append(int(p))
except IndexError:
planets = range(7)
except ValueError:
print ("bad usage, expects int arguments (max 7)(planet indexes)")
sys.exit()
class PlanetAnimator(MySolarSystem):
"""Docstring for PlanetAnimator. """
def __init__(self, seed, years=0):
MySolarSystem.__init__(self, seed)
"""TODO: to be defined1."""
self.years = years
self.sleep_time = 0
self.forwards = True
self.j = 0
self.res = 200
self.direction= "Forwards"
self.paused = False
def load_files(self, times = 'times.npy', sat=False):
pos = np.load('positionsHomePlanet.npy')
self.times = np.load('times.npy')
self.positions = pos[:,planets].swapaxes(0,1).swapaxes(1,2)
self.plot_pos = np.load('analyticalPositions.npy')
self.length = pos.shape[-1]
def animate(self, save = False):
self.fig = plt.figure(figsize = (7,7))
self.ax = self.fig.gca()
self.ax.plot(self.plot_pos[0].T, self.plot_pos[1].T, linewidth = 0.2)
positions = self.positions
color = np.random.random((3))
self.scat = plt.scatter(positions[:,0,0],positions[:,0,1], s = 50,c=color)
self.fig.suptitle("Sleep time: %f"%self.sleep_time)
self.max_dist = np.max(np.abs(positions)) + 1
self.ax.axis('equal')
self.ax.set_xlim([-self.max_dist,self.max_dist])
self.ax.set_ylim([-self.max_dist,self.max_dist])
self.ani = FuncAnimation(self.fig, self.update, interval=5)
cid = self.fig.canvas.mpl_connect('key_press_event', self.onpress)
plt.show()
if save:
from JSAnimation import HTMLWriter
self.ani.save('animation.html', writer=HTMLWriter(embed_frames=True))
def update(self,i):
if self.forwards:
self.j += self.res
if self.j >= self.length:
self.j -= self.length
else:
self.j -= self.res
if self.j < 0:
self.j += self.length
self.scat.set_offsets(self.positions[:,self.j])
self.fig.suptitle("Time = %f, Direction = %s, speed = %d"%\
( self.times[self.j], self.direction, self.res))
def onpress(self, event):
key = event.key
if key == " ":
if self.paused:
self.ani.event_source.start()
self.paused = False
else:
self.ani.event_source.stop()
self.paused = True
elif key == "w":
self.max_dist *= 0.5
self.ax.set_xlim([-self.max_dist,self.max_dist])
self.ax.set_ylim([-self.max_dist,self.max_dist])
elif key == "e":
self.max_dist *= 2
self.ax.set_xlim([-self.max_dist,self.max_dist])
self.ax.set_ylim([-self.max_dist,self.max_dist])
elif key == "right":
self.res += 50
elif key == "left":
self.res -= 50
elif key == "down" or key == "up":
self.forwards = not self.forwards
elif key == "r":
self.j = 0
self.direction ="Forwards" if self.forwards else "Backwards"
if __name__ == "__main__":
pa = PlanetAnimator(87464)
pa.load_files()
pa.animate()