Advertisement
here2share

# tk_lg_gradient_sq_spiral.py

May 15th, 2025
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.15 KB | None | 0 0
  1. # tk_lg_gradient_sq_spiral.py
  2.  
  3. from PIL import Image, ImageTk
  4. import tkinter as tk
  5.  
  6. WW, HH = 640, 640
  7. size = 16
  8.  
  9. root = tk.Tk()
  10. canvas = tk.Canvas(root, width=WW, height=HH)
  11. root.geometry(f"{WW}x{HH}+10+10")
  12. canvas.pack()
  13.  
  14. cache = []
  15.  
  16. def create_outward_spiral_matrix(size):
  17.     matrix = {(i, j): -1 for i in range(size) for j in range(size)}
  18.     directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]  # right, down, left, up
  19.     dir_idx = 0
  20.     x, y = size // 2 - 1, size // 2 - 1  # Start at center for even size
  21.     num_steps = 1
  22.     n = 0
  23.  
  24.     if matrix.get((x, y), -1) == -1:
  25.         matrix[(x, y)] = n
  26.         n += 1
  27.  
  28.     while True:
  29.         for _ in range(2):  # Two directions per layer
  30.             dx, dy = directions[dir_idx]
  31.             steps_completed = 0
  32.             for _ in range(num_steps):
  33.                 x += dx
  34.                 y += dy
  35.                 if x < 0 or x >= size or y < 0 or y >= size or matrix.get((x, y), -1) != -1:
  36.                     return matrix
  37.                 matrix[(x, y)] = n
  38.                 n += 1
  39.                 steps_completed += 1
  40.             dir_idx = (dir_idx + 1) % 4
  41.             if steps_completed < num_steps:
  42.                 return matrix
  43.         num_steps += 1
  44.  
  45. matrix = create_outward_spiral_matrix(size)
  46.  
  47. steps = [2, 4, 8, 16]
  48.  
  49. for i in range(len(steps) - 1):
  50.     current_s = steps[i]
  51.     next_s = steps[i + 1]
  52.    
  53.     current_center_start = 8 - current_s // 2
  54.     current_coords = [(x, y) for x in range(current_center_start, current_center_start + current_s)
  55.                       for y in range(current_center_start, current_center_start + current_s)]
  56.     current_colors = {}
  57.     for x, y in current_coords:
  58.         rel_x = x - current_center_start
  59.         rel_y = y - current_center_start
  60.         current_colors[(rel_x, rel_y)] = matrix[(x, y)]
  61.    
  62.     next_center_start = 8 - next_s // 2
  63.     next_coords = [(x, y) for x in range(next_center_start, next_center_start + next_s)
  64.                    for y in range(next_center_start, next_center_start + next_s)]
  65.     next_colors_rel = {}
  66.     for x, y in next_coords:
  67.         rel_x = x - next_center_start
  68.         rel_y = y - next_center_start
  69.         next_colors_rel[(rel_x, rel_y)] = matrix.get((x, y), -1)
  70.    
  71.     cache_entry = {}
  72.     for rel_pos, color in current_colors.items():
  73.         positions = [pos for pos, c in next_colors_rel.items() if c == color]
  74.         if positions:
  75.             cache_entry[color] = positions
  76.     cache.append(cache_entry)
  77.  
  78. current_step = 0
  79. images = []
  80.  
  81. for s in steps:
  82.     center_start = 8 - s // 2
  83.     img = Image.new("L", (s, s))
  84.     for x in range(s):
  85.         for y in range(s):
  86.             img.putpixel((x, y), matrix[(center_start + x, center_start + y)])
  87.     images.append(img.resize((WW, HH), Image.NEAREST))
  88.  
  89. tk_images = [ImageTk.PhotoImage(img) for img in images]
  90.  
  91. def next_image(event=None):
  92.     global current_step
  93.     if current_step < len(images):
  94.         canvas.create_image(0, 0, anchor=tk.NW, image=tk_images[current_step])
  95.         current_step += 1
  96.         root.after(1000, next_image)
  97.     else:
  98.         current_step = 0
  99.  
  100. root.after(1000, next_image)
  101. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement