fork download
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.animation as animation
  4.  
  5. # Parametry mechanizmu
  6. r = 1.0 # długość korby
  7. l = 3.0 # długość łącznika
  8. theta = np.linspace(0, 2 * np.pi, 100) # kąt obrotu
  9.  
  10. # Funkcja do obliczania pozycji suwaków
  11. def calculate_positions(theta, r, l):
  12. x1 = r * np.cos(theta)
  13. y1 = r * np.sin(theta)
  14. x2 = r * np.cos(theta + np.pi / 2)
  15. y2 = r * np.sin(theta + np.pi / 2)
  16. x3 = r * np.cos(theta + np.pi)
  17. y3 = r * np.sin(theta + np.pi)
  18. x4 = r * np.cos(theta + 3 * np.pi / 2)
  19. y4 = r * np.sin(theta + 3 * np.pi / 2)
  20.  
  21. return x1, y1, x2, y2, x3, y3, x4, y4
  22.  
  23. # Tworzenie wykresu
  24. fig, ax = plt.subplots()
  25. ax.set_xlim(-l, l)
  26. ax.set_ylim(-l, l)
  27.  
  28. # Linie reprezentujące korby i suwaki w różnych kolorach
  29. crank1, = ax.plot([], [], 'o-', lw=2, color='red')
  30. crank2, = ax.plot([], [], 'o-', lw=2, color='green')
  31. crank3, = ax.plot([], [], 'o-', lw=2, color='blue')
  32. crank4, = ax.plot([], [], 'o-', lw=2, color='purple')
  33. link1, = ax.plot([], [], 'o-', lw=2, color='orange')
  34. link2, = ax.plot([], [], 'o-', lw=2, color='cyan')
  35. link3, = ax.plot([], [], 'o-', lw=2, color='magenta')
  36. link4, = ax.plot([], [], 'o-', lw=2, color='yellow')
  37.  
  38. # Inicjalizacja animacji
  39. def init():
  40. crank1.set_data([], [])
  41. crank2.set_data([], [])
  42. crank3.set_data([], [])
  43. crank4.set_data([], [])
  44. link1.set_data([], [])
  45. link2.set_data([], [])
  46. link3.set_data([], [])
  47. link4.set_data([], [])
  48. return crank1, crank2, crank3, crank4, link1, link2, link3, link4
  49.  
  50. # Aktualizacja animacji
  51. def animate(i):
  52. x1, y1, x2, y2, x3, y3, x4, y4 = calculate_positions(theta[i], r, l)
  53.  
  54. crank1.set_data([0, x1], [0, y1])
  55. link1.set_data([x1, x1], [y1, y1 + l])
  56.  
  57. crank2.set_data([0, x2], [0, y2])
  58. link2.set_data([x2, x2 + l], [y2, y2])
  59.  
  60. crank3.set_data([0, x3], [0, y3])
  61. link3.set_data([x3, x3], [y3, y3 - l])
  62.  
  63. crank4.set_data([0, x4], [0, y4])
  64. link4.set_data([x4, x4 - l], [y4, y4])
  65.  
  66. return crank1, crank2, crank3, crank4, link1, link2, link3, link4
  67.  
  68. # Tworzenie animacji
  69. ani = animation.FuncAnimation(fig, animate, init_func=init, frames=len(theta), interval=50, blit=True)
  70.  
  71. plt.show()
  72.  
Success #stdin #stdout 2.3s 58724KB
stdin
Standard input is empty
stdout
Standard output is empty