$$C$$ = Sphere center
$$r$$ = Sphere radius
$$P$$ = Point on the surface of the sphere

$$L_{0}$$ = Point on the line
$$\mathbf{v}$$ = Vector that defines the line direction
Parametric equation of a point on the sphere A generic point on the sphere satisfies the equation: $\left\lVert P - C \right\rVert = r$ Both sides of the equation can be raised by the power of 2, to simplify the calculations. $\left\lVert P - C \right\rVert^{2} = r^{2}$ Parametric equation of the line A generic line defined by a point $$L_{0}$$ and a direction vector $$\mathbf{v}$$. It describes all the points along the line as a function of the parameter $$t$$. $L_{\left( t \right)} = L_{0} + t \mathbf{v}$ Intersections calculation To calculate the intersection between sphere and line, substitute the generic point $$P$$ on the sphere with a generic point on the line $$L_{ \left( t \right) }$$. $\left\lVert L_{0} + t \mathbf{v} - C \right\rVert^{2} = r^{2}$ Define a vector $$\mathbf{w}$$ equal to the difference of $$L_{0}$$ and $$C$$: $\mathbf{w} = L_{0} - C$ and substitute back. $\left\lVert t \mathbf{v} + \mathbf{w} \right\rVert^{2} = r^{2}$ The length squared of a vector is equal to the dot product of the vector by itself. $\left( t \mathbf{v} + \mathbf{w} \right) \cdot \left( t \mathbf{v} + \mathbf{w} \right) = r^{2}$ Expand the dot product. $\left( \mathbf{v} \cdot \mathbf{v} \right) t^{2} + 2 \left( \mathbf{v} \cdot \mathbf{w} \right) t + \left( \mathbf{w} \cdot \mathbf{w} \right) = r^{2}$ Substitute the coefficients of $$t$$ to obtain a simpler quadratic equation. $\begin{cases} a = \left( \mathbf{v} \cdot \mathbf{v} \right) \\ b = 2 \left( \mathbf{v} \cdot \mathbf{w} \right) \\ c = \left( \mathbf{w} \cdot \mathbf{w} \right) - r^{2} \end{cases}$ Solve for $$t$$. $a t^{2} + b t + c = 0$ Intersection points Depending on the values of the discriminant of the quadratic equation in $$t$$ there can be two, one or no intersections:

• if $$\left( b^{2} - 4 a c \right) > 0$$
The distance between the sphere center $$C$$ and the line $$L_{ \left( t \right) }$$ is smaller than the sphere radius $$r$$. There are two intersections at: $L_{int_{1,2}} = L_{0} + t_{1,2} \mathbf{v} \qquad t_{1,2} = \frac{ - b \pm \sqrt{ b^{2} - 4 a c } }{ 2 a }$
• if $$\left( b^{2} - 4 a c \right) = 0$$
The distance between the sphere center $$C$$ and the line $$L_{ \left( t \right) }$$ is equal to the sphere radius $$r$$. The line $$L_{ \left( t \right) }$$ is tangent to the sphere. There is one intersection at: $L_{int} = L_{0} + t \mathbf{v} \qquad t = \frac{ -b }{ 2 a }$
• if $$\left( b^{2} - 4 a c \right) < 0$$
The distance between the sphere center $$C$$ and the line $$L_{ \left( t \right) }$$ is greater than the sphere radius $$r$$. There are no intersections.