import random
from math import sin, cos, pi, log
from tkinter import *
CANVAS_WIDTH = 640
CANVAS_HEIGHT = 480
# 3D 爱心隐式曲面
def heart_surface(x, y, z):
a = x*x + (9/4)*z*z + y*y - 1
return a**3 - x*x*y**3 - (9/80)*z*z*y**3
def scatter_inside(x, y, beta=0.15):
rx = -beta * log(random.random())
ry = -beta * log(random.random())
dx = rx * (x - CANVAS_WIDTH/2)
dy = ry * (y - CANVAS_HEIGHT/2)
return x - dx, y - dy
# 蝴蝶参数曲线
def butterfly(t):
f = exp(cos(t)) - 2*cos(4*t) \
- sin(t/12)**5
return sin(t)*f, cos(t)*f
# 火箭:圆锥 + 圆柱 + 尾翼
def rocket(u, v):
x = -0.55 + u
r = 0.22
return x, cos(v*2*pi)*r, sin(v*2*pi)*r
class Orbit:
def __init__(self):
self.points = []
self.phase = "form"
self.rot = 0.0
def morph(self, target):
for p in self.points:
p.seek(target, rate=0.08)
def cruise(self):
# 以代码为轨,穿梭数字星河
self.pos += self.dir * 0.9
while True:
orbit.morph(shapes[i])
orbit.cruise()
i = (i + 1) % len(shapes)▋
0 与 1 化作星尘,字符流勾勒轨道 —— 一场属于代码的宇宙航行
拖拽旋转视角 · 滚轮缩放 · 右键平移