太空飞船任务,生成一个地球发射、火星着陆以及下一次发射窗口返回地球的动画3D代码

news/2025/2/21 5:11:34/

在这里插入图片描述


import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from mpl_toolkits.mplot3d import Axes3D# 天体参数设置(简化模型)
AU = 1.5e8  # 天文单位(公里)
earth_orbital_radius = 1.0 * AU
mars_orbital_radius = 1.5 * AU
orbital_speed = 2e4  # 简化轨道速度(km/s)# 时间参数
earth_period = 365  # 天
mars_period = 687  # 天
transfer_time = 258  # 霍曼转移时间(天)
time_step = 2  # 动画时间步长(天)
total_duration = 800  # 总任务时间(天)# 初始化图形
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
ax.set_facecolor('black')# 天体初始位置
def celestial_pos(t, period, radius):angle = 2 * np.pi * t / periodreturn radius * np.array([np.cos(angle), np.sin(angle), 0])# 霍曼转移轨道计算
def transfer_orbit(t, t_start, radius_from, radius_to, transfer_time):angle = np.pi * (t - t_start) / transfer_timer = radius_from + (radius_to - radius_from) * (t - t_start) / transfer_timereturn r * np.array([np.cos(angle), np.sin(angle), 0])# 初始化绘图元素
earth, = ax.plot([], [], [], 'o', color='blue', markersize=8)
mars, = ax.plot([], [], [], 'o', color='red', markersize=6)
ship, = ax.plot([], [], [], 'o', color='white', markersize=4)
trajectory, = ax.plot([], [], [], '-', color='gray', alpha=0.5)
sun = ax.plot([0], [0], [0], 'o', color='yellow', markersize=12)[0]# 坐标轴设置
max_orbit = mars_orbital_radius * 1.2
ax.set_xlim(-max_orbit, max_orbit)
ax.set_ylim(-max_orbit, max_orbit)
ax.set_zlim(-max_orbit / 10, max_orbit / 10)
ax.axis('off')# 动画更新函数
def update(frame):t = frame * time_step# 更新天体位置earth_pos = celestial_pos(t, earth_period, earth_orbital_radius)earth.set_data(earth_pos[0], earth_pos[1])earth.set_3d_properties(0)mars_pos = celestial_pos(t, mars_period, mars_orbital_radius)mars.set_data(mars_pos[0], mars_pos[1])mars.set_3d_properties(0)# 飞船状态机if t < transfer_time:  # 前往火星阶段ship_pos = transfer_orbit(t, 0, earth_orbital_radius, mars_orbital_radius, transfer_time)elif t < transfer_time + 30:  # 火星停留ship_pos = mars_poselse:  # 返回地球阶段ship_pos = transfer_orbit(t - transfer_time - 30, 0, mars_orbital_radius, earth_orbital_radius, transfer_time)ship.set_data(ship_pos[0], ship_pos[1])ship.set_3d_properties(0)# 更新轨迹x, y, z = trajectory.get_data_3d()x = np.append(x, ship_pos[0])y = np.append(y, ship_pos[1])z = np.append(z, 0)trajectory.set_data(x, y)trajectory.set_3d_properties(z)return earth, mars, ship, trajectory, sun# 创建动画
ani = FuncAnimation(fig, update, frames=int(total_duration / time_step),interval=50, blit=True)# 添加图例和标注
ax.text(0, 0, 0, "SUN", color='yellow', ha='center')
ax.text(earth_orbital_radius, 0, 0, "Earth", color='blue')
ax.text(mars_orbital_radius, 0, 0, "Mars", color='red')plt.show()

http://www.ppmy.cn/news/1573509.html

相关文章

【VSCode】一键清理旧版本插件脚本(Mac或者Windows都可)

文章目录 清理VSCode旧版本插件为什么会有旧版本插件Mac 环境脚本源码原理解释实例解释 Windows 环境 清理VSCode旧版本插件 为什么会有旧版本插件 vscode 插件经常需要更新的&#xff0c;有时候更新之后旧版本的插件还是会留存在插件文件夹中&#xff08;更新插件之后vscode…

【Go并发编程】Goroutine 调度器揭秘:从 GMP 模型到 Work Stealing 算法

每天一篇Go语言干货&#xff0c;从核心到百万并发实战&#xff0c;快来关注魔法小匠&#xff0c;一起探索Go语言的无限可能&#xff01; 在 Go 语言中&#xff0c;Goroutine 是一种轻量级的并发执行单元&#xff0c;它使得并发编程变得简单高效。而 Goroutine 的高效调度机制是…

在IDEA中误操作Git Rollback后的恢复方法

一、问题描述 在使用IDEA进行代码管理时&#xff0c;通过Git提交代码过程中不小心点击了“Rollback”选项&#xff0c;导致尚未提交的代码丢失。 二、解决办法 找到目标模块&#xff1a;在项目视图中选中需要恢复的文件或目录。 查看本地历史&#xff1a;右键点击&#xff0…

计算机视觉:卷积神经网络(CNN)基本概念(一)

第一章&#xff1a;计算机视觉中图像的基础认知 第二章&#xff1a;计算机视觉&#xff1a;卷积神经网络(CNN)基本概念(一) 第三章&#xff1a;计算机视觉&#xff1a;卷积神经网络(CNN)基本概念(二) 第四章&#xff1a;搭建一个经典的LeNet5神经网络(附代码) 第五章&#xff1…

深度学习中的知识蒸馏

知识蒸馏&#xff08;Knowledge Distillation&#xff09;是一种模型压缩技术&#xff0c;旨在将大型、复杂的模型&#xff08;通常称为教师模型&#xff09;的知识迁移到小型、简单的模型&#xff08;学生模型&#xff09;中。通过这种方式&#xff0c;学生模型可以在保持较高…

使用Java爬虫获取京东商品SKU信息的完整指南

在电商领域&#xff0c;商品SKU&#xff08;Stock Keeping Unit&#xff09;信息是商家和消费者都非常关注的内容。SKU信息不仅包括商品的基本属性&#xff08;如价格、库存、规格等&#xff09;&#xff0c;还涉及到商品的动态数据&#xff08;如促销信息、库存状态等&#xf…

深入解析 Flutter GetX

深入解析 Flutter GetX&#xff1a;从原理到实战 GetX 是 Flutter 中一个轻量级且功能强大的状态管理、路由管理和依赖注入框架。它以简单、快速、高效著称&#xff0c;适合从小型到大型项目的开发需求。GetX 的设计理念是一体化解决方案&#xff0c;通过一个框架解决状态管理…

【ArcGIS Pro二次开发】(87):样式_Style的用法

.Stylx类型的文件即为样式库文件&#xff0c;保存了符号样式。 1、根据名字获取当前工程中的style //获取当前工程中的所有style var ProjectStyles Project.Current.GetItems<StyleProjectItem>();//根据名字找出指定的style StyleProjectItem style ProjectStyles.F…