We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Evenly distribute objects, prevent overlap
Page Index Toggle Pages: 1
Evenly distribute objects, prevent overlap (Read 1312 times)
Evenly distribute objects, prevent overlap
Oct 8th, 2008, 12:03am
 
I'm trying to take a number of circle-shaped objects of different radii and evenly spread them out over one plane, so none of them overlap.  I want them to be roughly centered around one point.

For example, see:  http://alignedleft.com/projects/2008/spacing-objects-question.png

The physics libraries are helpful at "pulling" them toward a central point (attraction force), and others have figured out how to calculate collisions (to bounce objects off each other), but all I want is to evenly distribute the objects in space — no "collisions" or "gravity," except that I need them to be grouped around one central point.

An added bonus would be to have the larger circles at the center, and smaller ones floating toward the outside, but that's not necessary.

Any thoughts on how to approach this?  Thanks!
Re: Evenly distribute objects, prevent overlap
Reply #1 - Nov 1st, 2008, 11:15pm
 
You shouldnt even need physics if it to be static?

I would make a list or array of location and radii.
1. choose x and y randomly
2. find the closest sphere in list
3. if x and y is inside closest sphere repeat step 1
4. make my new spheres radius just big enough to touch closest.
5. store x, y and radius to list
6. repeat step 1
Re: Evenly distribute objects, prevent overlap
Reply #2 - Nov 2nd, 2008, 4:32pm
 
You can use the physics library to get what you want, you just need to have both attractive and repulsive forces.

e.g. http://www.hardcorepawn.com/ParticleBox/
Re: Evenly distribute objects, prevent overlap
Reply #3 - Nov 4th, 2008, 3:05am
 
Thanks to you both.  I have tried both approaches, but will work a little more and let you know how it works out.
Re: Evenly distribute objects, prevent overlap
Reply #4 - Nov 7th, 2008, 8:32am
 
I got it working without physics!  I have a detailed write up on the final solution here:

http://alignedleft.com/blog/2008/11/how-to-cluster-circles-of-different-sizes-while-preventing-overlap/
Re: Evenly distribute objects, prevent overlap
Reply #5 - Nov 7th, 2008, 8:37am
 
Awesome solution!
Page Index Toggle Pages: 1