我正在编写一个程序,以模拟从地球可见的火星的逆行运动。因此,这是地球和火星绕太阳运行的平面图。还有一条从地球到火星的直线。但是,我需要它与火星相交,并继续前进直到与x = 15线相交
另一个用户使用
向我发送了此代码我以前的代码
import math
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def _update_plot(i, fig, scat, l):
scat.set_offsets(([math.cos(math.radians(i))*5, math.sin(math.radians(i))*5], [math.cos(math.radians(i/2))*10, math.sin(math.radians(i/2))*10], [0, 0]))
l.set_data(([math.cos(math.radians(i))*5,math.cos(math.radians(i/2))*10],[math.sin(math.radians(i))*5,math.sin(math.radians(i/2))*10]))
return [scat,l]
fig = plt.figure()
x = [0]
y = [0]
ax = fig.add_subplot(111)
ax.set_aspect('equal')
ax.grid(True, linestyle = '-', color = '0.10')
ax.set_xlim([-11, 11])
ax.set_ylim([-11, 11])
l, = plt.plot([],[], 'r--', zorder=1)
scat = plt.scatter(x, y, c = x, zorder=2)
scat.set_alpha(0.8)
anim = animation.FuncAnimation(fig, _update_plot, fargs = (fig, scat, l),
frames = 720, interval = 10)
plt.show()
另一个用户代码
def _update_plot(i, fig, scat, l):
angle = math.radians(i)
sun_pos = np.array([0., 0.])
earth_pos = np.array([math.cos(angle)*5, math.sin(angle)*5])
mars_pos = np.array([math.cos(angle / 2.) * 10, math.sin(angle / 2.) * 10])
# compute the unit vector that points from Earth to Mars
direction = mars_pos - earth_pos
direction /= np.sqrt(np.dot(direction, direction))
# find out where the line would intersect the x = 15 axis
start_from = earth_pos
alpha = (15 - start_from[0]) / direction[0]
# if alpha comes out to be negative we picked the "wrong" planet
if alpha < 0:
start_from = mars_pos
direction = -direction
alpha = (15 - start_from[0]) / direction[0]
y_line = start_from[1] + alpha * direction[1]
# draw the planets
scat.set_offsets((earth_pos, mars_pos, sun_pos))
# draw the line
l.set_data(([start_from[0], 15], [start_from[1], y_line]))
return [scat,l]
所以目标是要有一条线从“地球”(最里面的移动物体)到x = 15线以及之间的另一条移动物体。
我还假设该行仅在该行到达x = 15时才出现,因此仅在轨道周期的第一和最后四分之一