Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_spring_loaded.py
- import tkinter as tk
- import math
- import time
- # Setup
- WIDTH, HEIGHT = 640, 480
- CENTER_X, CENTER_Y = WIDTH // 2, HEIGHT // 2
- canvas_scale = 100
- num_points = 200
- fov = 3.0
- root = tk.Tk()
- root.title("tk_spring_loaded.py")
- canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg="black")
- canvas.pack()
- # Generate 3D points in a spiral
- points = []
- for i in range(num_points):
- a = i * 0.3
- x = math.cos(a) * (1 + i * 0.01)
- y = math.sin(a) * (1 + i * 0.01)
- z = i * 0.05
- points.append((x, y, z))
- def project(x, y, z, t):
- # Rotate around Y axis
- angle = t * 0.5
- cos_a = math.cos(angle)
- sin_a = math.sin(angle)
- xz = x * cos_a - z * sin_a
- zz = x * sin_a + z * cos_a
- # Rotate around X axis
- angle2 = t * 0.3
- cos_b = math.cos(angle2)
- sin_b = math.sin(angle2)
- yz = y * cos_b - zz * sin_b
- zz = y * sin_b + zz * cos_b
- # Perspective projection
- scale = canvas_scale / (zz + fov)
- px = CENTER_X + xz * scale
- py = CENTER_Y + yz * scale
- return px, py, zz
- def animate():
- canvas.delete("all")
- t = time.time()
- for i, (x, y, z) in enumerate(points):
- px, py, depth = project(x, y, z, t)
- # Color based on depth and time
- r = int(128 + 127 * math.sin(t + depth))
- g = int(128 + 127 * math.sin(t + depth + 2))
- b = int(128 + 127 * math.sin(t + depth + 4))
- color = f'#{r:02x}{g:02x}{b:02x}'
- size = max(1, int(6 - depth))
- canvas.create_oval(px - size, py - size, px + size, py + size, fill=color, outline="")
- root.after(1, animate)
- animate()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment