here2share

# tk_circlepack.py ZZZ

Dec 27th, 2020 (edited)
1,146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.93 KB | None | 0 0
  1. # tk_circlepack.py ZZZ
  2.  
  3. import random
  4.  
  5. W, H=1200, 600
  6. HW, HH=W/2, H/2
  7. AREA=W*H
  8.  
  9. try:
  10.     import tkinter as tk
  11. except:
  12.     import Tkinter as tk
  13.  
  14. root=tk.Tk()
  15. root.title("Tk Circle Pack")
  16.  
  17. canvas=tk.Canvas(root,width=W,height=H)
  18. canvas.pack(fill="both",expand=True)
  19.  
  20. class circle:
  21.     def __init__(self, x, y, id):
  22.         self.x=x
  23.         self.y=y
  24.         self.radius=gap
  25.         self.id=id
  26.         self.active=10
  27. 0
  28. def create_circle(x, y, r, **kwargs): #center coordinates, radius
  29.     x0=x-r
  30.     y0=y-r
  31.     x1=x+r
  32.     y1=y+r
  33.     return canvas.create_oval(x0, y0, x1, y1, **kwargs)
  34. 0
  35. def hyp(a,b):
  36.     return (a**2+b**2)**0.5
  37. 0      
  38. circles=[]
  39. exit=False
  40. gap=10
  41. pad=100
  42. xy=list([(x,y) for x in range(pad, W-pad, 10) for y in range(pad, H-pad, 10)])
  43. random.shuffle(xy)
  44. t=10
  45. while True:
  46.     if not t%10:
  47.         while xy:
  48.             x,y=xy.pop()
  49.            
  50.             found_space=True
  51.             for c in circles:
  52.                 distance=hyp(c.x-x, c.y-y)
  53.                 if distance <= c.radius+gap+2:
  54.                     found_space=False
  55.                     break
  56.             if found_space:
  57.                 t=len(circles)
  58.                 circles.append(circle(x, y, t))
  59.                 print t
  60.                 break
  61.     0
  62.     canvas.delete('all')
  63.     end=1
  64.     for c in circles:
  65.        
  66.         if not c.active:
  67.             create_circle(c.x,c.y,c.radius,fill='green')
  68.             continue
  69.         else:
  70.             create_circle(c.x,c.y,c.radius,fill='yellow')
  71.             end=0
  72.         for C in circles:
  73.             if c.id == C.id: continue
  74.            
  75.             combined_radius=c.radius+C.radius
  76.            
  77.             x=c.x
  78.             y=c.y
  79.             # zzz xyt wiggles to further occupy space
  80.             for xyt in [(x,y),(x+1,y),(x-1,y),(x,y+1),(x,y-1),(x+1,y+1),(x-1,y-1),(x+1,y-1),(x-1,y+1),0]:
  81.                 if xyt:
  82.                     if (pad < xyt[0] < W-pad) and (pad < xyt[1] < H-pad):
  83.                         distance_between_circles=hyp(xyt[0]-C.x, xyt[1]-C.y)
  84.                         if distance_between_circles > combined_radius+c.active+1:
  85.                             c.x,c.y=xyt[0],xyt[1]
  86.                             end=0
  87.                             break
  88.                 else:
  89.                     if c.active:
  90.                         c.active -= 1
  91.                    
  92.         if c.active:
  93.             c.radius += 0.1
  94.     t+=1
  95.     root.update()
  96.     if end:
  97.         break
  98.  
  99. print "All Circles Generated!"
Add Comment
Please, Sign In to add comment