Advertisement
here2share

# tk_cubic_golden_ratio.py

Apr 29th, 2025
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.82 KB | None | 0 0
  1. # tk_cubic_golden_ratio.py
  2.  
  3. import tkinter as tk
  4. import math
  5.  
  6. def create_cube(size):
  7.     r = size / 2
  8.     points = [
  9.         (0, -r),
  10.         (r * math.cos(math.pi/6), -r * math.sin(math.pi/6)),
  11.         (r * math.cos(math.pi/6), r * math.sin(math.pi/6)),
  12.         (0, r),
  13.         (-r * math.cos(math.pi/6), r * math.sin(math.pi/6)),
  14.         (-r * math.cos(math.pi/6), -r * math.sin(math.pi/6))
  15.     ]
  16.     points[1] = (points[1][0]*0.8, points[1][1]*1.2)
  17.     points[2] = (points[2][0]*0.8, points[2][1]*1.2)
  18.     points[4] = (points[4][0]*0.8, points[4][1]*1.2)
  19.     points[5] = (points[5][0]*0.8, points[5][1]*1.2)
  20.     return points
  21.  
  22. def place_tile(x, y, angle, points):
  23.     rotated = []
  24.     for px, py in points:
  25.         rx = px * math.cos(angle) - py * math.sin(angle)
  26.         ry = px * math.sin(angle) + py * math.cos(angle)
  27.         rotated.append((x + rx, y + ry))
  28.     color = "#{:02x}{:02x}{:02x}".format(
  29.         int(255 * (0.5 + 0.5 * math.sin(angle))),
  30.         int(255 * (0.5 + 0.5 * math.sin(angle + 2.1))),
  31.         int(255 * (0.5 + 0.5 * math.sin(angle + 4.2))))
  32.     canvas.create_polygon(rotated, fill=color, outline="black", width=1)
  33.  
  34. def create_cubic_golden_ratio(x, y, size):
  35.     points = create_cube(size)
  36.     max_radius = (ww**2 + hh**2)**0.5 + size
  37.     tile_spacing = size * 0.302
  38.     golden_angle = math.pi * (3 - math.sqrt(5))  # ~137.5 degrees
  39.  
  40.     radius = 0
  41.     n = 1
  42.     while radius < max_radius:
  43.         radius = math.sqrt(n) * tile_spacing
  44.        
  45.         angle = n * golden_angle
  46.         tx = x + radius * math.cos(angle)
  47.         ty = y + radius * math.sin(angle)
  48.         n += 1
  49.        
  50.         place_tile(tx, ty, angle + math.pi/2, points)
  51.  
  52. ww, hh = 600, 600
  53. root = tk.Tk()
  54. root.geometry(f"{ww}x{hh}+0+0")
  55. canvas = tk.Canvas(root, width=ww, height=hh)
  56. canvas.pack()
  57.  
  58. create_cubic_golden_ratio(ww//2, hh//2, 50)
  59. print('done.')
  60.  
  61. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement