Skip to content

Instantly share code, notes, and snippets.

@338rajesh
Created June 21, 2024 15:05
Show Gist options
  • Save 338rajesh/997b07e0a6318b72c9c5921022ea94bb to your computer and use it in GitHub Desktop.
Save 338rajesh/997b07e0a6318b72c9c5921022ea94bb to your computer and use it in GitHub Desktop.
It creates the animation for a pair of rotating gears
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from os import path, makedirs
CSD = path.dirname(__file__)
def rotate_points(xy: np.ndarray, theta: float = 0.0, pivot_point: tuple[float, float] = (0.0, 0.0), ):
""" Rotate the points `x` and `y` by specified angle about the point (xc, yc). """
return (
(xy - pivot_point) @ [[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)], ]
) + pivot_point
# Function to update the animation
def update(frame):
ax.clear()
mrr = np.deg2rad(frame)
grr = np.deg2rad((z1 / z2) * frame) * -1.0
e1 = rotate_points(curve_1, mrr, pivot_point=(x1_c, y1_c))
e2 = rotate_points(curve_2, grr, pivot_point=(x2_c, y2_c))
ax.fill(e1[:, 0], e1[:, 1], 'lightblue')
ax.fill(e2[:, 0], e2[:, 1], 'lightgreen')
ax.plot(e1[:, 0], e1[:, 1], 'k-', linewidth=0.4)
ax.plot(e2[:, 0], e2[:, 1], 'k-', linewidth=0.4)
ax.scatter([x1_c, x2_c], [y1_c, y2_c], marker='o', c='k', s=50)
ax.plot(x1_c + r_pm * np.cos(t), y1_c + r_pm * np.sin(t), 'b:', linewidth=1)
ax.plot(x2_c + r_pg * np.cos(t), y2_c + r_pg * np.sin(t), 'r:', linewidth=1)
ax.set_xlim([-75.0, 150.0])
ax.set_ylim([-100.0, 100.0])
# ax.axis('equal')
ax.set_aspect('equal')
# Create a figure and axis
fig, ax = plt.subplots()
#
rotor_data = np.load(path.join(CSD, "gears_data.npz"))
print(rotor_data.files)
curve_1 = rotor_data["gear_1_xy"]
curve_2 = rotor_data["gear_2_xy"]
a, z1, z2 = rotor_data["axis_distance"], 4, 5
t = np.linspace(0.0, 2.0 * np.pi, 500)
r_pm, r_pg = (a * z1) / (z1 + z2), (a * z2) / (z1 + z2)
x1_c, y1_c = 0.0, 0.0
x2_c, y2_c = a, 0.0
ax.fill(curve_1[:, 0], curve_1[:, 1], 'lightgray')
ax.fill(curve_2[:, 0], curve_2[:, 1], 'lightgreen')
ax.plot(curve_1[:, 0], curve_1[:, 1], 'k-', linewidth=0.4)
ax.plot(curve_2[:, 0], curve_2[:, 1], 'k-', linewidth=0.4)
ax.plot(x1_c + r_pm * np.cos(t), y1_c + r_pm * np.sin(t), 'b:', linewidth=1)
ax.plot(x2_c + r_pg * np.cos(t), y2_c + r_pg * np.sin(t), 'r:', linewidth=1)
# Create the animation
ani = FuncAnimation(fig, update, frames=np.linspace(0, 360, 500), interval=50, cache_frame_data=False)
# Show the animation
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment