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 & HelpSyntax Questions › Inscribing Circles within Circles
Page Index Toggle Pages: 1
Inscribing Circles within Circles (Read 296 times)
Inscribing Circles within Circles
Jan 28th, 2009, 3:29am
 
This is probably very simple, but I need to draw a series of random smaller circles inscribed in larger circle. That is to say, that the smaller circles cannot pass beyond the boundary of the outer circle.

What is the easiest way to do this?
Re: Inscribing Circles within Circles
Reply #1 - Jan 28th, 2009, 11:41am
 
I see at least two ways to find where to place the centers of the sub-circles:
- Brute force: take x, y coordinates within the bounding square of the big circle. If dist(x, y, centerX, centerY) > radius of circle (minus minimum radius of sub-circles, minRad), drop it, draw another coordinate.
- Smarter way: make a random length between minRad and bigCircleRad - minRad, a random angle, compute x, y from this data.
Then the radius of the sub-circle is randomly drawn between minRad and min of maxRad and bigCircleRad - dist to center.

Not sure if I am very clear. I can quickly do a prototype if you want.
Re: Inscribing Circles within Circles
Reply #2 - Jan 28th, 2009, 6:00pm
 
I'm not sure I understand your second example. Could you quickly mock it up and show me what you mean?

Thanks!
Re: Inscribing Circles within Circles
Reply #3 - Jan 29th, 2009, 12:23am
 
Example:
Code:
int MIN_RAD = 10;
int MAX_RAD = 100;
int MAIN_RAD = 250;
int CIRCLE_NB = 222;

void setup()
{
 size(500, 500);
 background(255);
 noStroke();
 
 int centerX = width / 2;
 int centerY = height / 2;
 
 for (int i = 0; i < CIRCLE_NB; i++)
 {
   float d = random(0, MAIN_RAD - MIN_RAD);
   float a = random(0, TWO_PI);
   float x = centerX + d * cos(a);
   float y = centerY + d * sin(a);
   float r = random(MIN_RAD, min(MAX_RAD, MAIN_RAD - d));
   color c = lerpColor(#00FF00, #0000FF, (1 + cos(a))/2);
   
   fill(c);
   ellipse(x, y, r * 2, r * 2);
 }
}
Page Index Toggle Pages: 1