-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathball.py
More file actions
107 lines (92 loc) · 2.76 KB
/
Copy pathball.py
File metadata and controls
107 lines (92 loc) · 2.76 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
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
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from av import VideoFrame
from PIL import Image
# Acceleration due to gravity, m.s-2.
g = 9.81
# The maximum x-range of ball's trajectory to plot.
XMAX = 5
# The coefficient of restitution for bounces (-v_up/v_down).
cor = 0.65
# The time step for the animation.
dt = 0.05
# Initial position and velocity vectors.
x0, y0 = 0, 4
vx0, vy0 = 1, 0
def get_pos(t=0):
"""A generator yielding the ball's position at time t."""
x, y, vx, vy = x0, y0, vx0, vy0
while x < XMAX:
t += dt
x += vx0 * dt
y += vy * dt
vy -= g * dt
if y < 0:
# bounce!
y = 0
vy = -vy * cor
yield x, y
'''
# Set up a new Figure, with equal aspect ratio so the ball appears round.
count = 1
gen = get_pos()
while count <= 50:
x, y = next(gen)
fig, ax = plt.subplots()
ax.set_aspect('equal')
# These are the objects we need to keep track of.
line, = ax.plot([x], [y], lw=2, marker="o", markersize=5, markerfacecolor="blue")
plt.xlim([-1, 5])
plt.ylim([0, 5])
plt.savefig("img/image" + str(count) + ".png")
plt.close()
count += 1
'''
class Ball():
def __init__(self):
# Acceleration due to gravity, m.s-2.
self.g = 9.81
# The maximum x-range of ball's trajectory to plot.
self.XMAX = 500
# The coefficient of restitution for bounces (-v_up/v_down).
self.cor = 0.65
# The time step for the animation.
self.dt = 0.05
# Initial position and velocity vectors.
self.x0, self.y0 = 0, 4
self.vx0, self.vy0 = 1, 0
self.t = 0
def get_pos(self, t=0):
"""A generator yielding the ball's position at time t."""
x, y, vx, vy = self.x0, self.y0, self.vx0, self.vy0
while x < XMAX:
t += dt
x += vx0 * dt
y += vy * dt
vy -= g * dt
if y < 0:
# bounce!
y = 0
vy = -vy * cor
yield x, y
def draw_image(self, x, y):
fig, ax = plt.subplots()
ax.set_aspect('equal')
# These are the objects we need to keep track of.
line, = ax.plot([x], [y], lw=2, marker="o", markersize=5, markerfacecolor="blue")
plt.xlim([-1, 5])
plt.ylim([0, 5])
plt.savefig("temp.png")
plt.close()
def generate_frames(self):
gen = self.get_pos()
count = 1
frames = []
while count <= 10:
x, y = next(gen)
self.draw_image(x, y)
img = Image.open("temp.png")
frame = VideoFrame.from_image(img)
count += 1
frames.append(frame)
return frames