# Calculating Line Intersection with Hypersphere Surface in $\mathbb{R}^n$?

Given an (infinitely long) line described by two distinct points it contains in $\mathbb{R}^n$

$$\alpha^{(1)} = (\alpha_1^{(1)}, \alpha_2^{(1)},\dots,\alpha_n^{(1)})$$ $$\alpha^{(2)} = (\alpha_1^{(2)}, \alpha_2^{(2)},\dots,\alpha_n^{(2)})$$

And a hypersphere (surface) described by a point and a radius:

$$\beta = (\beta_1, \beta_2,\dots,\beta_n)$$ $$r \in \mathbb{R}$$

Let $\gamma$ denote the (possibly empty) set of points that the line intersects the spheres surface.

Let $m = |\gamma|$

Clearly $m \in \{0,1,2\}$

By what formulae can we calculate $m$ and the locations of $\gamma$:

$$\gamma^{(1)} = (\gamma_1^{(1)}, \gamma_2^{(1)},\dots,\gamma_n^{(1)})$$ $$.$$ $$.$$ $$\gamma^{(m)} = (\gamma_1^{(m)}, \gamma_2^{(m)},\dots,\gamma_n^{(m)})$$

### partial solution maybe:

$$|x-\beta| = r$$

And parameterize the line:

$$x = \alpha^{(1)} + t(\alpha^{(2)} - \alpha^{(1)})$$

So:

$$|\alpha^{(1)} + t(\alpha^{(2)} - \alpha^{(1)}) -\beta| = r$$

and then?

• Why don't you go via a direct way and write $n-1$ equations on $x =(x_1,\dots,x_n)$ for a line and $1$ equation on $x$ for a sphere to find the solution? – Ilya May 29 '12 at 7:51
• The equation for the sphere is clear enough, $|x-\beta| = r$ - what are the equations for the lines? Presumably the system will be somehow linear mixed with quadratic? Isn't there someway I can use matricies and eigenvalues and all that jazz for this? – Andrew Tomazos May 29 '12 at 8:04
• I'm not aware of such methods, at least. Also, for the lines it may be easier to use $n$ parametrization equations, say $x_i = \alpha_1^{(1)}+(\alpha_1^{(2)}-\alpha_1^{(1)})t$ and solve a system of $n+1$ equations on $(x,t)$. – Ilya May 29 '12 at 8:11
• Ok, so we have $x = \alpha^{(1)} + t(\alpha^{(2)}-\alpha^{(1)})$ and $|x-\beta| = r$ and solve for $(x,t)$. – Andrew Tomazos May 29 '12 at 8:16
• Yes, I hope that would help. – Ilya May 29 '12 at 8:20

And now just substitute: $$\sum\limits_{i=1}^n(x_i - \beta_i)^2 = r^2 \quad\rightarrow\quad \sum\limits_{i=1}^n \left[\left(\alpha_i^{(2)}-\alpha_i^{(1)}\right)t+\left(\alpha_i^{(1)}-\beta_i\right)\right]^2 = r^2.$$ If we denote $\delta_i = \alpha_i^{(2)}-\alpha_i^{(1)}$ and $\gamma_i =\beta_i-\alpha_i^{(1)}$ we obtain: $$\sum\limits_{i=1}^n(\delta_i t -\gamma_i)^2 = r^2\Leftrightarrow \left(\sum\limits_{i=1}^n\delta_i^2\right)t^2-2\left(\sum\limits_{i=1}^n\delta_i\gamma_ i\right)t + \sum\limits_{i=1}^n\gamma_i^2-r^2 = 0.$$ Which you can compactly write as $$\|\alpha^{(2)} - \alpha^{(1)}\|^2 \cdot t^2-2\left\langle \alpha^{(2)} - \alpha^{(1)},\beta - \alpha^{(1)}\right\rangle \cdot t+ \|\beta - \alpha^{(1)}\|^2 = r^2.$$ This equation you know how to solve, and know how to determine if $m = 0$ or $1$ or $2$.
If you replace each of the variables $x_i$ in the equation $(x_1−β_1)^2+(x_2−β_2)^2+⋯+(x_n−β_n)^2=r^2$ by using $x_i=α^{(1)}_i+t(α^{(2)}_i−α^{(1)}_i)$, you will then get a quadratic equation in the single variable $t$.
The coefficients of $t^2$ , $t$ and the constant term would be some (moderately complicated) combination of the $\alpha$'s and $\beta$'s, but you could then just write down the condition for the quadratic to have 2, or 1 or 0 roots, depending as $b^2-4ac$ is positive, zero or negative.