Two circles in a plane may have zero, one, two, three, or four common tangents, depending on how the circles overlap.
Find the equations of the (up to) four lines tangent to circles A and B, whose centers are (xA,yA) and (xB,yB) and whose radii are rA and rB.
The way I attack this problem is to note that each of the four tangent lines intersects at right angles a radius of circle A and a radius of circle B. Moreover, the radii of the two circles are oriented in the same direction, as they are both perpendicular to a single line. Even the radii that meet the two tangent lines that cross between the circles are considered to be oriented in the same direction; however one of them is treated as having negative length! In fact, this is how I will select the four tangent lines: using the four combinations of positive and negative radius length for the two circles.
The solution to this problem will give the equation of the tangent line (given one of the four combinations of positive and negative radii of the two circles) as
Ax + By + C = 0
The big challenge in this solution is to calculate the argument of the radii that meet each of the tangent lines, which I represent as θ. I tackle that right away, giving you A and B which are the cosine and sine of θ, respectively, in terms of the centers and radii of the two circles. In this way, by knowing the sine and cosine of θ, the quadrant is uniquely specified, and so it is possible to pick the right inverse sine and inverse cosine, and so θ is pinned down exactly.
Finally, we calculate C in terms of A, B, and the center and radius of one of the circles (it doesn't matter which circle, as it turns out), which completes the task.
Part 1: finding A and B, parameterized as A=cos θ and B=sin θ
The distance from a line of the form Ax+By+C=0 to a point (h,k) is |Ah+Bk+C|/sqrt(A2+B2) (proof), and the parameters A, B, C of the line can be scaled by dividing each one by sqrt(A2+B2) so that after they have been scaled, A2+B2=1. Then you can define θ so that A = cos θ, and B = sin θ.
Then the tangent lines, expressed as (cosθ)x+(sinθ)y+C=0, and parameterized by θ and C are the four solutions of:
|xA cos θ + yA sin θ + C| = rA
|xB cos θ + yB sin θ + C| = rB
Solving these equations for cosθ and sinθ isn't easy, but when we do, we get:
A = cosθ = ((xA-xB)(±rA±rB) + (yA-yB) sqrt((xA-xB)2+(yA-yB)2-(±rA±rB)2))/((xA-xB)2+(yA-yB)2)
B = sinθ = ((yA-yB)(±rA±rB) - (xA-xB) sqrt((xA-xB)2+(yA-yB)2-(±rA±rB)2))/((xA-xB)2+(yA-yB)2)
with the four solutions being the four possible combinations of ±rA±rB (allowing ±rA and ±rB to toggle independently). Each solution yields values of A=cosθ and B=sinθ for a tangent line Ax+By+C=0. When the radii have the same sign, the tangent is one of the pair of lines that cross between the circles. When they have opposite sign, the solution is for one of the outer tangents.
Part 2: finding C, which selects among many parallel lines the one line that's just the right distance from the centers of the two circles
To find C, since we know a point (h,k) on the line Ax+By+C=0, then C=-(Ah+Bk). Now, observe that
(xB±rB cosθ, yB±rB sinθ)
is a point on the tangent line, so
C = -(xBcosθ±rB cos2 θ + yBsinθ±rB sin2 θ), and simplifying cos2+sin2,
C = -(xBcosθ+yBsinθ±rB)
You may wonder why I used circle B to identify a point on the tangent line. It turns out that circle A can be used, but you need to travel the other direction along the identified radius. That is,
(xB±rB cos θ, yB±rB sin θ) and
(xA±-rA cos θ, yA±-rA sin θ)
are points on the same tangent line (for a given choice of signs of ±rA and ±rB). My choice of sign for cosθ and sinθ was arbitrary. Interchanging the roles of circle A and circle B results in assigning the opposite signs to cosθ, sinθ, and C.
Note regarding this solution: Although the derivation of solutions for cosθ and sinθ is not presented here, I'll show you how to verify the solution is correct. First square the expressions for cosθ and sinθ, add them together, and collect terms. You'll see it comes to one. That is, the numerator of this result factors as ((xA-xB)2+(yA-yB)2)2.
Next, you can evaluate |xA cosθ+yA sinθ+C| and |xB cosθ+yB sinθ+C| to verify they are indeed rA and rB, respectively. Using C=-(xBcosθ+yBsinθ±rB), the second equation verifies easily. To verify the first, you need to show
xA cosθ+yA sinθ-(xBcosθ+yBsinθ±rB)=±rA, i.e.
Again, it's rather tedious, but I've done it, and everything on the left hand side except ±rA±rB cancels, verifying the result.
www.mathhelpforum.com/math-help/calculus/26553-calculate-tangent-line-2-circles.html was very helpful in developing this page, especially the post by TwistedOne151, which suggested using cosθ and sinθ to parameterize the A and B coefficients of the equation of the line.
Find the points of intersection of two circles
Find the equation of a circle, given three points
Heron's Formula for the area of a triangle, which gives an imaginary "area" for impossible triangles.
The webmaster and author of this Math Help site is Graeme McRae.