Advertisement
JohnathanMayhem

Epicicloid

Mar 27th, 2023
1,741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.animation as animation
  4.  
  5. R1 = 8
  6. R2 = 8
  7.  
  8. def circle(a, b, r):
  9.     T = 100
  10.     x, y = [0]*T, [0]*T
  11.     for i,theta in enumerate(np.linspace(0,2*np.pi,T)):
  12.         x[i] = a + r*np.cos(theta)
  13.         y[i] = b + r*np.sin(theta)
  14.     return x, y
  15.  
  16.  
  17. def gen():
  18.     for theta in np.linspace(0,15*np.pi,500):
  19.         k = R1/R2
  20.         a = R2 * (k + 1)*(np.cos(theta) - np.cos((k + 1) * theta )/ (k + 1))
  21.         b = R2 * (k + 1)*(np.sin(theta) - np.sin((k + 1) * theta )/ (k + 1))
  22.         yield a, b, (R1+R2) * np.cos(theta), (R1+R2) * np.sin(theta)
  23.  
  24. fig = plt.figure(figsize=(6, 3))
  25. ax = fig.add_subplot(111)
  26. ax.set_ylim(-20, 20)
  27. ax.set_xlim(-20, 20)
  28. ax.set_xlabel('x')
  29. ax.set_ylabel('y')
  30. ax.set_aspect('equal')
  31. #ax.grid()
  32. time_text = ax.text(0.05, 0.8, '', transform=ax.transAxes)
  33.  
  34. cardiod, = ax.plot([], [], 'r-', lw=2)
  35. line, = ax.plot([], [], 'y-', lw=2)
  36. circle_line, = ax.plot([], [], 'g', lw=2)
  37. circle_static, = ax.plot([], [], 'g', lw=2)
  38. point, = ax.plot([], [], 'bo', ms=4)
  39.  
  40. xx, yy = [], []
  41.  
  42. def func(data):
  43.     x, y, Rx, Ry = data
  44.     xx.append(x)
  45.     yy.append(y)
  46.     cx, cy = circle(Rx, Ry, R2)
  47.     c1_x, c1_y = circle(0, 0, R1)
  48.  
  49.     cardiod.set_data(xx, yy)
  50.     line.set_data((x, Rx), (y, Ry))
  51.     circle_line.set_data(cx, cy)
  52.     circle_static.set_data(c1_x, c1_y)
  53.     point.set_data(x, y)
  54.  
  55. ani = animation.FuncAnimation(fig, func, gen, blit=False, interval=50)
  56. plt.show()
  57. # fn = 'cacloid_FuncAnimation'
  58. # #ani.save('%s.mp4'%(fn), writer='ffmpeg', fps=1000/50)
  59. # xx, yy = [], []
  60. # ani.save('%s.gif'%(fn), writer='imagemagick', fps=1000/50)
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement