---
title: "Test if a point is inside a tetrahedron in C#"
url: https://recursiveai.org/onemath/cookbook/test-point-inside-tetrahedron/
description: "Use exact orientation signs to test whether a point lies inside a tetrahedron in C#, including faces, edges, vertices, and zero-volume input."
version: "1.0.0"
---

# Test if a point is inside a tetrahedron in C#

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

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneMath) · [GitHub](https://github.com/recursive-ai-oss/onemath-csharp) · [Docs](https://recursiveai.org/onemath/docs/) · [Download example (.zip)](https://recursiveai.org/onemath/cookbook/test-point-inside-tetrahedron/test-point-inside-tetrahedron.zip)

Compute the four face signs exactly to test whether a point is inside. There’s no epsilon to tune and no rounding error in the signs, even on a face, edge, or vertex.

```shell
dotnet add package RecursiveAI.OneMath
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneMath;
using RecursiveAI.OneMath.Predicates;

Vector3D a = new(0, 0, 0), b = new(1, 0, 0);
Vector3D c = new(0, 1, 0), d = new(0, 0, 1);
var p = new Vector3D(0.25, 0.25, 0.25);
Console.WriteLine(Contains(a, b, c, d, p));

static bool Contains(Vector3D a, Vector3D b,
    Vector3D c, Vector3D d, Vector3D p)
{
    int sign = Side(a, b, c, d);
    if (sign == 0)
        throw new ArgumentException(
            "Zero-volume tetrahedron.");

    return Side(p, b, c, d) * sign >= 0
        && Side(a, p, c, d) * sign >= 0
        && Side(a, b, p, d) * sign >= 0
        && Side(a, b, c, p) * sign >= 0;
}

static int Side(Vector3D a, Vector3D b,
    Vector3D c, Vector3D d) =>
    ExactPredicates.Orient3D(a.X, a.Y, a.Z,
        b.X, b.Y, b.Z, c.X, c.Y, c.Z,
        d.X, d.Y, d.Z);
```

Unzip [the example](https://recursiveai.org/onemath/cookbook/test-point-inside-tetrahedron/test-point-inside-tetrahedron.zip), then `dotnet run`.

The snippet prints `True` for the point `(0.25, 0.25, 0.25)`.

```text
True
```

## The full program

The download tests the same solid with a point on a face, on an edge, on a vertex, and outside. It also swaps two vertices and tries a flat set of four vertices.

```csharp
using RecursiveAI.OneMath;
using RecursiveAI.OneMath.Predicates;

Vector3D a = new(0, 0, 0), b = new(1, 0, 0);
Vector3D c = new(0, 1, 0), d = new(0, 0, 1);
var p = new Vector3D(0.25, 0.25, 0.25);
Console.WriteLine(Contains(a, b, c, d, p));

static bool Contains(Vector3D a, Vector3D b,
    Vector3D c, Vector3D d, Vector3D p)
{
    int sign = Side(a, b, c, d);
    if (sign == 0)
        throw new ArgumentException(
            "Zero-volume tetrahedron.");

    return Side(p, b, c, d) * sign >= 0
        && Side(a, p, c, d) * sign >= 0
        && Side(a, b, p, d) * sign >= 0
        && Side(a, b, c, p) * sign >= 0;
}

static int Side(Vector3D a, Vector3D b,
    Vector3D c, Vector3D d) =>
    ExactPredicates.Orient3D(a.X, a.Y, a.Z,
        b.X, b.Y, b.Z, c.X, c.Y, c.Z,
        d.X, d.Y, d.Z);

Console.WriteLine($"Face: {Contains(a, b, c, d, new(0.25, 0.25, 0))}");
Console.WriteLine($"Edge: {Contains(a, b, c, d, new(0.5, 0, 0))}");
Console.WriteLine($"Vertex: {Contains(a, b, c, d, a)}");
Console.WriteLine($"Outside: {Contains(a, b, c, d, new(0.5, 0.5, 0.5))}");
Console.WriteLine($"Swapped: {Contains(b, a, c, d, p)}");
try
{
    Contains(a, b, c, new(1, 1, 0), p);
}
catch (ArgumentException error)
{
    Console.WriteLine(error.Message);
}
```

`dotnet run` prints:

```text
True
Face: True
Edge: True
Vertex: True
Outside: False
Swapped: True
Zero-volume tetrahedron.
```

The helper accepts both vertex orders and includes the boundary. If the four vertices are coplanar, `Contains` throws `ArgumentException`: the input has no 3D interior.

## Variations

**Check boundary points and vertex order.** Append these calls to the snippet at the top:

```csharp
Console.WriteLine($"Face: {Contains(a, b, c, d, new(0.25, 0.25, 0))}");
Console.WriteLine($"Edge: {Contains(a, b, c, d, new(0.5, 0, 0))}");
Console.WriteLine($"Vertex: {Contains(a, b, c, d, a)}");
Console.WriteLine($"Outside: {Contains(a, b, c, d, new(0.5, 0.5, 0.5))}");
Console.WriteLine($"Swapped: {Contains(b, a, c, d, p)}");
try
{
    Contains(a, b, c, new(1, 1, 0), p);
}
catch (ArgumentException error)
{
    Console.WriteLine(error.Message);
}
```

```text
Face: True
Edge: True
Vertex: True
Outside: False
Swapped: True
Zero-volume tetrahedron.
```

## Related recipes

- [Test whether a point is inside a triangle](https://recursiveai.org/onemath/cookbook/test-point-in-triangle-robustly/)
- [Use exact geometric predicates](https://recursiveai.org/onemath/cookbook/exact-predicates/)

## The short answer

Call `ExactPredicates.Orient3D` on the four vertices to get the reference orientation. If it’s zero, stop: the tetrahedron has zero volume. Otherwise replace each vertex in turn with the point, keeping the other three in their original slots. The point is inside or on the surface if every replacement sign is zero or agrees with the reference sign. Checking against the reference makes the test work for either vertex order.

## How do you tell whether a point is inside or on a tetrahedron?

For each triangular face, the fourth vertex tells you which side of that face’s plane is inside. Replacing that vertex with the point tests whether the point is on the same side. Keeping the point in the replaced vertex’s slot keeps the orientation consistent across all four tests. The method is the [five-determinant point-in-tetrahedron test](https://steve.hollasch.net/cgindex/geometry/ptintet.html), but you only need the signs, not the determinant values or their ratios.

All four signs must agree with the reference or be zero. A zero sign by itself only puts the point on the plane of one face; the point could be well outside the actual triangle. The other faces rule that out. For strictly interior points, change all four `>= 0` comparisons to `> 0`. A point on a face, edge, or vertex will then fail at least one comparison.

## What if the tetrahedron has zero volume or is nearly flat?

Coplanar vertices, including repeated vertices, produce a reference sign of zero. Handle that before comparing the face signs; otherwise a multiplication by zero would make every comparison pass. This helper rejects that input rather than treating it as a flat triangle or segment. A nearly flat tetrahedron with nonzero volume still has an inside, and the same sign test applies.

[OneMath’s predicate docs](https://recursiveai.org/onemath/docs/predicates/#input-requirements-and-range) guarantee exact signs when every coordinate is finite and either zero or has magnitude from `2^-140` to `2^200`. The sign is for the doubles supplied to the call. If earlier arithmetic rounded two vertices to the same coordinates, the predicate can detect the resulting degeneracy but can’t recover the original geometry.
