[Math] Calculating a tangent arc between two points on two circles

circlescomputational geometrygeometry

How can I calculate the arc between two circles? The arc must be tangent to the two points on the circles.

Here is a picture illustrating it. I'm trying to code and calculate the orange arc and the blue arc.

circle arcs

The points B, F, D, and E can be changed by the user and dynamically update.

Best Answer

As others have mentioned in comments, your control points cannot be independent. Nevertheless, if we assume that a given configuration has the properties you want, we can analyze the geometry.

I'll consider the orange arc, $BE$, and I'll assume that both circles $A$ and $C$ overlap the interior of the orange circle, which I'll further assume has not degenerated into a line.

Let $a = |AB|$, $c = |CE|$, and $x=|AC|$; all of these can be considered known quantities. Let the (unknown) radius of the orange circle be $r = |PB| = |PE|$, where $P$ is the circle's center. Because radii $AB$ and $PB$ are perpendicular to a common tangent line at $B$, these segments lie on the same line; likewise for $CE$ and $PE$; consequently, $P$ lies at the intersection of the two extended radii $AB$ and $CE$, so that the angle $BPE$ is congruent to the angle between the vectors $AB$ and $CE$. Call the measure of that angle $\theta$; it, too, can be considered a known quantity.

Now, triangle $APC$ has sides of length $x$, $r-a$, and $r-c$ (the last two because of the assumed overlap of circles), with angle $\theta$ between the last two. By the Law of Cosines:

$$x^2 = (r-a)^2 + (r-c)^2 - 2 (r-a)(r-c) \cos\theta$$

Solve this quadratic equation for $r$, and you can calculate whatever else you need to know: arc length, location of $P$, equation of the orange circle, etc.

(The equation of the orange circle can be expressed in a form that degenerates into a line equation as $r$ approaches infinity. Note that, in such a degenerate case, $\theta = 0$.)