Similarly to the article about intersection points of two circles we're now interested in the area that is formed by the intersection of two overlapping circles. We could formulate cases to step through the same as in the other article, but I will do it a little shorter this time.

We can see that when the distance measure \(d\) is zero, the intersection area is \(\pi r^2\) with \(r\) being the smaller radius of both circles. If \(d\) is greater than the sum of both radii, the area of intersection is zero.

## Calculate the actual overlapping

The idea is quite simple. When you look at the piece of cake which can be extracted on both circles, you have way too much of the area you want to calculate. But subtracting the triangle on each side gives you the area of the overlapping section.

We use the same equations as previously derived:

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

\[x=\frac{A^2_r-B^2_r+d^2}{2d}\]

\[y=\sqrt{A^2_r-x^2}\]

## Area of the circle sector

The goal is to calculate the area of the sector of the circle, which is a fraction of the whole. As a circle has 360°, the actual fraction is \(\frac{\theta_A}{2\pi}\)

Calculating \(\theta_A\) can be done either by the points \(\vec{P}_{1,2}\) we calculated already, or much simpler by using the sine of the half of the triangle and multiplying it by two:

\[\theta_A = 2\sin^{-1}\left(\frac{y}{A_r}\right)\]

The same can be applied for the second circle:

\[\theta_B = 2\sin^{-1}\left(\frac{y}{B_r}\right)\]

The area of the two circle sectors is then:

\[\text{area}_{s,1} = \frac{\theta_A}{2\pi} \pi A_r^2 = A_r^2 \sin^{-1}\left(\frac{y}{A_r}\right)\]

\[\text{area}_{s,2} = \frac{\theta_B}{2\pi} \pi B_r^2 = B_r^2 \sin^{-1}\left(\frac{y}{B_r}\right)\]

All we still need is the area of the two triangles. We have all information to do that:

\[\text{area}_{t,1} = \frac{1}{2}A_r^2 sin(\theta_A)\]

\[\text{area}_{t,2} = \frac{1}{2}B_r^2 sin(\theta_B)\]

or alternatively with Heron's formula:

\[\text{area}_{t,1} = y\sqrt{A_r^2-y^2}\]

\[\text{area}_{t,2} = y\sqrt{B_r^2-y^2}\]

The final area is

\[ \begin{array}{rl} \text{area} &= \text{area}_{s,1} + \text{area}_{s,2} - \text{area}_{t,1} - \text{area}_{t,2}\\ &= A_r^2 \sin^{-1}\left(\frac{y}{A_r}\right) + B_r^2 \sin^{-1}\left(\frac{y}{B_r}\right)-y\left(\sqrt{A_r^2-y^2}+\sqrt{B_r^2-y^2}\right)\\ &= A_r^2 \sin^{-1}\left(\frac{y}{A_r}\right) + B_r^2 \sin^{-1}\left(\frac{y}{B_r}\right)-y \left(x + \sqrt{B_r^2 - A_r^2+x^2}\right)\\ \end{array} \]

## Give me some code

All the math can be packed in a quite nice (unit tested) function

```
double area(A, B) {
d = hypot(B.x - A.x, B.y - A.y)
if (d < A.r + B.r) {
a = A.r * A.r
b = B.r * B.r
x = (a - b + d * d) / (2 * d)
z = x * x
y = sqrt(a - z)
if (d <= abs(B.r - A.r)) {
return PI * min(a, b)
}
return a * asin(y / A.r) + b * asin(y / B.r) - y * (x + sqrt(z + b - a))
}
return 0
}
```