here2share

# tk_spring_loaded.py

Oct 5th, 2025
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1. # tk_spring_loaded.py
  2.  
  3. import tkinter as tk
  4. import math
  5. import time
  6.  
  7. # Setup
  8. WIDTH, HEIGHT = 640, 480
  9. CENTER_X, CENTER_Y = WIDTH // 2, HEIGHT // 2
  10. canvas_scale = 100
  11. num_points = 200
  12. fov = 3.0
  13.  
  14. root = tk.Tk()
  15. root.title("tk_spring_loaded.py")
  16. canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg="black")
  17. canvas.pack()
  18.  
  19. # Generate 3D points in a spiral
  20. points = []
  21. for i in range(num_points):
  22.     a = i * 0.3
  23.     x = math.cos(a) * (1 + i * 0.01)
  24.     y = math.sin(a) * (1 + i * 0.01)
  25.     z = i * 0.05
  26.     points.append((x, y, z))
  27.  
  28. def project(x, y, z, t):
  29.     # Rotate around Y axis
  30.     angle = t * 0.5
  31.     cos_a = math.cos(angle)
  32.     sin_a = math.sin(angle)
  33.     xz = x * cos_a - z * sin_a
  34.     zz = x * sin_a + z * cos_a
  35.  
  36.     # Rotate around X axis
  37.     angle2 = t * 0.3
  38.     cos_b = math.cos(angle2)
  39.     sin_b = math.sin(angle2)
  40.     yz = y * cos_b - zz * sin_b
  41.     zz = y * sin_b + zz * cos_b
  42.  
  43.     # Perspective projection
  44.     scale = canvas_scale / (zz + fov)
  45.     px = CENTER_X + xz * scale
  46.     py = CENTER_Y + yz * scale
  47.     return px, py, zz
  48.  
  49. def animate():
  50.     canvas.delete("all")
  51.     t = time.time()
  52.  
  53.     for i, (x, y, z) in enumerate(points):
  54.         px, py, depth = project(x, y, z, t)
  55.  
  56.         # Color based on depth and time
  57.         r = int(128 + 127 * math.sin(t + depth))
  58.         g = int(128 + 127 * math.sin(t + depth + 2))
  59.         b = int(128 + 127 * math.sin(t + depth + 4))
  60.         color = f'#{r:02x}{g:02x}{b:02x}'
  61.  
  62.         size = max(1, int(6 - depth))
  63.         canvas.create_oval(px - size, py - size, px + size, py + size, fill=color, outline="")
  64.  
  65.     root.after(1, animate)
  66.  
  67. animate()
  68. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment