import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# Link lengths
L1 = 2.0 # Base
L2 = 1.0 # Input link
L3 = 1.0 # Output link
L4 = 2.0 # Coupler (same as base for parallelogram)
# Base points
A = np.array([0, 0])
D = np.array([L1, 0])
# Angle range for motion
theta_range = np.linspace(0, np.pi/3, 100) # input angle range
def update_link_positions(theta):
# Input point B
B = A + L2 * np.array([np.cos(theta), np.sin(theta)])
# Find point C using parallelogram constraint (same length and opposite side)
# Vector from B to A
BA = A - B
# Point C is at B + same direction as D to A
C = D + BA
return A, B, C, D
# Set up the plot
fig, ax = plt.subplots()
ax.set_xlim(-1, 3.5)
ax.set_ylim(-1, 2)
ax.set_aspect('equal')
line, = ax.plot([], [], 'o-', lw=2)
title = ax.set_title("")
def init():
line.set_data([], [])
return line,
def animate(i):
theta = theta_range[i]
A, B, C, D = update_link_positions(theta)
xdata = [A[0], B[0], C[0], D[0], A[0]]
ydata = [A[1], B[1], C[1], D[1], A[1]]
line.set_data(xdata, ydata)
title.set_text(f"Input Angle: {np.degrees(theta):.1f}°")
return line, title
ani = animation.FuncAnimation(fig, animate, frames=len(theta_range),
init_func=init, blit=True, interval=50)
plt.show()