Recursive AI

Test a point against a circumsphere in C#

OneMath is a free, open-source deterministic math library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

A tetrahedron’s circumsphere passes through its four vertices.

Terminal
dotnet add package RecursiveAI.OneMath

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneMath.Predicates;
Console.WriteLine(ExactPredicates.InSphere(
    0, 0, 0,  0, 4, 0,  // a, b
    4, 0, 0,  0, 0, 4,  // c, d
    2, 2, 2));          // query point

Unzip the example, then dotnet run.

Prints:

Text
1

For this vertex order, 1 = inside, -1 = outside, and 0 = on the sphere.

The full program

C#
using RecursiveAI.OneMath.Predicates;
 
// XYZ vertices. This order gives positive orientation.
double[] a = [0, 0, 0], b = [0, 4, 0], c = [4, 0, 0], d = [0, 0, 4];
Console.WriteLine($"Center: {Classify(a, b, c, d, [2, 2, 2])}");
Console.WriteLine($"On sphere: {Classify(a, b, c, d, [4, 4, 4])}");
Console.WriteLine($"Beyond sphere: {Classify(a, b, c, d, [5, 4, 4])}");
Console.WriteLine($"Swapped vertices: {Classify(a, c, b, d, [2, 2, 2])}");
Console.WriteLine($"Coplanar vertices: {Classify(a, b, c, [4, 4, 0], [2, 2, 2])}");
 
static string Classify(double[] a, double[] b, double[] c, double[] d, double[] point)
{
    int orientation = ExactPredicates.Orient3D(
        a[0], a[1], a[2], b[0], b[1], b[2], c[0], c[1], c[2], d[0], d[1], d[2]);
    if (orientation == 0) return "no unique sphere";
 
    int sign = ExactPredicates.InSphere(
        a[0], a[1], a[2], b[0], b[1], b[2], c[0], c[1], c[2], d[0], d[1], d[2],
        point[0], point[1], point[2]);
    return (orientation * sign) switch
    {
        > 0 => "inside",
        < 0 => "outside",
        _ => "on the sphere",
    };
}
Text
Center: inside
On sphere: on the sphere
Beyond sphere: outside
Swapped vertices: inside
Coplanar vertices: no unique sphere

The full program uses Orient3D to make those labels independent of vertex order.

Coplanar vertices don’t determine a unique sphere; the helper returns no unique sphere. Nonfinite coordinates throw ArgumentOutOfRangeException.

Variations

Check a point one representable step across the boundary. In the full program, add this at the end of the Console.WriteLine calls:

C#
Console.WriteLine(Classify(a, b, c, d,
    [Math.BitDecrement(4), 4, 4]));
Console.WriteLine(Classify(a, b, c, d,
    [Math.BitIncrement(4), 4, 4]));

The added lines print inside and outside. Exactly (4, 4, 4) is on the sphere.

The short answer

Call ExactPredicates.InSphere with 15 doubles: the X, Y, and Z of a, then of b, c, d, and finally of the query point e. Multiply the result by Orient3D(a, b, c, d): a positive sign means inside, a negative sign means outside, and zero means on the sphere. Check for zero orientation first. The in-sphere determinant has no sphere interpretation when the vertices are coplanar.

What is the circumsphere of a tetrahedron?

The circumsphere is the sphere through the tetrahedron’s vertices. It’s not the sphere that fits inside and touches its faces. With the vertices in this example, the center is (2, 2, 2) because its squared distance to each vertex is 12. You don’t need to compute that center or the radius to classify a point: InSphere uses the sign of a determinant formed from the vertices relative to the query point and their squared distances from it.

Switching two vertices swaps two rows in that determinant. It changes the raw sign without changing the sphere. Orient3D changes sign too, so their product leaves the classification unchanged. For many query points against one tetrahedron, compute and check its orientation only once.

How do you tell if the point is inside the tetrahedron itself?

That’s a different test. The example’s center (2, 2, 2) lies outside the tetrahedron: the face through (4, 0, 0), (0, 4, 0), and (0, 0, 4) has x + y + z = 4, the origin is on the lower side, and the center’s coordinates sum to 6. Yet the center is inside the circumsphere. A point-in-tetrahedron test compares the point and the opposite vertex against each of the four face planes; they must be on the same side, or the point must lie on the plane if you include boundary points. Use InSphere when the sphere is the boundary you care about, not the faces.

How do you classify a point close to the sphere?

A center-and-radius calculation rounds intermediate values, which can change a near-boundary comparison. OneMath first checks whether the computed determinant’s sign is reliable. If it’s too close to zero to decide, it evaluates it with exact arithmetic. A zero sign means the input coordinates, as represented by their doubles, place the point on the sphere. It doesn’t mean “within an epsilon.”

The predicate reference guarantees exactness for finite coordinates that are zero or have magnitude from 2⁻¹⁴⁰ to 2²⁰⁰ inclusive. Finite inputs outside that range can throw if exact evaluation is needed. The predicate also can’t recover precision lost before the call, for example when a decimal coordinate was rounded to double.

All onemath recipes