# Calculate the intersection points of two Circles

How can you find the intersection points of two circles, given by the two center points and their radii? More formally: Given two circles with their center points \(\vec{A}\) and \(\vec{B}\) and their radii \(A_r\) and \(B_r\), we want to find the points \(\vec{P}_1\) and \(\vec{P}_2\) which represent the intersection points of both circles. Obviously, there can be one, two, infinity or no intersections at all, namely when one circle is completely within the other or the areas of the circles does not overlap. One way would be to formulate the two circles as their circle-formulas, subtract them and work out the x and y values. Quite simple, but needs a lot of calculations if you want to keep it fully parameterizable. Here is a different way:

## Case 1

The first case is the trivial case, when two identical circles have \(\vec{A}=\vec{B}\) and \(r_A=r_B\). Mathematically seen, there are infinity many intersection points. In practice you have to decide on how you work with this case.

## Case 2

In the second case we focus on circles which don't overlap at all. Detecting the collission of two circles is quite easy, given the center points and their radii, as the sum of them must be greater than the distance between their center points to overlap:

We define a distance measure between the two center points:

\[d = |\vec{B}-\vec{A}| = \sqrt{(B_x-A_x)^2 + (B_y-A_y)^2}\]

Using the distance, an intersection or collision between two circles occurs if and only if \(d \leq A_r+B_r\) in all other cases \(\vec{P}_1\) and \(\vec{P}_2\) are undefined.

## Case 3

The third case conserns the intersection of one or two points. We can ignore the case of meeting in one point, as it is a special case when \(\vec{P}_1=\vec{P}_2\).

When you look at the plot, you can see a rectangle which is stretched by \(y\) and \(x\) as well as bisected by the radius \(A_r\). Symmetrically, we can do the same for the second circle and express both as:

\[x^2+y^2=A_r^2\]

\[(d-x)^2+y^2=B_r^2\]

Re-arrange the first for \(y^2\) and applying it for the second equation yields

\[(d-x)^2+A_r^2-x^2=B_r^2\]

Solving this for \(x\):

\[x=\frac{A_r^2-B_r^2+d^2}{2d}\]

Using the first pythagorean equation, we can solve for \(y\) like so:

\[y_{1,2}=\pm\sqrt{A_r^2-x^2}\]

In order to transform the values back to actual points, we calculate the unit vectors \(\vec{e}_1\) and \(\vec{e}_2\), shown in the plot above and scale and translate it accordingly:

\[\vec{e}_1 = \frac{\vec{B}-\vec{A}}{|\vec{B}-\vec{A}|} = \frac{1}{d}\left(\begin{array}{c}B_x-A_x\\B_y-A_y\\\end{array}\right)\]

\[\vec{e}_2 = rot(90)\vec{e}_1 = \left(\begin{array}{cc}0&-1\\1&0\\\end{array}\right)\vec{e}_1\]

\[\vec{P}_{1,2}=\vec{A}+x \cdot \vec{e}_1\pm y\cdot\vec{e}_2\]

## Case 4

If one circle is inside the other, there is also no intersection. The easiest way to determine this case is checking if \(A_r < |x|\) or alternatively \(d < |B_r - A_r|\).

# Bringing it to life

```
d = hypot(B.x - A.x, B.y - A.y)
if (d <= A.r + B.r) {
x = (A.r * A.r - B.r * B.r + d * d) / (2 * d)
y = sqrt(A.r * A.r - x * x)
if (A.r < abs(x)) {
// No intersection, one circle is in the other
} else {
var ex = (B.x - A.x) / d;
var ey = (B.y - A.y) / d;
P1 = {
x: A.x + x * ex - y * ey,
y: A.y + x * ey + y * ex
}
P2 = {
x: A.x + x * ex + y * ey,
y: A.y + x * ey - y * ex
}
}
} else {
// No Intersection, far outside
}
```

You can simplify it even more, but you get the point. Here is a simple example to see it in action.

You might also be interested in the following