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?
 A: 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$.
A: 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.
(I think Ilya has just said almost the same thing while I was working out how to format my first post here!)
