Line–sphere intersection

[[File:Line-Sphere Intersection Cropped.png|thumb|350px|right|The three possible line-sphere intersections:

1. No intersection.

2. Point intersection.

3. Two point intersection.]]

In analytic geometry, a line and a sphere can intersect in three ways:


 * 1) No intersection at all
 * 2) Intersection in exactly one point
 * 3) Intersection in two points.

Methods for distinguishing these cases, and determining the coordinates for the points in the latter cases, are useful in a number of circumstances. For example, it is a common calculation to perform during ray tracing.

Calculation using vectors in 3D
In vector notation, the equations are as follows:

Equation for a sphere
 * $$\left\Vert \mathbf{x} - \mathbf{c} \right\Vert^2=r^2$$
 * $$\mathbf{x}$$ : points on the sphere
 * $$\mathbf{c}$$ : center point
 * $$r$$ : radius of the sphere

Equation for a line starting at $$\mathbf{o}$$
 * $$\mathbf{x}=\mathbf{o} + d\mathbf{u}$$
 * $$\mathbf{x}$$ : points on the line
 * $$\mathbf{o}$$ : origin of the line
 * $$d$$ : distance from the origin of the line
 * $$\mathbf{u}$$ : direction of line (a non-zero vector)

Searching for points that are on the line and on the sphere means combining the equations and solving for $$d$$, involving the dot product of vectors:


 * Equations combined
 * $$\left\Vert \mathbf{o} + d\mathbf{u} - \mathbf{c} \right\Vert^2=r^2 \Leftrightarrow (\mathbf{o} + d\mathbf{u} - \mathbf{c}) \cdot (\mathbf{o} + d\mathbf{u} - \mathbf{c}) = r^2$$
 * Expanded and rearranged:
 * $$d^2(\mathbf{u}\cdot\mathbf{u})+2d[\mathbf{u}\cdot(\mathbf{o}-\mathbf{c})]+(\mathbf{o}-\mathbf{c})\cdot(\mathbf{o}-\mathbf{c})-r^2=0$$
 * The form of a quadratic formula is now observable. (This quadratic equation is an instance of Joachimsthal's equation. )
 * $$a d^2 + b d + c = 0$$
 * where
 * $$a=\mathbf{u}\cdot\mathbf{u}=\left\Vert\mathbf{u}\right\Vert^2$$
 * $$b=2[\mathbf{u}\cdot(\mathbf{o}-\mathbf{c})]$$
 * $$c=(\mathbf{o}-\mathbf{c})\cdot(\mathbf{o}-\mathbf{c})-r^2=\left\Vert\mathbf{o}-\mathbf{c}\right\Vert^2-r^2$$
 * Simplified

d = \frac{-2[\mathbf{u}\cdot(\mathbf{o}-\mathbf{c})] \pm \sqrt{(2[\mathbf{u}\cdot(\mathbf{o}-\mathbf{c})])^2-4\left\Vert\mathbf{u}\right\Vert^2(\left\Vert\mathbf{o}-\mathbf{c}\right\Vert^2-r^2)}}{2 \left\Vert\mathbf{u}\right\Vert^2 }$$
 * Note that in the specific case where $$\mathbf{u}$$ is a unit vector, and thus $$\left\Vert\mathbf{u}\right\Vert^2=1$$, we can simplify this further to (writing $$\hat{\mathbf{u}}$$ instead of $$\mathbf{u}$$ to indicate a unit vector):
 * $$\nabla=[\hat{\mathbf{u}}\cdot(\mathbf{o}-\mathbf{c})]^2-(\left\Vert\mathbf{o}-\mathbf{c}\right\Vert^2-r^2)$$
 * $$d=-[\hat{\mathbf{u}}\cdot(\mathbf{o}-\mathbf{c})] \pm \sqrt{\nabla}$$


 * If $$\nabla < 0$$, then it is clear that no solutions exist, i.e. the line does not intersect the sphere (case 1).
 * If $$\nabla = 0$$, then exactly one solution exists, i.e. the line just touches the sphere in one point (case 2).
 * If $$\nabla > 0$$, two solutions exist, and thus the line touches the sphere in two points (case 3).