Line–sphere intersection

From Wikipedia, the free encyclopedia

The three possible line-sphere intersections: 1. No intersection. 2. Point intersection. 3. Two point intersection.
The three possible line-sphere intersections:
1. No intersection.
2. Point intersection.
3. Two point intersection.

In analytic geometry, the intersection of a line and a sphere can be the empty set, a point, or two points. Distinguishing these cases, and determining equations for the points in the latter cases have use, for example, when calculating ray intersections in ray tracing.

Equation for a sphere

(x-s_x)^2+(y-s_y)^2+(z-s_z)^2=s_r^2
  • s - center point and radius

Equation for a line starting at (0,0,0)

x = d * lx
y = d * ly
z = d * lz
  • d - distance along line from starting point
  • l - direction of line

Solving for d

Equations combined
(d*l_x-s_x)^2+(d*l_y-s_y)^2+(d*l_z-s_z)^2=s_r^2
Expanded
d^2l_x^2-2dl_xs_x+s_x^2+d^2l_y^2-2dl_ys_y+s_y^2+d^2l_z^2-2dl_zs_z+s_z^2=s_r^2
Factored
d^2(l_x^2+l_y^2+l_z^2)+d(-2l_xs_x-2l_ys_y-2l_zs_z)+(s_x^2+s_y^2+s_z^2-s_r^2)=0
Quadratic formula and simplified
d=\frac{l_xs_x+l_ys_y+l_zs_z \pm \sqrt {(l_xs_x+l_ys_y+l_zs_z)^2-(l_x^2+l_y^2+l_z^2)(s_x^2+s_y^2+s_z^2-s_r^2)}}{l_x^2+l_y^2+l_z^2}


[edit] See also