Overlapping circles


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

Geometry of two overlapping circles

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

$$ 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.