Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_cubic_golden_ratio.py
- import tkinter as tk
- import math
- def create_cube(size):
- r = size / 2
- points = [
- (0, -r),
- (r * math.cos(math.pi/6), -r * math.sin(math.pi/6)),
- (r * math.cos(math.pi/6), r * math.sin(math.pi/6)),
- (0, r),
- (-r * math.cos(math.pi/6), r * math.sin(math.pi/6)),
- (-r * math.cos(math.pi/6), -r * math.sin(math.pi/6))
- ]
- points[1] = (points[1][0]*0.8, points[1][1]*1.2)
- points[2] = (points[2][0]*0.8, points[2][1]*1.2)
- points[4] = (points[4][0]*0.8, points[4][1]*1.2)
- points[5] = (points[5][0]*0.8, points[5][1]*1.2)
- return points
- def place_tile(x, y, angle, points):
- rotated = []
- for px, py in points:
- rx = px * math.cos(angle) - py * math.sin(angle)
- ry = px * math.sin(angle) + py * math.cos(angle)
- rotated.append((x + rx, y + ry))
- color = "#{:02x}{:02x}{:02x}".format(
- int(255 * (0.5 + 0.5 * math.sin(angle))),
- int(255 * (0.5 + 0.5 * math.sin(angle + 2.1))),
- int(255 * (0.5 + 0.5 * math.sin(angle + 4.2))))
- canvas.create_polygon(rotated, fill=color, outline="black", width=1)
- def create_cubic_golden_ratio(x, y, size):
- points = create_cube(size)
- max_radius = (ww**2 + hh**2)**0.5 + size
- tile_spacing = size * 0.302
- golden_angle = math.pi * (3 - math.sqrt(5)) # ~137.5 degrees
- radius = 0
- n = 1
- while radius < max_radius:
- radius = math.sqrt(n) * tile_spacing
- angle = n * golden_angle
- tx = x + radius * math.cos(angle)
- ty = y + radius * math.sin(angle)
- n += 1
- place_tile(tx, ty, angle + math.pi/2, points)
- ww, hh = 600, 600
- root = tk.Tk()
- root.geometry(f"{ww}x{hh}+0+0")
- canvas = tk.Canvas(root, width=ww, height=hh)
- canvas.pack()
- create_cubic_golden_ratio(ww//2, hh//2, 50)
- print('done.')
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement