# Determination of center and radius of circles

I have a problem that I have to solve mathematically. I need the solution1 for drawing circles in a GUI:

I have a rect with width s, the height is not important. Inside this rect I draw a circle at M1 with r1 = 1/3*s, so M1 is (2/3*s, 1/3*s).

I want to draw two additional circles having the same radius (r2==r3) so that all three circles touch each other and the two additional circles touch the border of the rect. So I need to find out M2 and M3 and the radius of the two circles.

There should exist exactly one solution, I could just try to find the needed radius and centers depending on s but it should be possible to calculate that exaclty..

-

## migrated from stackoverflow.comApr 20 '12 at 12:47

This question came from our site for professional and enthusiast programmers.

I need it for programming (as noticed). It's in Java for Android, but the programming language does not matter for this problem. I have only tried with trial and error to find the centers and the radius. –  user1264585 Apr 19 '12 at 0:49
Needing some mathematics to implement some software does not automatically make that mathematics appropriate to stack-overflow. (Just as questions about archery are off-topic here even if you're writing an RPG.) –  Michael Anderson Apr 19 '12 at 1:02

Let's assume M1=(m1x, m1y), M2=(m2x, m2y), M3=(m3x, m3y).

Circles will touch each other if

|M1 M2| = r1 + r2
|M1 M3| = r1 + r2


Or

r1 + r2 = sqrt( (m1x-m2x)^2 + (m1y-m2y)^2 )
r1 + r2 = sqrt( (m1x-m3x)^2 + (m1y-m3y)^2 )


We known some of this variables values:

m1x = 2*s/3;
m1y = r1;
m2x = r2;
m3x = s - r2;


So, we have two variables (m2y, m3y) and two equations. It must be easy to find this two variables values.

-
@trutheality, just by eyeballing this figure I'd guess an answer of about 0.25, and by my back-of-an-envelope calculation (literally!) I get (1+2sqrt(6))/23, which comes out to 0.256477... –  Beta Apr 19 '12 at 5:08
Thanks a lot, it works for r2=0.256477.. * s, but how is the formula for r2 for a given r1? With the formulas from Riateche I have three variables (r2, m2y and m3y) for two equations or do I miss something? –  user1264585 Apr 19 '12 at 17:51
If you don't know r2, add the following equation: |M2 M3| = 2*r2. –  Riateche Apr 19 '12 at 20:11