Equation of a Sphere from 4 Points on the Surface

Written by Paul Bourke
June 2002

See also: Determinant of a square matrix.


Given 4 points in 3 dimensional space [ (x1,y1,z1) (x2,y2,z2) (x3,y3,z3) (x4,y4,z4) ] the equation of the sphere with those points on the surface is found by solving the following determinant.

x2 + y2 + z2 x y z 1
x12 + y12 + z12 x1 y1 z1 1
x22 + y22 + z22 x2 y2 z2 1
x32 + y32 + z32 x3 y3 z3 1
x42 + y42 + z42 x4 y4 z4 1
= 0

There are conditions on the 4 points, they are listed below and correspond to the determinant above being undefined (no solutions, multiple solutions, or infinite solutions).

If the determinant is found using the expansion by minors using the top row then the equation of the sphere can be written as follows.

(x2 + y2 + z2)
x1 y1 z1 1
x2 y2 z2 1
x3 y3 z3 1
x4 y4 z4 1
- x
x12 + y12 + z12 y1 z1 1
x22 + y22 + z22 y2 z2 1
x32 + y32 + z32 y3 z3 1
x42 + y42 + z42 y4 z4 1

+ y
x12 + y12 + z12 x1 z1 1
x22 + y22 + z22 x2 z2 1
x32 + y32 + z32 x3 z3 1
x42 + y42 + z42 x4 z4 1
- z
x12 + y12 + z12 x1 y1 1
x22 + y22 + z22 x2 y2 1
x32 + y32 + z32 x3 y3 1
x42 + y42 + z42 x4 y4 1
+
x12 + y12 + z12 x1 y1 z1
x22 + y22 + z22 x2 y2 z2
x32 + y32 + z32 x3 y3 z3
x42 + y42 + z42 x4 y4 z4
= 0

Or more simply in term of the minors M1j

(x2 + y2 + z2) M11 - x M12 + y M13 - z M14 + M15 = 0

The general equation of a sphere with radius r centered at (x0,y0,z0) is

(x - x0)2 + (y - y0)2 + (z - z0)2 = r2

Equating the terms from these two equations allows one to solve for the center and radius of the sphere, namely:

x0 = 0.5 M12 / M11

y0 = - 0.5 M13 / M11

z0 = 0.5 M14 / M11

r2 = x02 + y02 + z02 - M15 / M11

Note that these can't be solved for M11 equal to zero. This corresponds to no quadratic terms (x2, y2, z2) in which case we aren't dealing with a sphere and the points are either coplanar or three are colinear.

C source code

This piece of simple C code tests the solution as described above. It creates a known sphere (center and radius) and creates 4 random points on that sphere. It then proceeds to find the original center and radius using those four random points.