Advertisement
here2share

# tk_circlepack2.py

Dec 29th, 2020
1,170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.16 KB | None | 0 0
  1. # tk_circlepack2.py
  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. 0
  27. def create_circle(x, y, r, **kwargs): #center coordinates, radius
  28.     x0=x-r
  29.     y0=y-r
  30.     x1=x+r
  31.     y1=y+r
  32.     return canvas.create_oval(x0, y0, x1, y1, **kwargs)
  33. 0
  34. def hyp(a,b):
  35.     return (a**2+b**2)**0.5
  36. 0
  37. max_area=0
  38. circles=[]
  39. circles_max=[]
  40. exit=False
  41. gap=10
  42. pad=100
  43. XYO=list([(x,y) for x in range(pad, W-pad, 10) for y in range(pad, H-pad, 10)])
  44. random.shuffle(XYO)
  45. xy=XYO[:]*2
  46. t=5
  47. while True:
  48.     if not t%5:
  49.         while xy:
  50.             x,y=xy.pop()
  51.            
  52.             found_space=True
  53.             for c in circles:
  54.                 distance=hyp(c.x-x, c.y-y)
  55.                 if distance <= c.radius+gap+3:
  56.                     found_space=False
  57.                     break
  58.             if found_space:
  59.                 num_circles=len(circles)
  60.                 circles.append(circle(x, y, num_circles))
  61.                 print num_circles
  62.                 break
  63.     0
  64.     end=0
  65.     area=0
  66.     random.shuffle(circles)
  67.     for c in circles:
  68.         c.active=0
  69.         for C in circles:
  70.             if c.id == C.id: continue
  71.            
  72.             combined_radius=c.radius+C.radius
  73.            
  74.             x=c.x
  75.             y=c.y
  76.             # zzz xyt wiggles to further occupy space
  77.             wiggle=[(x+1,y-1),(x+1,y),(x+1,y+1)]
  78.             wiggle += [(x-1,y-1),(x-1,y),(x-1,y+1),(x,y-1),(x,y+1)]
  79.             random.shuffle(wiggle)
  80.             for xyt in [(x,y)]+wiggle+[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:
  85.                             if c.radius > gap:
  86.                                 c.x,c.y=xyt[0],xyt[1]
  87.                             c.active=1
  88.                             break
  89.                         elif c.radius > gap:
  90.                             c.radius -= 0.25
  91.                        
  92.                    
  93.         if c.active and (c.radius < gap*5):
  94.             c.radius += 0.2
  95.         area += c.radius
  96.     t+=1
  97.     if area > max_area:
  98.         max_area=area
  99.         circles_max=circles[:]
  100.         canvas.delete('all')
  101.         for c in circles_max:
  102.             create_circle(c.x,c.y,c.radius,fill='green')
  103.         print num_circles, max_area
  104.     root.update()
  105.  
  106. print "All Circles Generated!"
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement