![]() ![]() # Attractive force: all circles move to center. Vy = (dy / d ) * (r-d ) * 0.5 if circle1 not in exclude: for i in range ( len (circles ) ):Ĭircle1 = circles for j in range (i +1, len (circles ) ):ĭ = distance (circle1. offset ) # Repulsive force: move away from intersecting circles. radius* 2 ) def pack (circles, damping= 0.1, padding= 2, exclude= ):Ĭircles. fill ) stroke ( 0.3 ) strokewidth ( 1 ) oval ( self. radius def intersects ( self, other ):ĭ = distance ( self. Offset = property (_offset ) def contains ( self, x, y ): geo import distanceĭef _init_ ( self, x, y, radius, fill= None ): # Ported from Sean McCullough's Processing code: # See also: from nodebox. You can drag circles around with the mouse. like the Graph library): circles are attracted to the center of the canvas, and repulse other intersecting circles. Here's another interesting circle packing algorithm, ported from Sean McCullough's code for Processing. transformBezierPath (path ) drawpath (path ) To make it really work, the overlap check would need to rotate the shapes and then see if there is still some room left if a shape is rotated. Here's a quick and dirty test using the same method and path1.intersects(path2) for compound paths. The method of growing circles until they run out of room is very elegant. ![]() Return True def fast_circle_collision (x1, y1, x2, y2, radius1, radius2 ): # without square root return (x1-x0 )** 2 + (y1-y0 )** 2 def circle_collision (x1, y1, x2, y2, radius1, radius2 ): population -= 1 def draw_node (x, y, r, c ):ĭ = r* 2 oval (x-r, y-r, d, d ) def distance (x0, y0, x1, y1 ): alive = False def display ( self ):ĭraw_node ( self. growth = random ( 0.1, 0.5 ) def update ( self ): alive = False # if the randomly placed new node is not alive, it means that it's overlapping, so we remove it if nodes. population -1 ):įor j in range (i +1, nodes. add ( random (w ), random (h ), r= 4 ) # brute force collision test on every node/circle for i in range (nodes. add (x=h/ 2, y=w/ 2, r= 1 ) #add the first node (in the center of the canvas) def draw ( ):īackground ( 0.27, 0.17, 0.197 ) # add a new random node to nodes.list Nodes = NodeManager ( ) #initialize an empty nodes container Global h, w, max_size, max_nodes, min_gap, nodes ![]() This program is distributed under the GNU General Public License version 2Ĭopyright: TODO, interaction & media design - " "" size ( 600, 600 ) speed ( 30 ) from math import sqrt The code could still be optimized - there's too much brute force involved - but it works pretty decently if you are not in an hurry "" "Ĭircle Packing example for Nodebox. That particolar piece of code was 100% crap, now I finally had the time to polish it a little bit since we are going to use it for some new projects. on Aug 07, 2008įew months ago I promised Tom to publish the algorithm for circle packing that we used to produce this work: ![]()
0 Comments
Leave a Reply. |