here2share

# tk_Square_Spiral_Fractal_Animation.py

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