Circle Intersections

2025-09-27. Filed in public, twoville.

In two projects I've been working on, I need to find the points where two circles \(p\) and \(q\) intersect. Instead of immediately searching the web for an algorithm, I tried to work out the answer myself. Here's the information I had and the information I wanted to have:

My first approach was to write down the equations for the two circles:

$$\begin{align} (x - p_x)^2 + (y - p_y)^2 = p_r^2 \\ (x - q_x)^2 + (y - q_y)^2 = q_r^2 \\ \end{align}$$

After expanding out these equations and trying to combine them, I got overwhelmed by the amount of terms. I went looking for a different approach. Maybe I could do some trigonometric tricks with the centers and the intersections? I either knew or could calculate the distances between them:

I imposed some right triangles in there but struggled to find another side length.

At this point, I caved and searched the web. The first result simplified the circle equations by putting one circle at the origin and rotating the other to be immediately to its right. These new equations are much easier to combine and solve:

$$\begin{align} x^2 + y^2 = p_r^2 \\ (x - d)^2 + y^2 = q_r^2 \\ \end{align}$$

But I like trigonometric solutions better, so I went back to the triangle formulation. At this point, I recognized that I had three side lengths. Is that something the law of cosines can handle? For me, this law rusts quickly with disuse. It can handle it. In this particular situation, we know three sides but no angles. We get the cosine of the angle at either circle's center by solving this equation:

$$\cos A = \frac{b^2 + c^2 - a^2}{2bc}$$

Once we have the angle, we rotate a ray from the center in each direction to get the two intersection points. Here's a Twoville program that calculates these intersections on the fly as you manipulate the circles:

Sometimes the circles have the same radius, like this:

If that's the case, several terms cancel from the law of cosines:

$$\begin{align} \cos A &= \frac{r^2 + d^2 - r^2}{2rd} \\ &= \frac{d^2}{2rd} \\ &= \frac{d}{2r} \\ \end{align}$$

We get the same ratio if we split the isosceles triangle into a right triangle with hypotenuse \(r\) and an adjacent side of length \(\frac{d}{2}\).

While the rust is off, I think it's time to go remind myself of how the law of cosines is derived so that I can reinvent it next time.