Position 3 circles so their circumferences are always touching

circlesgeometry

I have 3 circles with radii r1, r2, r3.

I place the first circle at (r1, r1) on x and y axis.

I then place the second at (A, r2), where A = ((r1 + r2)^2 + (r1 – r2)^2)^0.5

Now I want to place the 3rd circle r2 away from the centre of circle 2, so it rests on the circumference of circle 1.

The following diagram shows 3 circles in a possible configuration. d1 is the diameter of the red circle, d2 is the diameter of the green circle, and d3 is the diameter of the blue circle. What I need to know are the coords of the centre of the blue circle, or a and b.

The diagram may, however, be misleading because for small values of d2 and r2, the green circle tucks under the red circle.

diagram

Here is a codesandbox link that demonstrates what I mean, although the last circle is not working. codesandbox

I wish to assume that the circles are sorted in order of radius size, from biggest to smallest, although ideally it should not matter.

This is not really a code question because I can find the values iteratively with code, but would prefer a formula solution.

Best Answer

3 circles tangent to each other

As shown in the diagram above, let $A$, $B$ and $C$ be the centers of the $3$ circles. Draw the lines $AC$ and $BC$ to join these centers. Since the circles are tangent to each other, these lines go through the points where they are tangent, so each length is the sum of the circle radii, i.e.,

$$|AC| = r_1 + r_3 \tag{1}\label{eq1B}$$

$$|BC| = r_2 + r_3 \tag{2}\label{eq2B}$$

Assign the point coordinates to be $A(x_1,y_1)$, $B(x_2,y_2)$ and $C(x_3,y_3)$. With \eqref{eq1B} and \eqref{eq2B}, using these coordinates in the squares of the lengths of $|AC|$ and $|BC|$ gives

$$\begin{equation}\begin{aligned} (x_1 - x_3)^2 + (y_1 - y_3)^2 & = (r_1 + r_3)^2 \\ x_1^2 - 2x_1x_3 + x_3^2 + y_1^2 - 2y_1y_3 + y_3^2 & = r_1^2 + 2r_1r_3 + r_3^2 \end{aligned}\end{equation}\tag{3}\label{eq3B}$$

$$\begin{equation}\begin{aligned} (x_2 - x_3)^2 + (y_2 - y_3)^2 & = (r_2 + r_3)^2 \\ x_2^2 - 2x_2x_3 + x_3^2 + y_2^2 - 2y_2y_3 + y_3^2 & = r_2^2 + 2r_2r_3 + r_3^2 \end{aligned}\end{equation}\tag{4}\label{eq4B}$$

Next, \eqref{eq3B} minus \eqref{eq4B} gives

$$\begin{equation}\begin{aligned} & x_1^2 - x_2^2 + 2(x_2 - x_1)x_3 + y_1^2 - y_2^2 + 2(y_2 - y_1)y_3 = r_1^2 - r_2^2 + 2(r_1 - r_2)r_3 \\ & 2(x_2 - x_1)x_3 + 2(y_2 - y_1)y_3 = r_1^2 - r_2^2 + 2(r_1 - r_2)r_3 - x_1^2 + x_2^2 - y_1^2 + y_2^2 \\ & c_1x_3 + c_2y_3 = c_3 \end{aligned}\end{equation}\tag{5}\label{eq5B}$$

where, to make the algebra easier to deal with, I have set

$$c_1 = 2(x_2 - x_1) \tag{6}\label{eq6B}$$

$$c_2 = 2(y_2 - y_1) \tag{7}\label{eq7B}$$

$$c_3 = r_1^2 - r_2^2 + 2(r_1 - r_2)r_3 - x_1^2 + x_2^2 - y_1^2 + y_2^2 \tag{8}\label{eq8B}$$

In \eqref{eq3B}, move the $x_1^2 + y_1^2$ terms to the right to get

$$-2x_1x_3 + x_3^2 - 2y_1y_3 + y_3^2 = c_4 \tag{9}\label{eq9B}$$

where

$$c_4 = r_1^2 + 2r_1r_3 + r_3^2 - x_1^2 - x_2^2 \tag{10}\label{eq10B}$$

Assuming $x_2 \neq x_1$, so $c_1 \neq 0$ in \eqref{eq6B}, you can get from \eqref{eq5B} that

$$x_3 = \frac{c_3 - c_2y_3}{c_1} \tag{11}\label{eq11B}$$

Substituting this into \eqref{eq9B} gives

$$\begin{equation}\begin{aligned} & -2x_1\left(\frac{c_3 - c_2y_3}{c_1}\right) + \left(\frac{c_3 - c_2y_3}{c_1}\right)^2 - 2y_1y_3 + y_3^2 = c_4 \\ & -\frac{2x_1c_3}{c_1} + \left(\frac{2x_1c_2}{c_1}\right)y_3 + \frac{c_3^2}{c_1^2} - \left(\frac{2c_2c_3}{c_1^2}\right)y_3 + \left(\frac{c_2^2}{c_1^2}\right)y_3^2 - 2y_1y_3 + y_3^2 = c_4 \\ & \left(\frac{c_2^2}{c_1^2} + 1\right)y_3^2 + \left(\frac{2x_1c_2}{c_1} - \frac{2c_2c_3}{c_1^2} - 2y_1\right)y_3 - \frac{2x_1c_3}{c_1} + \frac{c_3^2}{c_1^2} - c_4 = 0 \\ & c_5y_3^2 + c_6y_3 + c_7 = 0 \end{aligned}\end{equation}\tag{12}\label{eq12B}$$

where, once again, to keep the algebra somewhat simpler, I've set

$$c_5 = \frac{c_2^2}{c_1^2} + 1 \tag{13}\label{eq13B}$$

$$c_6 = \frac{2x_1c_2}{c_1} - \frac{2c_2c_3}{c_1^2} - 2y_1 \tag{14}\label{eq14B}$$

$$c_7 = -\frac{2x_1c_3}{c_1} + \frac{c_3^2}{c_1^2} - c_4 \tag{15}\label{eq15B}$$

Using the quadratic formula in \eqref{eq12B} gives

$$y_3 = \frac{-c_6 \pm \sqrt{c_6^2 - 4c_5c_7}}{2c_5} \tag{16}\label{eq16B}$$

There are $2$ roots because circle $C$ could be located either below the tangent point between $A$ and $B$ or above it. Assuming you want the one below, choose the smaller root in \eqref{eq16B}. Finally, you can substitute that value into \eqref{eq11B} to get $x_3$. You will then have the coordinates of the center of the blue circle.