-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircle_translation.py
68 lines (56 loc) · 2.36 KB
/
circle_translation.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
"""
Animation: Translating a circle along the x axis
"""
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib.animation import FFMpegWriter
from matplotlib.patches import Circle
# --------------------------------------------------------------------------------------------------
# Initialize the parameters
# --------------------------------------------------------------------------------------------------
dt = 0.01 # Simulation timestep (s)
radius = 1 # Current radius value
pos = np.array([0.0, 0.0]) # Current position (x, y)
pos_log = [pos] # Array to keep track of simulation data
v = np.array([3.0, 0.0]) # Speed of the circle (v_x, v_y)
# --------------------------------------------------------------------------------------------------
# Simulation
#
# Here, we translate the circle along the x-axis. At every timestep, we keep track of its position
# by appending it to our pos_log list
# --------------------------------------------------------------------------------------------------
while pos[0] < 5:
pos += dt * v
pos_log.append(list(pos))
v = -v
while pos[0] > 0:
pos += dt * v
pos_log.append(list(pos))
# --------------------------------------------------------------------------------------------------
# Animation
#
# Using the simulation data, we now make the animation
# --------------------------------------------------------------------------------------------------
# Initialize the plot
fig, ax = plt.subplots()
ax.axis('scaled')
ax.axis([-2, 7, -2, 2])
# Create and add a circle patch to the axis
patch = Circle((0, 0), radius=radius)
ax.add_patch(patch)
# Animation function to update and return the patch at each frame
def animate(i):
patch.center = pos_log[i]
return patch,
# Specify the animation parameters and call animate
ani = FuncAnimation(fig,
animate,
frames=len(pos_log), # Total number of frames in the animation
interval=int(1000 * dt), # Set the length of each frame (milliseconds)
blit=True, # Only update patches that have changed (more efficient)
repeat=False) # Only play the animation once
# Play the animation
plt.show()
# Uncomment to save the animation to a local file
# ani.save('/path/to/save/animation.mp4', writer=FFMpegWriter(fps=int(1/dt)))