here2share

# tk_Circular_Spiral_Fractal_Animation.py

Sep 8th, 2025 (edited)
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.70 KB | None | 0 0
  1. # tk_Circular_Spiral_Fractal_Animation.py
  2. import tkinter as tk
  3. import math
  4.  
  5. WIDTH, HEIGHT = 300, 300
  6. root = tk.Tk()
  7. root.title("Circular Spiral Fractal Animation")
  8. canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg='black')
  9. canvas.pack()
  10.  
  11. rainbow = []
  12. def z(r, g, b):
  13.     rainbow.append(f'#{r:02x}{g:02x}{b:02x}')
  14.  
  15. r, g, b = 255, 0, 0
  16. for g in range(256):
  17.     z(r, g, b)
  18. for r in range(254, -1, -1):
  19.     z(r, g, b)
  20. for b in range(256):
  21.     z(r, g, b)
  22. for g in range(254, -1, -1):
  23.     z(r, g, b)
  24. for r in range(256):
  25.     z(r, g, b)
  26. for b in range(254, -1, -1):
  27.     z(r, g, b)
  28.  
  29. Lcolors = len(rainbow)
  30. steps = (WIDTH/5) * (HEIGHT/5)
  31. incr = 100 / steps
  32.  
  33. def circular_spiral_coords(width, height, step):
  34.     cx, cy = width // 2, height // 2
  35.     coords = []
  36.     max_radius = math.sqrt(width**2 + height**2)
  37.     num_points = int(max_radius / step * 360)
  38.     a = 0.1
  39.     b = 0.1
  40.     for i in range(num_points):
  41.         angle = a * i
  42.         radius = b * angle
  43.         x = (cx + radius * math.cos(angle)) // step * step
  44.         y = (cy + radius * math.sin(angle)) // step * step
  45.         if 0 <= x <= width and 0 <= y <= height:
  46.             if (x, y) not in coords:
  47.                 coords.append((x, y))
  48.     return coords
  49.  
  50. spiral_order = circular_spiral_coords(WIDTH, HEIGHT, 5)
  51.  
  52. pixels = {}
  53. i = 0
  54. for x, y in spiral_order:
  55.     j = 50 + (i * incr)
  56.     pixels[x, y] = [j, j]
  57.     i += 1
  58.  
  59. while 1:
  60.     canvas.delete("all")
  61.     for x, y in spiral_order:
  62.         i, j = pixels[x, y]
  63.         j = (i + j) % Lcolors
  64.         pixels[x, y] = [i, j]
  65.         color = rainbow[int(j)]
  66.         canvas.create_rectangle(x, y, x+5, y+5, fill=color, outline='')
  67.     canvas.update()
  68.  
  69. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment