 $$C$$ = Center of the ellipsoid in world space
$$\mathbf{a}$$ = First ellipsoid axis vector (along local x axis)
$$\mathbf{b}$$ = Second ellipsoid axis vector (along local y axis)
$$\mathbf{c}$$ = Third ellipsoid axis vector (along local z axis)

$$L_{0}$$ = Point on the line in world space
$$\mathbf{v}$$ = Vector that defines the line direction in world space

$$P$$ = Point on the surface of a unit radius sphere centered in the origin
Transform matrix from unit radius sphere centered in the origin to non uniformly scaled sphere A simpler way to calculate the intersection of a line and a sphere arbitrarily scaled in space is to build the matrix that transforms from the scaled sphere to a unit radius sphere centered in the origin. Apply the transformation to the line, calculate the intersection with a unit radius sphere, then apply the results to the original line and obtain the intersection points if any are found.

The transform matrix is the result of the composition of the matrices that describe translation, rotation and scale of the scaled sphere. Its inverse maps the non uniform scaled space to the uniform one. $\mathbf{T} = \begin{bmatrix} 1 & 0 & 0 & C_{x} \\ 0 & 1 & 0 & C_{y} \\ 0 & 0 & 1 & C_{z} \\ 0 & 0 & 0 & 1 \end{bmatrix} \qquad \mathbf{R} = \begin{bmatrix} \hat{a}_{x} & \hat{b}_{x} & \hat{c}_{x} & 0 \\ \hat{a}_{y} & \hat{b}_{y} & \hat{c}_{y} & 0 \\ \hat{a}_{z} & \hat{b}_{z} & \hat{c}_{z} & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \qquad \mathbf{S} = \begin{bmatrix} \left\lVert \mathbf{a} \right\rVert & 0 & 0 & 0 \\ 0 & \left\lVert \mathbf{b} \right\rVert & 0 & 0 \\ 0 & 0 & \left\lVert \mathbf{c} \right\rVert & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}$ $\mathbf{M} = \mathbf{TRS}$ Point on unit radius sphere centered in the origin 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} = \left( r' \right)^{2}$ The rescaled sphere center $$C'$$ is in the origin and its radius is 1: $C' = \mathbf{M}^{-1} C = \begin{bmatrix} 0 \\ 0 \\ 0 \\ 1 \end{bmatrix} \qquad r = 1$ Parametric 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}$ To perform the intersection with the unit radius sphere centered in the origin, the point $$L_{0}$$ and the vector $$\mathbf{v}$$ are transformed by the matrix $$\mathbf{M}^{-1}$$. $L'_{\left( t \right)} = \mathbf{M}^{-1} L_{0} + \mathbf{M}^{-1} t \mathbf{v}$ $L'_{0} = \mathbf{M}^{-1} L_{0} \qquad \mathbf{v}' = \mathbf{M}^{-1} \mathbf{v}$ $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} = 1$ 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} = 1$ 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) = 1$ 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) = 1$ 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) - 1 \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 1. 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 1. 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 1. There are no intersections.