math is fun and useful

WARNING: this post contains math, specifically geometry, and talks about programming sort of, a little.
You have been warned.

So I am writing some software. And I have to draw some number of circles (I don't know how many) along an arc (I don't know how long). And the circles need to NOT overlap.
How to figure out the radius of the circles so that the don't overlap?

Well I do know two things:

  1. the radius of the big circle along whose edge the circles are distributed; I said they were distributed along an arc, and that arc is part of the perimeter of a larger circle.
  2. the angle between the centers of the circles; they are evenly distributed along said arc.


Time for some math!
Geometry says that the length of a circle's perimeter is 2 * PI * radius. I know the radius of the big circle (big_r), so I know the perimeter.
I also know the angle between two small circles (ang), so I know the arc length between two circles:
2 * PI * big_r * 360/ang
That is to say, the big circle perimeter is the length of an arc for 360 degrees, so ratio it down to angle ang.

However, this gives us the arc between two small circle centers.
An ARC, which is a curved line, not a straight line.
So if I use half of the arc as the radius of a small circle, the circles will overlap, because a curved line conntecting two points is, by definition, longer than a straight line between those two points.

Hmm...that doesn't work.

Ah, but TRIANGLES.

If we have a right triangle, math says that the sin of an angle is equal to the length of the side opposite the angle divided by the hypotenuse (the side opposite the right angle):
sin(angle) = opposite / hypotenuse
So we have a triangle connecting the two small circles and the center of the big circle. Two sides are big_r, one side is the straight line between the small circle centers. If the two small circles are just touching, then a straight line between them is the radius of one (small_r) plus the radius of the other. Since the circles are the same size, the length of that line is 2 * small_r.
If we split that line in half, and draw a line down to the center of the big circle, we have split our triangle in half.
Now we have two right triangles. The hypotenuse length is big_r, the angle is angle/2, and small_r is opposite.
Therefore:
sin(angle/2) = small_r / big_r
small_r = sin(angle/2) * big_r
And looky there, we know angle, and we know big_r. So now we know small_r!
We can therefore draw any number of circles evenly distributed along an arc of unknown length such that the circles do not overlap, as long as we know the angle between the circles and the radius of the big circle on whose arc the circles are aligned.

Math. Is. Cool.

P.S. Note that you need angle in RADIANS, not DEGRESS, when using sin! There are 360 degrees in a circle, and there are 2 * PI radians in circle, so to convert degrees to radians:
360 degress = 2 * PI radians
1 degree = 2 * PI / 360
Since PI is about 3.14159, one degree is about 0.01745 radians...or just use the useful function in your programming language of choice:
Java: import Math; float rad = Math.toRadians(deg);
Python: import math; rad = math.radians(deg);
Perl: use Math::Trig; $rad = deg2rad($deg);

No comments: