太空飞船使命,生成一个地球发射、火星着陆以及下一次发射窗口返回地球的动 ...

冬雨财经  金牌会员 | 2025-2-20 06:09:38 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 841|帖子 841|积分 2523



  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.animation import FuncAnimation
  4. from mpl_toolkits.mplot3d import Axes3D
  5. # 天体参数设置(简化模型)
  6. AU = 1.5e8  # 天文单位(公里)
  7. earth_orbital_radius = 1.0 * AU
  8. mars_orbital_radius = 1.5 * AU
  9. orbital_speed = 2e4  # 简化轨道速度(km/s)
  10. # 时间参数
  11. earth_period = 365  # 天
  12. mars_period = 687  # 天
  13. transfer_time = 258  # 霍曼转移时间(天)
  14. time_step = 2  # 动画时间步长(天)
  15. total_duration = 800  # 总任务时间(天)
  16. # 初始化图形
  17. fig = plt.figure(figsize=(10, 8))
  18. ax = fig.add_subplot(111, projection='3d')
  19. ax.set_facecolor('black')
  20. # 天体初始位置
  21. def celestial_pos(t, period, radius):
  22.     angle = 2 * np.pi * t / period
  23.     return radius * np.array([np.cos(angle), np.sin(angle), 0])
  24. # 霍曼转移轨道计算
  25. def transfer_orbit(t, t_start, radius_from, radius_to, transfer_time):
  26.     angle = np.pi * (t - t_start) / transfer_time
  27.     r = radius_from + (radius_to - radius_from) * (t - t_start) / transfer_time
  28.     return r * np.array([np.cos(angle), np.sin(angle), 0])
  29. # 初始化绘图元素
  30. earth, = ax.plot([], [], [], 'o', color='blue', markersize=8)
  31. mars, = ax.plot([], [], [], 'o', color='red', markersize=6)
  32. ship, = ax.plot([], [], [], 'o', color='white', markersize=4)
  33. trajectory, = ax.plot([], [], [], '-', color='gray', alpha=0.5)
  34. sun = ax.plot([0], [0], [0], 'o', color='yellow', markersize=12)[0]
  35. # 坐标轴设置
  36. max_orbit = mars_orbital_radius * 1.2
  37. ax.set_xlim(-max_orbit, max_orbit)
  38. ax.set_ylim(-max_orbit, max_orbit)
  39. ax.set_zlim(-max_orbit / 10, max_orbit / 10)
  40. ax.axis('off')
  41. # 动画更新函数
  42. def update(frame):
  43.     t = frame * time_step
  44.     # 更新天体位置
  45.     earth_pos = celestial_pos(t, earth_period, earth_orbital_radius)
  46.     earth.set_data(earth_pos[0], earth_pos[1])
  47.     earth.set_3d_properties(0)
  48.     mars_pos = celestial_pos(t, mars_period, mars_orbital_radius)
  49.     mars.set_data(mars_pos[0], mars_pos[1])
  50.     mars.set_3d_properties(0)
  51.     # 飞船状态机
  52.     if t < transfer_time:  # 前往火星阶段
  53.         ship_pos = transfer_orbit(t, 0, earth_orbital_radius, mars_orbital_radius, transfer_time)
  54.     elif t < transfer_time + 30:  # 火星停留
  55.         ship_pos = mars_pos
  56.     else:  # 返回地球阶段
  57.         ship_pos = transfer_orbit(t - transfer_time - 30, 0, mars_orbital_radius, earth_orbital_radius, transfer_time)
  58.     ship.set_data(ship_pos[0], ship_pos[1])
  59.     ship.set_3d_properties(0)
  60.     # 更新轨迹
  61.     x, y, z = trajectory.get_data_3d()
  62.     x = np.append(x, ship_pos[0])
  63.     y = np.append(y, ship_pos[1])
  64.     z = np.append(z, 0)
  65.     trajectory.set_data(x, y)
  66.     trajectory.set_3d_properties(z)
  67.     return earth, mars, ship, trajectory, sun
  68. # 创建动画
  69. ani = FuncAnimation(fig, update, frames=int(total_duration / time_step),
  70.                     interval=50, blit=True)
  71. # 添加图例和标注
  72. ax.text(0, 0, 0, "SUN", color='yellow', ha='center')
  73. ax.text(earth_orbital_radius, 0, 0, "Earth", color='blue')
  74. ax.text(mars_orbital_radius, 0, 0, "Mars", color='red')
  75. plt.show()
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

冬雨财经

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表