Robert Eisele
Engineer, Systems Architect and DBA

Calculate the intersection area of two circles

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
}

You might also be interested in the following

1 Comment on „Calculate the intersection area of two circles”

R
R

I think that this will not work if the centroid B is to the left of the chord line.

In particular, notice that asin can only give you values in the range [-π/2..π/2].

Consequently, when looking at

θA = 2 * (asin (y / A.r))
θB = 2 * (asin (y / B.r))

θA and θB can only be in the ranges [0..π] since none of the values can be negative. But the angle needs to be greater than π if B is on the same side of the chord as A.

 

Sorry, comments are closed for this article. Contact me if you want to leave a note.