Advertisement
JohnathanMayhem

Cicloid

Mar 27th, 2023
697
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.animation as animation
  4.  
  5. R = 3
  6.  
  7. def circle(a, b, r):
  8.     T = 100
  9.     x, y = [0]*T, [0]*T
  10.     for i,theta in enumerate(np.linspace(0,2*np.pi,T)):
  11.         x[i] = a + r*np.cos(theta)
  12.         y[i] = b + r*np.sin(theta)
  13.     return x, y
  14.  
  15.  
  16. def gen():
  17.     for theta in np.linspace(0, 20*np.pi, 150):
  18.         yield R*(theta-np.sin(theta)), R*(1-np.cos(theta)), R*theta
  19.  
  20. fig = plt.figure(figsize=(6, 3))
  21. ax = fig.add_subplot(111)
  22. ax.set_ylim(0, 20)
  23. ax.set_xlim(0, 70)
  24. ax.set_xlabel('x')
  25. ax.set_ylabel('y')
  26. ax.set_aspect('equal')
  27. #ax.grid()
  28. #time_text = ax.text(0.05, 0.8, '', transform=ax.transAxes)
  29.  
  30. cycloid, = ax.plot([], [], 'r-', lw=2)
  31. line, = ax.plot([], [], 'y-', lw=2)
  32. circle_line, = ax.plot([], [], 'g', lw=2)
  33. point, = ax.plot([], [], 'bo', ms=4)
  34.  
  35. xx, yy = [], []
  36.  
  37. def func(data):
  38.     x, y, Rt = data
  39.     #time_text.set_text(r'$\theta$ = %.2f $\pi$' % (Rt / np.pi))
  40.     xx.append(x)
  41.     yy.append(y)
  42.     cx, cy = circle(Rt, R, R)
  43.  
  44.     cycloid.set_data(xx, yy)
  45.     line.set_data((x, Rt), (y, R))
  46.     circle_line.set_data(cx, cy)
  47.     point.set_data(x, y)
  48.  
  49. ani = animation.FuncAnimation(fig, func, gen, blit=False, interval=50)
  50. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement