首页 > 解决方案 > 如何为围绕给定圆形路径移动的圆圈设置动画?

问题描述

我正在用 python 建模一个太阳系,并使用牛顿定律和一些离散的微积分,我设法得到了内部太阳系。我尝试为圆圈(代表行星)设置动画,以显示行星在围绕太阳旋转的过程中所走的路径,但我根本无法让它工作,请帮忙!我认为我做圆圈的方式是问题的根源,但目前找不到更好的方法。我试图让行星走的路径在数组 rx 和 ry 中有坐标(分别是 x 和 y 坐标)。非常感谢 这里的内行星 ---> 我的代码如下:

import numpy as np
import matplotlib.pyplot as plt
plt.style.use('dark_background')
vE=[0,3e4]#initial conditions for planetary orbits
rE=[1.5e11,0]
vMa=[0,2.4e4]
rMa=[2.28e11,0]
rMe=[5e10,0]
vMe=[0,4e4]
rV=[1e11,0]
vV=[0,3.5e4]
vE=np.array(vE)
rE=np.array(rE)
vMa=np.array(vMa)
rMa=np.array(rMa)
vV=np.array(vV)
rV=np.array(rV)
vMe=np.array(vMe)
rMe=np.array(rMe) # probably a very inneficient way of doing it

vx=[]
vy=[]
axes=[]
magr=[]
G=6.67e-11
dt=1
ax = plt.axes([0., 0., 1., 1.], xlim=(-3e11, 3e11), ylim=(-3e11, 3e11))
ax.set_aspect('equal')
circleS=plt.Circle((0,0), 1e9, color = 'r')
circleMe=plt.Circle((5e10,0),5e9, color = 'r') # setting up the circles which represent the planets
circleV=plt.Circle((1e11,0),5e9, color = 'yellow')
circleE=plt.Circle((1.5e11,0), 5e9, color = 'blue')
circleMa=plt.Circle((2.28e11,0), 5e9, color = 'red')
fig = plt.gcf()
ax = fig.gca()

#print(rE/mag(rE))
def mag(vector):
    magnitude=np.linalg.norm(vector)
    return magnitude
def evolve(v,r): #function that calculates the orbit and plots the trajector
    m=2e30
    rx=[]
    ry=[]
    for i in range(0,100000):    
        dt=1000
        axes.append(i)
        dv=-((G*m*dt)/(mag(r))**2)*r/mag(r)
        v= v+dv
        vx.append(dv[0])
        vy.append(dv[1])
        dr=v*dt
        r+=dr
        rx.append(r[0])
        ry.append(r[1])
    plt.plot(rx,ry)        
        
    
evolve(vE,rE)
evolve(vMa,rMa)
evolve(vMe,rMe)
evolve(vV,rV)

ax.add_patch(circleS) #adding the circles onto the photo
ax.add_patch(circleMe)
ax.add_patch(circleV)
ax.add_patch(circleE)
ax.add_patch(circleMa)

非常感谢。

标签: pythonanimation

解决方案


推荐阅读