Как построить линию, следующую за точками. Тень движения. Примерно так: https://www.youtube.com/watch?v=pEjZd-AvPco Pastebin с кодом: https://pastebin.com/AkHaEM4i
Все находится в ссылке, поэтому я не могу добавить некоторые подробности. Собираюсь вставить lorem ipsum ... Похоже, ваш пост состоит в основном из кода; пожалуйста, добавьте более подробную информацию.
class DoublePendulum:
def __init__(self,
init_state = [120,0,-20,0],
L1 = .5,
L2 = .5,
M1 = 1.0,
M2 = 2.0,
G = 9.8,
origin=(0,0)):
self.init_state = np.asarray(init_state,dtype='float')
self.params = (L1,L2,M1,M2,G)
self.origin = origin
self.time_elapsed = 0
self.state = self.init_state * np.pi/180
def position(self):
(L1, L2, M1, M2, G) = self.params
x = np.cumsum([self.origin[0],
L1 * sin(self.state[0]),
L2 * sin(self.state[2])])
y = np.cumsum([self.origin[1],
-L1 * cos(self.state[0]),
-L2 * cos(self.state[2])])
return (-x,-y)
def dstate_dt(self,state,t):
(M1,M2,L1,L2,G)=self.params
dydx = np.zeros_like(state)
dydx[0] = state[1]
dydx[2] = state[3]
cos_delta = cos(state[2] - state[0])
sin_delta = sin(state[2] - state[0])
den1 = (M1 + M2) * L1 - M2 * L1 * cos_delta * cos_delta
dydx[1] = (M2 * L1 * state[1] * state[1] * sin_delta * cos_delta
+ M2 * G * sin(state[2]) * cos_delta
+ M2 * L2 * state[3] * state[3] * sin_delta
- (M1+M2) * G * sin(state[0])) / den1
den2 = (L2 / L1) * den1
dydx[3] = (-M2 * L2 * state[3] * state[3] * sin_delta * cos_delta
+ (M1 + M2) * G * sin(state[0]) * cos_delta
- (M1 + M2) * L1 * state[1] * state[1] * sin_delta
- (M1 + M2) * G * sin(state[2])) / den2
return dydx
def step(self,dt):
self.state = integrate.odeint(self.dstate_dt, self.state, [0,dt])[1]
self.time_elapsed += dt
pendulum = DoublePendulum([120.,0.0,180.,0.0],.5,.5,10,10,10)
dt = 1./30 #fps
fig = plt.figure(1)
lim1,lim2 = 2,-2
ax = fig.add_subplot(111,aspect='equal', autoscale_on=False,
xlim=(lim1,lim2),ylim=(lim1,lim2),alpha=0.5)
ax.grid()
line, = ax.plot([],[],'o-',lw=2)
time_text = ax.text(0.02,0.95,'', transform=ax.transAxes)
def init():
line.set_data([],[])
time_text.set_text('')
return line, time_text
def animate(i):
global pendulum, dt
pendulum.step(dt)
line.set_data(*pendulum.position())
time_text.set_text('time = %.1f' % pendulum.time_elapsed)
return line, time_text
from time import time
t0 = time()
animate(0)
t1 = time()
interval = 100 * dt - (t1-t0)
ani = animation.FuncAnimation(fig,animate,frames=150,
interval=interval, blit=True, init_func=init)
fig.set_size_inches(6.5, 6.5)
plt.show()