Intersection of a Line and a Sphere (or circle)

Written by Paul Bourke
November 1992

C code example by author
Source code example by: Iebele Abel.
Sphere/ellipse and line intersection code for Visual Basic by Adrian DeAngelis.
LISP version for AutoCAD (and Intellicad) by Andrew Bennett intC2.lsp and intC2_app.lsp.


Points P (x,y) on a line defined by two points P1 (x1,y1,z1) and P2 (x2,y2,z2) is described by

P = P1 + u (P2 - P1)

or in each coordinate
x = x1 + u (x2 - x1)
y = y1 + u (y2 - y1)
z = z1 + u (z2 - z1)

A sphere centered at P3 (x3,y3,z3) with radius r is described by

(x - x3)2 + (y - y3)2 + (z - z3)2 = r2

Substituting the equation of the line into the sphere gives a quadratic equation of the form

a u2 + b u + c = 0

where:

a = (x2 - x1)2 + (y2 - y1)2 + (z2 - z1)2

b = 2[ (x2 - x1) (x1 - x3) + (y2 - y1) (y1 - y3) + (z2 - z1) (z1 - z3) ]

c = x32 + y32 + z32 + x12 + y12 + z12 - 2[x3 x1 + y3 y1 + z3 z1] - r2

The solutions to this quadratic are described by

The exact behaviour is determined by the expression within the square root

b * b - 4 * a * c

To apply this to two dimensions, that is, the intersection of a line and a circle simply remove the z component from the above mathematics.

Line Segment

When dealing with a line segment it may be more efficient to first determine whether the line actually intersects the sphere or circle. This is achieved by noting that the closest point on the line through P1P2 to the point P3 is along a perpendicular from P3 to the line. In other words if P is the closest point on the line then

(P3 - P) dot (P2 - P1) = 0

Substituting the equation of the line into this

[P3 - P1 - u(P2 - P1)] dot (P2 - P1) = 0

Solving the above for u =

(x3 - x1)(x2 - x1) + (y3 - y1)(y2 - y1) + (z3 - z1)(z2 - z1)
-----------------------------------------------------------
(x2 - x1)(x2 - x1) + (y2 - y1)(y2 - y1) + (z2 - z1)(z2 - z1)

If u is not between 0 and 1 then the closest point is not between P1 and P2

Given u, the intersection point can be found, it must also be less than the radius r. If these two tests succeed then the earlier calculation of the actual intersection point can be applied.