**[Update: 10-21-2012: The geometry discussed in this post has been superseded by the discussion about path planning for Dubin’s Cars: https://gieseanw.wordpress.com/2012/10/21/a-comprehensive-step-by-step-tutorial-to-computing-dubins-paths/]** I recently had a geometry problem I needed to solve involving finding tangent lines to two circles. If you’re like me, you don’t remember all the geometry you learned in school. And if you’re like me, you prefer searching the internet for answers versus turning to your books. I looked and looked online, but couldn’t really find a satisfying guide on finding the tangent points for two circles. Right now, even the Wikipedia page is a mess. How exactly does one “equate theta” and then “add x-y coordinates of a triangle” to a point? Perhaps in the future this will improve, but for now, here’s how I figured it out. I’m only going to run through finding one external tangent point. Figuring out the others as well as the tangent lines should become trivial afterwards.

This method will work for any two circles, even congruent ones (i.e. ones with the same radius).

## Setup

You’re given two circles, C1 and C2 we’ll call them. C1 has a radius larger than or equal to C2. You want to find the points along external tangent lines for the circles. I will not cover internal tangent lines in this article, but perhaps I’ll edit it in the future. External tangent lines, like the name implies “go outside” both circles. That is, both circles lie on the same side of the line. With internal tangent lines, the circles lie on opposite sides of the line.

## Find distance between centers

First things first, find the distance D between the centers of the two circles. This can be a simple Euclidean distance: if the center of C1 is (a,b) and the center of C2 is (c,d) then Euclidean distance D = **sqrt**((a-c)^{2} + (b-d)^{2}). Next, let’s get a look at what one of the external tangents looks like:

Shown in blue is X, the external tangent we care about. We also see the radii of the circles. Remember, we’re going to consider R1 (the radius of C1) to be larger (or at least as large) a R2.

Right now we don’t have enough information to determine X, or either of the two points it is tangent to. Fortunately, we have enough information to derive it! Some very clever mathematicians thought of the next trick:

- We can make a third circle, with center same as C1’s, but with radius R3 = R1 – R2

The above image shows C3 added, and how its radius is R1-R2. What’s interesting now is that the “rest” of R1 is now equivalent to R2! How can we use this? - Draw a tangent line from C3 to the center of C2. Just do it, trust me.

What’s this, did we just make a quadrilateral using two parallel tangent lines? Yup. I marked the line I just drew as “H” because, Heck, I couldn’t think of a better label. Note in the image above we also formed a triangle with sides equal to H, D, and (R1-R2). We don’t know H, but we *do* have enough information to calculate it, because H formed a right triangle. Using the Pythagorean theorem, H = **sqrt**(D^{2} – (R1-R2)^{2}).

Note that in congruent circles the (R1-R2) term drops out, leaving H = D.

Alright, now we *might* have enough information to calculate our tangent point on C1 * IF *our circles aren’t congruent. If R1 = R2, we can’t calculate it yet. Let’s just be safe and assume that we cannot. The problem I was trying to solve only had congruent circles, so I need to calculate it under that assumption anyway.

- Draw a new line from the tangent point on C1 to the center of C2.

In the image above I drew the line in green and labelled in Y, because Y not? Terrible math humor aside, I also marked an angle of interest, theta, which is what we’ve really been trying to calculate this entire time. If we can get theta, then we can get X and Y components of the radial line extending out to the tangent point on C1. What does that mean? We can add these X and Y components to the center of C1 to get the coordinates of the tangent point.

Okay, let’s look at the new line we drew, Y. If we knew Y, we could easily calculate theta using the law of cosines. Unfortunately we don’t know it just yet, but we have enough information to get it, so bear with me for one more calculation.

Notice that Y split our “polygon” into two triangles. The “above” triangle is a right triangle with sides H, Y, and R2. Using Pythagoras’ theorem again, we determine that Y = **sqrt**(H^{2} + R2^{2}).

Now we have Y. Let’s determine theta. Using the law of cosines, theta = **acos**( (R1^{2}+D^{2}-Y^{2}) / (2*R1*D) ).

If we had tried to compute theta before, then we’d see a (R1-R2) in the divisor instead of only R1. If R1= R2, our equation blows up.

Now that we have theta and R1, we can safely calculate Xt1, the external tangent point of interest. Assuming the line between the circles is parallel to the X axis, we can say that Xt1=(e,f) has values e=a+R1***cos**(theta), and f=b+R1***sin**(theta). Remember that the pair (a,b) represents the coordinates for the center of C1. In the general case, R1***cos**(theta) defines our X component and R1***sin**(theta) defines our y component. We don’t always simply add these components as is, in some cases we subtract them. It depends on the relative positioning of your circles and which tangent point on the circle you’re calculating.

However, we should assume that the circles are rotated about each other at least a little bit. Imagine if C2 was 4 units higher up the Y axis. To account for what I’ll call “rotation of the center line”, we can use the atan2 function, which gives amount of rotation about the X axis, perfect. Now theta=theta(from before) + **atan2**(d-b,c-a). The **atan2** function is a bit weird in that it takes Y coordinates first. The reason we gave it the values we did is because we wanted to define a line from C1’s center toward C2’s center, and the easiest way to do that is (c,d) – (a,b) which equates to (c-a, d-b). While this looks like a simple coordinate, it actually defines a line!

Now that you’ve calculated Xt1, and have the magnitude of the external tangent line, it should be much, much easier to calculate all the other external tangent points, as well as the tangent lines. I hope this helped. Feel free to correct any mistakes I undoubtedly made, or simplify this for me. Like I said, I was looking for a guide to help me figure this out myself and couldn’t find a good one. Thanks for reading.

Now what might one need these sorts of calculations for? Well, there is a certain class of car-like robot that may be called a Dubin’s Car. One can solve geometrically shortest paths between two configurations for a Dubin’s car by brute-force testing of all possibilities (There’s only 6!). Most of the trajectories consist of a straight line connecting two circles. Hence the motivation for the above geometry.

Haha, that’s true! I’m lazy the same way as you. I need to buy an e-book on trig & geometry.

I enjoyed talking to you this afternoon in Bryan. Titan is cool.

Hey! It was nice talking to you too. Hope you enjoyed the game, We’re gonna polish it for a bit before releasing it into the wild. -Andy

Awesome.