-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimation.py
61 lines (45 loc) · 1.44 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
#note, this program currently only works for three bodies. I will eventually work to generalize this to n bodies
#importing dependencies
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as an
from matplotlib.animation import FuncAnimation
#crate position arrays
bigray = []
t = []
x1 = []
x2 = []
x3 = []
y1 = []
y2 = []
y3 = []
z1 = []
z2 = []
z3 = []
bsize = 250 #size of box
with open("body_cords.dat","r") as data: #open the dat file generated by c++
for line in data: #maps the data file to the array bigray
raw = [item.strip() for item in line.split(' ')]
bigray.append(raw)
slices = len(bigray)
for line in range(slices): #splits the big array into smaller arrays for the x,y,z of each body
t.append(bigray[line][0])
x1.append(bigray[line][1])
y1.append(bigray[line][2])
z1.append(bigray[line][3])
x2.append(bigray[line][4])
y2.append(bigray[line][5])
z2.append(bigray[line][6])
x3.append(bigray[line][7])
y3.append(bigray[line][8])
z3.append(bigray[line][9])
def update(i): #updates the position of the bodies on scatter plot(s) (it is currently putting all the frames on top of each other)
ax.scatter(float(x1[i]),float(y1[i]),float(z1[i]),c='r')
ax.scatter(float(x2[i]),float(y2[i]),float(z2[i]),c='b')
ax.scatter(float(x3[i]),float(y3[i]),float(z3[i]),c='g')
return ax
#sets up the plot
fig = plt.figure()
ax = plt.axes(projection='3d')
anim = FuncAnimation(fig, update, interval=1)
plt.show()