Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_circle_packing.py
- import tkinter as tk
- import random
- ww = 500
- hh = 500
- # Create the root window
- root = tk.Tk()
- root.title("tk_circle_packing.py")
- # Create a canvas to draw on
- canvas = tk.Canvas(root, width=ww, height=hh)
- canvas.pack()
- # Generate a list of random colors
- colors = ["red", "orange", "yellow", "green", "blue", "purple"]
- L = len(colors)
- # Generate a list of random diameter sizes from 200 to 10
- sizes = [i for i in (101,80,70,50,21,10,5)]
- # Create a list to store the coordinates of the center of each circle
- centers = []
- XY_main = [(x, y) for x in range(ww) for y in range(hh)]
- # Generate the circles
- for size in sizes:
- xy = XY_main[:]
- random.shuffle(xy)
- for x,y in xy:
- # Check if the circle overlaps with any of the other circles
- overlap = False
- for center in centers:
- distance = ((center[0] - x) ** 2 + (center[1] - y) ** 2) ** 0.5
- if distance < size + center[2]:
- overlap = True
- break
- # If the circle does not overlap, draw it and add its center to the list of centers
- if not overlap:
- color = colors[len(centers)%L]
- canvas.create_oval(x - size, y - size, x + size, y + size, fill=color)
- centers.append((x, y, size))
- canvas.update()
- # Run the Tkinter event loop
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement