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.
|
= | 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) |
|
- x |
|
| + y |
|
- z |
|
+ |
|
= 0 |
Or more simply in term of the minors M1j
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 codeThis 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.