# How to compute the volume of intersection between two hyperspheres

Let's say I have two n-spheres and I've no prior knowledge about the spheres (such as one of the sphere might be inside the other one) and I need to compute the volume of the intersection of the two hyper-spheres. Is there an efficient and generic way to compute the hyper-spherical cap of the intersection of these n-spheres? Note that the hyperspheres are expected to be very high dimensional such as 4096.

• You'll need beta functions apparently.
– anon
Jun 24 '12 at 3:43

Let's suppose you are given the coordinates of the center and radius of each sphere. Then you can compute the distance between their centers.

The volume of the intersection depends only on this distance $d$ and the two radii $r_1$, $r_2$.

If $d \ge r_1+r_2$ then the sphere interiors do not intersect and the volume is zero.

If $d \le |r_1-r_2|$, then one sphere is inside the other and the volume is that of the smaller sphere.

Otherwise, the volume consists of two spherical caps, glued at a hyperplane.
The cap sizes can be found by considering the triangle with sides $r_1$, $r_2$, $d$, which appears on any planar cross-section including the line through the centers. The altitude to side $d$ is where the two caps are glued. The cap base heights are at the (signed) distances from the endpoints of side $d$ to this altitude: $$c_1 = \frac{d^2+r_1^2-r_2^2}{2d}$$ $$c_2 = \frac{d^2-r_1^2+r_2^2}{2d}$$

Now we just need to compute the cap volumes. EDIT: This cannot be done using elementary functions. The volume of a spherical cap can be expressed in terms of the gamma function $\Gamma$ and the regularized incomplete beta function $I$. For a clean derivation, see Concise Formulas for the Area and Volume of a Hyperspherical Cap, Shengqiao Li, Asian Journal of Mathematics and Statistics 4(1), 66-70, 2011.

$$V^{cap}_n(r,a \ge 0) = \frac{1}{2} \frac{\pi^{n/2}}{\Gamma(\frac{n}{2}+1)}r^n I_{1-a^2/r^2}(\frac{n+1}{2},\frac{1}{2})$$ $$V^{cap}_n(r,a < 0) = \frac{\pi^{n/2}}{\Gamma(\frac{n}{2}+1)}r^n - V^{cap}_n(r,-a)$$

The final answer is then $V^{cap}_n(r_1,c_1)+V^{cap}_n(r_2,c_2)$.

• thanks very clean explanation. Jun 26 '12 at 1:03
• I know that this is an old answer but just for the sake of trying to improve it. I agree with @kirilsolo. The idea of this procedure is right, nevertheless the last integrand is mitaken. This is beacause the radius of the $(n-1)$-dimensional ball that we want to integrate is not x but rather a function of x, namely $(\sqrt{r_1^2-x^2})$ for the first ball and $(\sqrt{r_2^2-(x-d)^2})$. Nov 6 '13 at 14:55
• for the second one. The integral for the first ball then becomes $\int_a^rV_{n-1}((\sqrt{r_1^2-x^2})) dx$. Which can be expressed in some horrible formula using the hypergeometric function. Nov 6 '13 at 15:02
• @Bruce Thank you for your comment, I have fixed the formula.
– Matt
Nov 14 '13 at 17:43
• @Solomonoff Yes, it does. This is why you need to use signed distances (i.e. $c_1$ or $c_2$ can be negative) and why you need the extra formula for $V_n^{cap}(r,a)$ covering the case where the "cap" is more than half the sphere (i.e. the case $a<0$).
– Matt
Nov 22 '16 at 0:53

It seems to me that the answer written above by Matt is incorrect. Specifically, the expression that is believed to be the exact value of $V_n^{cap}(r,a)$ is in fact only an underestimation which gets farther from the correct value as $n$ is increased. Formally the radius of $V_n^{cap}$ for a specific $x$ should be $$\sqrt{r^2-(r-x)^2}=\sqrt{2rx-x^2},$$ instead of just $x$.

Thus, the integral for this expression should look as follows $$V_n^{cap}(r,a)=\int_a^r V_{n-1}\left(\sqrt{2rx-x^2}\right)dx,$$ although I'm not sure how it can integrated.

• Thank you for pointing this out (although I unfortunately didn't see your remarks until the system notified me of Bruce Wayne's comment on my answer). I have fixed the solution, and included a link to a paper that shows how to do the required integration.
– Matt
Nov 14 '13 at 17:45

Following the idea of using caps, The Wikipedia article has a mention of a nice asymptotic result: If $h$ is the height of the cap and $r$ is the radius of the hypersphere, then $V^{cap}_n\rightarrow V_n(1-F((1-h/r)\sqrt{n}))$ for large $n$, where $F$ is the integral of the standard gaussian.

In our case $h=\frac{2r-d}{2}$ and we are interested in the intersection which is $2V_n^{cap}$, so we get $2V^{cap}_n\approx V_n\,2(1-F(\frac{d}{2r}\sqrt{n}))$.

This shows that the intersection, even normalized by $V_n$ goes to $0$ as the dimension gets large. Pretty cool.