提交 8ca38f4b 编写于 作者: 三月三net's avatar 三月三net

Python超人-宇宙模拟器

上级 fa0b5116
...@@ -106,7 +106,7 @@ def 场景1(): ...@@ -106,7 +106,7 @@ def 场景1():
def 场景2(): def 场景2():
global 星球列表, 时间步长 global 星球列表, 时间步长
时间步长 = 0.01 时间步长 = 0.02
r1, r2 = 20, 10 r1, r2 = 20, 10
# A、B 质量 # A、B 质量
mA = 1e13 mA = 1e13
...@@ -116,7 +116,7 @@ def 场景2(): ...@@ -116,7 +116,7 @@ def 场景2():
pxB, pyB = r1 + r2, r1 pxB, pyB = r1 + r2, r1
# A、B 坐标 # A、B 坐标
pA, pB = [pxA, pyA], [pxB, pyB] pA, pB = [pxA, pyA], [pxB, pyB]
pC = [0,0]
vxA, vxB = 0, 0 vxA, vxB = 0, 0
# https://latex.codecogs.com/svg.image?\frac{G.mA.mB}{(r1+r2)^{2}}%20=%20mA.\frac{vA^{2}}{r1}=%20mB.\frac{vB^{2}}{r1} # https://latex.codecogs.com/svg.image?\frac{G.mA.mB}{(r1+r2)^{2}}%20=%20mA.\frac{vA^{2}}{r1}=%20mB.\frac{vB^{2}}{r1}
# https://latex.codecogs.com/png.image?\dpi{300}\frac{G.m_{A}.m_{B}}{(r_{1}+r_{2})^{2}}%20=%20m_{A}.\frac{v{_{A}}^{2}}{r_{1}}=%20m_{B}.\frac{v{_{B}}^{2}}{r_{1}} # https://latex.codecogs.com/png.image?\dpi{300}\frac{G.m_{A}.m_{B}}{(r_{1}+r_{2})^{2}}%20=%20m_{A}.\frac{v{_{A}}^{2}}{r_{1}}=%20m_{B}.\frac{v{_{B}}^{2}}{r_{1}}
...@@ -131,14 +131,15 @@ def 场景2(): ...@@ -131,14 +131,15 @@ def 场景2():
vA, vB = [vxA, vyA], [vxB, vyB] vA, vB = [vxA, vyA], [vxB, vyB]
星球列表 = [ 星球列表 = [
星球(质量=mA, 位置=pA, 速度=vA, 颜色='blue', 名称="A"), 星球(质量=mA, 位置=pA, 速度=vA, 颜色='blue', 名称="A"),
星球(质量=mB, 位置=pB, 速度=vB, 颜色='green', 名称="B") 星球(质量=mB, 位置=pB, 速度=vB, 颜色='green', 名称="B"),
# 星球(质量=mA/1000, 位置=pC, 速度=vA, 颜色='red', 名称="C"),
] ]
坐标轴.set_xlim(-5, 60) 坐标轴.set_xlim(-10, 60)
坐标轴.set_ylim(-10, 50) 坐标轴.set_ylim(-10, 50)
场景1() # 场景1()
# 场景2() 场景2()
# 设置纵横比为固定值,使得 X、Y 刻度比例不变 # 设置纵横比为固定值,使得 X、Y 刻度比例不变
坐标轴.set_aspect('equal') 坐标轴.set_aspect('equal')
...@@ -146,7 +147,7 @@ def 场景2(): ...@@ -146,7 +147,7 @@ def 场景2():
plt.subplots_adjust(left=0.05, bottom=0.04, right=0.97, top=0.97, wspace=0.4, hspace=0.4) 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([], [], 'o', color=星球.颜色, markersize=星球.大小)[0] for 星球 in 星球列表]
轨迹列表 = [坐标轴.plot([], [], '--', color=星球.颜色)[0] for 星球 in 星球列表] 轨迹列表 = [坐标轴.plot([], [], '--', color=星球.颜色)[0] for 星球 in 星球列表]
名称列表 = [坐标轴.text(0, 0, 星球.名称, color="white", size=30, fontname='SimHei') for 星球 in 星球列表] 名称列表 = [坐标轴.text(0, 0, 星球.名称, color=星球.颜色, size=20, fontname='SimHei') for 星球 in 星球列表]
速度线列表 = [plt.Arrow(0, 0, 0, 0, color=星球.颜色) for 星球 in 星球列表] 速度线列表 = [plt.Arrow(0, 0, 0, 0, color=星球.颜色) for 星球 in 星球列表]
加速度线列表 = [plt.Arrow(0, 0, 0, 0, color=星球.颜色) for 星球 in 星球列表] 加速度线列表 = [plt.Arrow(0, 0, 0, 0, color=星球.颜色) for 星球 in 星球列表]
速度文本列表 = [坐标轴.text(0, 0, "", size=15, fontname='Consolas', color=星球.颜色) for 星球 in 星球列表] 速度文本列表 = [坐标轴.text(0, 0, "", size=15, fontname='Consolas', color=星球.颜色) for 星球 in 星球列表]
...@@ -200,7 +201,7 @@ def 更新(帧): ...@@ -200,7 +201,7 @@ def 更新(帧):
轨迹 = np.array(星球.轨迹) 轨迹 = np.array(星球.轨迹)
轨迹列表[i].set_data(轨迹[:, 0], 轨迹[:, 1]) 轨迹列表[i].set_data(轨迹[:, 0], 轨迹[:, 1])
偏移量_x, 偏移量_y = 0, 0 # 星球.名称偏移 星球.位置[0] + 偏移量_x + 0.5, 星球.位置[1] + 偏移量_y 偏移量_x, 偏移量_y = 0, 0 # 星球.名称偏移 星球.位置[0] + 偏移量_x + 0.5, 星球.位置[1] + 偏移量_y
名称列表[i].set_position((x - 0.8, y - 1)) 名称列表[i].set_position((x + 2, y - 2))
min_length, max_length = 0, 100 min_length, max_length = 0, 100
# 更新速度箭头 # 更新速度箭头
速度线列表[i].remove() 速度线列表[i].remove()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册