三体运动
Lagrange_Treap · · 休闲·娱乐
众所周知,三体的运动是混沌的,于是,我们可以根据万有引力公式模拟一个
1.0(纯恒星版)
import turtle as t
import math
import time
import tkinter as tk
import random
# 获取屏幕尺寸
r = tk.Tk()
sw = r.winfo_screenwidth()
sh = r.winfo_screenheight()
r.destroy()
# 屏幕设置
s = t.Screen()
s.setup(sw, sh)
s.title("三体")
s.bgcolor("black")
s.tracer(0)
# 创建三个天体
a, b, c = t.Turtle(), t.Turtle(), t.Turtle()
bodys = [a, b, c]
colors = ["red", "green", "blue"]
# 速度控制参数
sf = 0.4
td = 0.01
# 随机速度范围
min_v = -1
max_v = 1
# 初始化天体
def init(t, clr, sz, x, y):
t.shape("circle")
t.color(clr)
t.shapesize(sz)
t.penup()
t.goto(x, y)
t.pendown()
t.pensize(0.3)
# 随机生成初始速度
t.vx = random.uniform(min_v, max_v)
t.vy = random.uniform(min_v, max_v)
t.m = sz * 150 # 质量
# 初始化三个天体(位置固定,速度随机)
def init_rand_vel():
init(a, colors[0], 0.5, -200, 100)
init(b, colors[1], 0.5, 200, 100)
init(c, colors[2], 0.5, 0, -200)
# 初始化
init_rand_vel()
G = 200 # 引力常数
def grav(x, y):
dx = y.xcor() - x.xcor()
dy = y.ycor() - x.ycor()
d = max(math.sqrt(dx**2 + dy**2), 50) # 最小距离50
f = G * x.m * y.m / d**2
return f * dx/d, f * dy/d
def update():
forces = [(0,0)]*3 # 各天体受力
# 计算引力
for i in range(3):
for j in range(i+1, 3):
fx, fy = grav(bodys[i], bodys[j])
forces[i] = (forces[i][0]+fx, forces[i][1]+fy)
forces[j] = (forces[j][0]-fx, forces[j][1]-fy)
# 更新运动状态
for i in range(3):
# 加速度
ax, ay = forces[i][0]/bodys[i].m, forces[i][1]/bodys[i].m
bodys[i].vx += ax * sf
bodys[i].vy += ay * sf
# 更新位置
bodys[i].setx(bodys[i].xcor() + bodys[i].vx)
bodys[i].sety(bodys[i].ycor() + bodys[i].vy)
# 边界处理
w, h = sw/2-20, sh/2-20
x, y = bodys[i].xcor(), bodys[i].ycor()
if x < -w:
bodys[i].setx(-w)
bodys[i].vx = -bodys[i].vx * 0.9
elif x > w:
bodys[i].setx(w)
bodys[i].vx = -bodys[i].vx * 0.9
if y < -h:
bodys[i].sety(-h)
bodys[i].vy = -bodys[i].vy * 0.9
elif y > h:
bodys[i].sety(h)
bodys[i].vy = -bodys[i].vy * 0.9
info = t.Turtle()
info.speed(0)
info.color("white")
info.penup()
info.hideturtle()
info.goto(0, sh/2 - 50)
info.write("ESC:退出 R:重置", align="center", font=("SimHei", 12, "normal"))
# 重置功能 - 重新生成随机初始速度
def reset():
for bd in bodys:
bd.clear()
init_rand_vel()
# 键盘事件
s.listen()
s.onkeypress(s.bye, "Escape")
s.onkeypress(reset, "r")
# 主循环
try:
while True:
update()
s.update()
time.sleep(td)
except:
pass
2.0(加行星,无尾迹版)
import turtle as t
import math
import time
import tkinter as tk
import random
# 获取屏幕尺寸
r = tk.Tk()
sw = r.winfo_screenwidth()
sh = r.winfo_screenheight()
r.destroy()
# 屏幕设置
s = t.Screen()
s.setup(sw, sh)
s.title("《三体》模拟")
s.bgcolor("black")
s.tracer(0)
# 创建天体:a/b/c=三颗恒星,d=行星
a, b, c, d = t.Turtle(), t.Turtle(), t.Turtle(), t.Turtle()
bodys = [a, b, c, d]
colors = ["#FF4500", "#32CD32", "#1E90FF", "yellow"]
# 背景矩形
bg_canvas = s.getcanvas()
bg_rect = bg_canvas.create_rectangle(-sw//2, -sh//2, sw//2, sh//2,
fill="black", outline="black")
# 核心参数
sf = 0.06 # 时间步长系数
td = 0.02 # 帧间隔
# 速度范围
min_v_star = -0.05
max_v_star = 0.05
p_min_v = 0.8
p_max_v = 1.1
p_min_v_y = -0.2
p_max_v_y = 0.2
# 天体初始化
def init(t, clr, sz, x, y, is_p=False):
t.hideturtle()
t.shape("circle")
t.color(clr)
t.shapesize(sz)
t.penup()
t.goto(x, y)
t.pendown()
t.pensize(0.8)
if is_p:
t.m = sz * 150 / 100000000
t.vx = random.uniform(p_min_v, p_max_v)
t.vy = random.uniform(p_min_v_y, p_max_v_y)
else:
base_mass = sz * 400
random_mass = random.uniform(-20, 20)
t.m = base_mass + random_mass
t.vx = random.uniform(min_v_star, max_v_star)
t.vy = random.uniform(min_v_star, max_v_star)
t.p = is_p
t.showturtle()
# 初始位置设置
def init_rand_vel():
init(a, colors[0], 0.6, -150, 260)
init(b, colors[1], 0.6, 150, 260)
init(c, colors[2], 0.6, 0, -260)
init(d, colors[3], 0.2, 350, 0, is_p=True)
# 初始化天体
init_rand_vel()
# 引力常数
G = 350
# 引力计算
def grav(x, y):
dx = y.xcor() - x.xcor()
dy = y.ycor() - x.ycor()
dist_sq = dx*dx + dy*dy
min_dist = 120 if (x.p or y.p) else 100
d = math.sqrt(dist_sq) if dist_sq > (min_dist**2) else min_dist
f = G * x.m * y.m / (d*d)
return f * dx/d, f * dy/d
# 运动更新
def update():
forces = [(0.0, 0.0)] * 4
for i in range(4):
x = bodys[i]
for j in range(i+1, 4):
y = bodys[j]
if x.p:
if not y.p:
fx, fy = grav(x, y)
forces[i] = (forces[i][0] + fx, forces[i][1] + fy)
elif y.p:
fx, fy = grav(y, x)
forces[j] = (forces[j][0] + fx, forces[j][1] + fy)
else:
fx, fy = grav(x, y)
forces[i] = (forces[i][0] + fx, forces[i][1] + fy)
forces[j] = (forces[j][0] - fx, forces[j][1] - fy)
w_half = sw//2 - 30
h_half = sh//2 - 30
for i in range(4):
body = bodys[i]
fx, fy = forces[i]
mass = body.m
ax = fx / mass
ay = fy / mass
body.vx += ax * sf
body.vy += ay * sf
new_x = body.xcor() + body.vx
new_y = body.ycor() + body.vy
if new_x < -w_half or new_x > w_half:
new_x = max(-w_half, min(new_x, w_half))
body.vx = -body.vx * 0.9
if new_y < -h_half or new_y > h_half:
new_y = max(-h_half, min(new_y, h_half))
body.vy = -body.vy * 0.9
body.penup()
body.goto(new_x, new_y)
body.pendown()
# 信息显示
info = t.Turtle()
info.speed(0)
info.color("white")
info.penup()
info.hideturtle()
info.goto(0, sh//2 - 50)
info.write("《三体》模拟 | ESC:退出 R:重置 ",
align="center", font=("SimHei", 12, "normal"))
# 重置功能
def reset():
init_rand_vel()
# 键盘事件绑定
s.listen()
s.onkey(s.bye, "Escape")
s.onkey(reset, "r")
# 主循环
try:
while True:
update()
s.update()
time.sleep(td)
except:
s.bye()
pass
2.5 (2.0重做版)
import turtle as t
import math
import time
import tkinter as tk
import random
# 获取屏幕尺寸
r = tk.Tk()
sw = r.winfo_screenwidth()
sh = r.winfo_screenheight()
r.destroy()
# 屏幕设置
s = t.Screen()
s.setup(sw, sh)
s.title("《三体》模拟")
s.bgcolor("black")
s.tracer(0)
# 创建天体:a/b/c=三颗恒星,d=行星
a, b, c, d = t.Turtle(), t.Turtle(), t.Turtle(), t.Turtle()
bodys = [a, b, c, d]
colors = ["white", "white", "white", "cyan"]
# 背景矩形
bg_canvas = s.getcanvas()
bg_rect = bg_canvas.create_rectangle(-sw//2, -sh//2, sw//2, sh//2,
fill="black", outline="black")
# 核心参数
sf = 0.01 # 时间步长系数
td = 0.02 # 帧间隔
# 速度范围
min_v_star = -0.2
max_v_star = 0.2
# 天体初始化
def init(t, clr, sz, x, y, is_p=False):
t.hideturtle()
t.shape("circle")
t.color(clr)
t.shapesize(sz)
t.penup()
t.goto(x, y)
t.pendown()
t.pensize(0.8)
if is_p:
t.m = sz * 600 / 250000
t.vx = random.uniform(min_v_star, max_v_star)
t.vy = random.uniform(min_v_star, max_v_star)
else:
base_mass = sz * 600
random_mass = random.uniform(-20, 20)
t.m = base_mass + random_mass
t.vx = random.uniform(min_v_star, max_v_star)
t.vy = random.uniform(min_v_star, max_v_star)
t.p = is_p
t.showturtle()
# 初始位置设置
def init1():
init(a, colors[0], 0.6, -150, 260)
init(b, colors[1], 0.6, 150, 260)
init(c, colors[2], 0.6, 0, -260)
init(d, colors[3], 0.4, 350, 0, is_p=True)
# 初始化天体
init1()
# 引力常数
G = 500
# 引力计算
def grav(x, y):
dx = y.xcor() - x.xcor()
dy = y.ycor() - x.ycor()
dist_sq = dx * dx + dy * dy
min_dist = 50
d = math.sqrt(dist_sq) if dist_sq > (min_dist**2) else min_dist
f = G * x.m * y.m / (d*d)
return f * dx/d, f * dy/d
# 运动更新
def update():
forces = [(0.0, 0.0)] * 4
for i in range(4):
x = bodys[i]
for j in range(i+1, 4):
y = bodys[j]
fx, fy = grav(x, y)
forces[i] = (forces[i][0] + fx, forces[i][1] + fy)
forces[j] = (forces[j][0] - fx, forces[j][1] - fy)
w_half = sw//2 - 30
h_half = sh//2 - 30
for i in range(4):
body = bodys[i]
fx, fy = forces[i]
mass = body.m
ax = fx / mass
ay = fy / mass
body.vx += ax * sf
body.vy += ay * sf
new_x = body.xcor() + body.vx
new_y = body.ycor() + body.vy
if new_x < -w_half or new_x > w_half:
new_x = max(-w_half, min(new_x, w_half))
body.vx = -body.vx * 0.9
if new_y < -h_half or new_y > h_half:
new_y = max(-h_half, min(new_y, h_half))
body.vy = -body.vy * 0.9
body.penup()
body.goto(new_x, new_y)
body.pendown()
# 信息显示
info = t.Turtle()
info.speed(0)
info.color("white")
info.penup()
info.hideturtle()
info.goto(0, sh//2 - 50)
info.write("《三体》模拟 | ESC:退出 R:重置 ",
align="center", font=("SimHei", 12, "normal"))
# 重置功能
def reset():
init1()
# 键盘事件绑定
s.listen()
s.onkey(s.bye, "Escape")
s.onkey(reset, "r")
# 主循环
try:
while True:
update()
s.update()
time.sleep(td)
except:
s.bye()
pass
2.5.1 (2.5调参版)
import turtle as t
import math
import time
import tkinter as tk
import random
# 获取屏幕尺寸
r = tk.Tk()
sw = r.winfo_screenwidth()
sh = r.winfo_screenheight()
r.destroy()
# 屏幕设置
s = t.Screen()
s.setup(sw, sh)
s.title("《三体》模拟")
s.bgcolor("black")
s.tracer(0)
# 创建天体:a/b/c=三颗恒星,d=行星
a, b, c, d = t.Turtle(), t.Turtle(), t.Turtle(), t.Turtle()
bodys = [a, b, c, d]
colors = ["white", "white", "white", "cyan"]
# 背景矩形
bg_canvas = s.getcanvas()
bg_rect = bg_canvas.create_rectangle(-sw//2, -sh//2, sw//2, sh//2,
fill="black", outline="black")
# 核心参数
sf = 0.1 # 时间步长系数
td = 0.02 # 帧间隔
# 速度范围
min_v_star = -0.2
max_v_star = 0.2
# 天体初始化
def init(t, clr, sz, x, y, is_p=False):
t.hideturtle()
t.shape("circle")
t.color(clr)
t.shapesize(sz)
t.penup()
t.goto(x, y)
t.pendown()
t.pensize(0.8)
if is_p:
t.m = sz * 150 / 25000
t.vx = random.uniform(min_v_star, max_v_star)
t.vy = random.uniform(min_v_star, max_v_star)
else:
base_mass = sz * 150
random_mass = random.uniform(-10, 1000)
t.m = base_mass + (base_mass * (1 / random_mass))
t.vx = random.uniform(min_v_star, max_v_star)
t.vy = random.uniform(min_v_star, max_v_star)
t.p = is_p
t.showturtle()
# 初始位置设置
def init1():
init(a, colors[0], 0.6, -150, 260)
init(b, colors[1], 0.6, 150, 260)
init(c, colors[2], 0.6, 0, -260)
init(d, colors[3], 0.4, 350, 0, is_p=True)
# 初始化天体
init1()
# 引力常数
G = 1000
# 引力计算
def grav(x, y):
dx = y.xcor() - x.xcor()
dy = y.ycor() - x.ycor()
dist_sq = dx * dx + dy * dy
min_dist = 50
d = math.sqrt(dist_sq) if dist_sq > (min_dist**2) else min_dist
f = G * x.m * y.m / (d*d)
return f * dx/d, f * dy/d
# 运动更新
def update():
forces = [(0.0, 0.0)] * 4
for i in range(4):
x = bodys[i]
for j in range(i+1, 4):
y = bodys[j]
fx, fy = grav(x, y)
forces[i] = (forces[i][0] + fx, forces[i][1] + fy)
forces[j] = (forces[j][0] - fx, forces[j][1] - fy)
w_half = sw//2 - 30
h_half = sh//2 - 30
for i in range(4):
body = bodys[i]
fx, fy = forces[i]
mass = body.m
ax = fx / mass
ay = fy / mass
body.vx += ax * sf
body.vy += ay * sf
new_x = body.xcor() + body.vx
new_y = body.ycor() + body.vy
if new_x < -w_half or new_x > w_half:
new_x = max(-w_half, min(new_x, w_half))
body.vx *= -0.1
if new_y < -h_half or new_y > h_half:
new_y = max(-h_half, min(new_y, h_half))
body.vy *= -0.1
def UP():
bodys[0].clear()
bodys[1].clear()
bodys[2].clear()
bodys[3].clear()
s.listen()
s.onkey(UP, "p")
body.goto(new_x, new_y)
body.pendown()
# 信息显示
info = t.Turtle()
info.speed(0)
info.color("white")
info.penup()
info.hideturtle()
info.goto(0, sh//2 - 50)
info.write("《三体》模拟 | ESC:退出 | R:重置 | P清除尾迹",
align="center", font=("SimHei", 12, "normal"))
# 重置功能
def reset():
init1()
# 键盘事件绑定
s.listen()
s.onkey(s.bye, "Escape")
s.onkey(reset, "r")
# 主循环
try:
while True:
update()
s.update()
time.sleep(td)
except:
s.bye()
pass
有什么好的意见可以和我说,考虑加
求赞AWA
2025/9/13 感谢@AzusaShirasu提出的建议,正在学习 BH 算法。