Learning Scientific Programming with Python (2nd edition)

P8.X.2: Overlapping circles

Question P8.X.2

Consider two circles of radii $R$ and $r$ whose centres are separated by a distance $d$.

The area of overlap between them (assuming $|R-r| \le d \le R+r$) is given by the following formula, derived below.

$$ A(d; R, r) = \beta R^2 + \alpha r^2 - \frac{1}{2}r^2\sin 2\alpha - \frac{1}{2}R^2\sin 2\beta, $$ where $$ \cos \alpha = \frac{r^2 + d^2 - R^2}{2rd} \quad \mathrm{and} \quad \cos \beta = \frac{R^2 + d^2 - r^2}{2Rd}. $$

Inverting this formula to find $d$ for a given overlap area $A=A_0$ cannot be achieved analytically, but a numerical solution can be found as the root of the function $A(d; R, r) - A_0$.

Write a Python function which takes arguments A, the target overlap area, and R and r the two circle radii and returns d, the distance between the circle centres giving overlap area A. Use, for example, brentq.

Derivation

First note that $A=0$ if $d \ge R+r$: the circles do not intersect at all in this case. Also, $A = \pi (\mathrm{min}(R,r))^2$ if $d \le |R-r|$: the smaller circle is entirely enclosed in the larger in this case. For the case of partial overlap, $|R-r| < d < R+r$ the area to find is shaded in the diagram below.

Geometry of ovelapping circles highlighting their area of intersection

Geometry of ovelapping circles highlighting their area of intersection.

The cosine formula gives the angles $\alpha$ and $\beta$: $$ \cos \alpha = \frac{r^2 + d^2 - R^2}{2rd} \quad \mathrm{and} \quad \cos \beta = \frac{R^2 + d^2 - r^2}{2Rd}. $$ The required area may be found as the sum of the two circle segments cut off by the chord CD. For the circle centred at A in the diagram above, its segment is the area of the circular sector ACD minus the area of the triangle ACD:

With $x=\mathrm{AE} = R\cos\beta$ and $h=\mathrm{CE}=R\sin\beta$, triangle ACD has area $xh=R^2\cos\beta\sin\beta=\frac{1}{2}R^2\sin2\beta$. The area of the circular sector ACD is simply $\beta R^2$ with $\beta$ measured in radians (the entire circle has area $\pi R^2$ and we want the fraction $\beta/2\pi$ of it).

Therefore, the shaded circular segment has area $\beta R^2 - \frac{1}{2}R^2\sin2\beta$. Similarly, the area of the segment of the circle centred at B cut off by chord CD is $\alpha r^2 - \frac{1}{2}r^2\sin2\alpha$. The total intersection area is therefore $$ A = \alpha r^2 + \beta R^2 - \frac{1}{2}r^2\sin2\alpha - \frac{1}{2}R^2\sin2\beta. $$