From 2a1021d745f2dbeb0827762c69a466b57dcaaa67 Mon Sep 17 00:00:00 2001 From: march3 Date: Sun, 23 Mar 2025 12:37:22 +0800 Subject: [PATCH] =?UTF-8?q?Python=E8=B6=85=E4=BA=BA-=E5=AE=87=E5=AE=99?= =?UTF-8?q?=E6=A8=A1=E6=8B=9F=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simulators/mpl_2d_simulator.py | 83 +++++++++++++++++++++++++++------- 1 file changed, 67 insertions(+), 16 deletions(-) diff --git a/simulators/mpl_2d_simulator.py b/simulators/mpl_2d_simulator.py index da08dc9..fd2f3f3 100644 --- a/simulators/mpl_2d_simulator.py +++ b/simulators/mpl_2d_simulator.py @@ -6,6 +6,8 @@ # link :https://gitcode.net/pythoncr/ # python_version :3.8 # ============================================================================== +import math + import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation @@ -13,6 +15,10 @@ from matplotlib.animation import FuncAnimation # 设置 matplotlib 支持中文显示 plt.rcParams['font.sans-serif'] = ['SimHei'] # 使用黑体字体 plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题 +plt.rcParams['font.family'] = 'Times New Roman' +# https://miktex.org/download +# 安装:https://mirrors.ustc.edu.cn/CTAN/systems/win32/miktex/setup/windows-x64/basic-miktex-24.1-x64.exe +# plt.rcParams['text.usetex'] = 'True' # 定义物理常量 # 万有引力常量,单位:m^3 kg^-1 s^-2 @@ -21,7 +27,7 @@ plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题 # 定义星球类 class 星球: - def __init__(self, 质量, 位置, 速度, 颜色, 大小=None, 名称=None, 名称偏移=[0, 0], 轨迹最大长度=2000): + def __init__(self, 质量, 位置, 速度, 颜色, 大小=None, 名称=None, 名称偏移=[2, 0], 轨迹最大长度=2200): """ 初始化星球对象。 @@ -81,6 +87,7 @@ class 星球: # 时间步长,单位:s 时间步长 = 0.01 + def 场景1(): global 星球列表, 时间步长 时间步长 = 0.01 @@ -96,23 +103,52 @@ def 场景1(): def 场景2(): global 星球列表, 时间步长 - 时间步长 = 0.2 + 时间步长 = 0.01 星球列表 = [ - 星球(质量=1e12, 位置=[10, 0], 速度=[0, 1], 颜色='blue', 名称="蓝星", 名称偏移=[1, 0]), - 星球(质量=1e12, 位置=[-10, 0], 速度=[0, -1], 颜色='green', 名称="绿星", 名称偏移=[1, 0]) + 星球(质量=1e13, 位置=[0, 20], 速度=[0, 0], 颜色='blue', 名称="A", 名称偏移=[1, 0]), + 星球(质量=1e13, 位置=[30, 20], 速度=[0, 0], 颜色='green', 名称="B", 名称偏移=[1, 0]) ] 坐标轴.set_xlim(-50, 50) 坐标轴.set_ylim(-50, 50) -场景1() -# 场景2() +def 场景2(): + global 星球列表, 时间步长 + 时间步长 = 0.01 + r1, r2 = 20, 10 + # A、B 质量 + mA = 1e13 + mB = 2 * mA + + pxA, pyA = 0, r1 + pxB, pyB = r1 + r2, r1 + # A、B 坐标 + pA, pB = [pxA, pyA], [pxB, pyB] + + vxA, vxB = 0, 0 + # 万有引力常量 *mA * mB/ math.pow(r1+r2,2) = mA*math.pow(vyA,2)/r1 + vyA = -math.sqrt((万有引力常量 * mA * mB) / math.pow(r1 + r2, 2) * r1 / mA) + # 万有引力常量 *mA * mB/ math.pow(r1+r2,2) = mB*math.pow(vyB,2)/r2 + vyB = math.sqrt((万有引力常量 * mA * mB) / math.pow(r1 + r2, 2) * r2 / mB) + vA, vB = [vxA, vyA], [vxB, vyB] + 星球列表 = [ + 星球(质量=mA, 位置=pA, 速度=vA, 颜色='blue', 名称="A"), + 星球(质量=mB, 位置=pB, 速度=vB, 颜色='green', 名称="B") + ] + 坐标轴.set_xlim(-5, 60) + 坐标轴.set_ylim(-10, 50) + + +# 场景1() +场景2() +# 设置纵横比为固定值,使得 X、Y 刻度比例不变 +坐标轴.set_aspect('equal') # 调整边距 plt.subplots_adjust(left=0.05, bottom=0.04, right=0.97, top=0.97, wspace=0.4, hspace=0.4) 点列表 = [坐标轴.plot([], [], 'o', color=星球.颜色, markersize=星球.大小)[0] for 星球 in 星球列表] 轨迹列表 = [坐标轴.plot([], [], '-', color=星球.颜色)[0] for 星球 in 星球列表] -名称列表 = [坐标轴.text(0, 0, 星球.名称, color=星球.颜色) for 星球 in 星球列表] +名称列表 = [坐标轴.text(0, 0, 星球.名称, color="white", size=30, fontname='SimHei') for 星球 in 星球列表] # 初始化函数 @@ -148,17 +184,32 @@ def 更新(帧): 点列表[i].set_data(星球.位置[0], 星球.位置[1]) 轨迹 = np.array(星球.轨迹) 轨迹列表[i].set_data(轨迹[:, 0], 轨迹[:, 1]) - 偏移量_x, 偏移量_y = 星球.名称偏移 - 名称列表[i].set_position((星球.位置[0] + 偏移量_x + 0.5, 星球.位置[1] + 偏移量_y)) - 速度 = 坐标轴.text(星球.位置[0] + 偏移量_x + 0.5, 星球.位置[1] + 偏移量_y + 3, - f"v:{round(星球.速度[0], 2)},{round(星球.速度[1], 2)}", color=星球.颜色) - 加速度 = 坐标轴.text(星球.位置[0] + 偏移量_x + 0.5, 星球.位置[1] + 偏移量_y + 5, - f"a:{round(星球.加速度[0], 2)},{round(星球.加速度[1], 2)}", color=星球.颜色) + 偏移量_x, 偏移量_y = 0, 0 # 星球.名称偏移 星球.位置[0] + 偏移量_x + 0.5, 星球.位置[1] + 偏移量_y + 名称列表[i].set_position((星球.位置[0] - 0.8, 星球.位置[1] - 1)) + 速度 = 坐标轴.text(星球.位置[0] + 偏移量_x - 2, 星球.位置[1] + 偏移量_y + 3, + f"v:{星球.速度[0]:+.2f},{星球.速度[1]:+.2f}(m/s)\na:{星球.加速度[0]:+.2f},{星球.加速度[1]:+.2f}(m/s²)", + size=15, fontname='Consolas', color=星球.颜色) + 速度列表.append(速度) - 加速度列表.append(加速度) - # 名称列表[i].set_position((星球.位置[0] + 偏移量_x + 0.5, 星球.位置[1] + 偏移量_y)) - return 点列表 + 轨迹列表 + 名称列表 + 速度列表 + 加速度列表 + # 名称列表[i].set_position((星球.位置[0] + 偏移量_x + 0.5, 星球.位置[1] + 偏移量_y)) + return 点列表 + 轨迹列表 + 名称列表 + 速度列表 + + +# 设置窗口大小(单位:英寸) +width = 9 +height = 11 +图形.set_size_inches(width, height) + +# 设置窗口位置(x, y, width, height) +x_pos = 1020 +y_pos = 0 +try: + manager = plt.get_current_fig_manager() + if hasattr(manager, 'window'): + manager.window.setGeometry(x_pos, y_pos, int(width * 100), int(height * 100)) +except AttributeError: + print("无法设置窗口位置,可能是因为使用的后端不支持此操作。") # 创建动画 动画 = FuncAnimation(图形, 更新, frames=range(1000), init_func=初始化, blit=True, interval=2) -- GitLab