Recursive AI

Robust point-in-triangle test in C#

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

Exact orientation keeps rounding from misclassifying points on or near an edge. Coordinates must be finite: zero or magnitude between 2^-140 and 2^200.

Terminal
dotnet add package RecursiveAI.OneMath

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

C#
using RecursiveAI.OneMath.Predicates;
// Edge A(0,0) -> B(4,0); point P(1,1).
int ab = ExactPredicates.Orient2D(0,0,4,0,1,1);
int bc = ExactPredicates.Orient2D(4,0,0,4,1,1);
int ca = ExactPredicates.Orient2D(0,4,0,0,1,1);
Console.WriteLine(ab>=0 && bc>=0 && ca>=0);

Prints:

Text
True

Unzip the example, then dotnet run.

The full program

C#
using RecursiveAI.OneMath.Predicates;
 
Console.WriteLine($"Inside: {Contains(1, 1, 0, 0, 4, 0, 0, 4)}");
Console.WriteLine($"Clockwise: {Contains(1, 1, 0, 0, 0, 4, 4, 0)}");
Console.WriteLine($"Edge: {Contains(2, 2, 0, 0, 4, 0, 0, 4)}");
Console.WriteLine($"Vertex: {Contains(0, 0, 0, 0, 4, 0, 0, 4)}");
Console.WriteLine($"Outside: {Contains(3, 3, 0, 0, 4, 0, 0, 4)}");
Console.WriteLine($"Degenerate: {Contains(1, 0, 0, 0, 2, 0, 4, 0)}");
 
static bool Contains(double px, double py,
    double ax, double ay, double bx, double by, double cx, double cy)
{
    int winding = ExactPredicates.Orient2D(ax, ay, bx, by, cx, cy);
    if (winding == 0) return false;
    int ab = ExactPredicates.Orient2D(ax, ay, bx, by, px, py);
    int bc = ExactPredicates.Orient2D(bx, by, cx, cy, px, py);
    int ca = ExactPredicates.Orient2D(cx, cy, ax, ay, px, py);
    return winding > 0
        ? ab >= 0 && bc >= 0 && ca >= 0
        : ab <= 0 && bc <= 0 && ca <= 0;
}

dotnet run prints:

Text
Inside: True
Clockwise: True
Edge: True
Vertex: True
Outside: False
Degenerate: False

The snippet at the top uses a counterclockwise triangle. The download handles either winding, includes edges and vertices, and returns false for a degenerate triangle. Nonfinite coordinates throw ArgumentOutOfRangeException; outside the documented magnitude range, finite coordinates may also throw.

Variations

Try an outside point: place this after the Contains calls in the full program, keeping its helper.

C#
Console.WriteLine(Contains(3, 3, 0, 0, 4, 0, 0, 4));
Text
False

The short answer

Use ExactPredicates.Orient2D on each directed edge and the point. For a counterclockwise triangle, accept a point when all three signs are nonnegative. For a clockwise triangle, accept it when all three signs are nonpositive. First test the triangle’s own orientation; the helper in the download returns false when it is zero.

This is an inclusive test: edges and vertices count as contained. It is a helper built from OneMath’s exact predicates, not a separate point-containment API in the package. Its degenerate-triangle policy is explicit, so a collapsed triangle cannot accidentally accept an entire line.

How do I check if a point is inside a triangle in C#?

Think of each edge as dividing the plane into two half-planes. An interior point must lie on the triangle’s interior side of every edge. The same-side explanation describes this geometric construction. Here, exact orientation signs supply the side classification directly.

The top snippet uses the triangle (0, 0), (4, 0), (0, 4) and point (1, 1). Its vertices run counterclockwise in a y-up coordinate system, so each interior sign is positive. The full program obtains the winding from the vertices instead of assuming it. Reversing the winding reverses the signs but does not change containment.

OneMath’s predicate contract specifies results of −1, 0 or +1 equal to the determinant sign of the exact real values of the input doubles. This avoids choosing a tolerance merely to compensate for a rounded determinant. It does not recover precision lost before you supplied the coordinates. If two source positions round to the same double, the predicate sees the same coordinate.

What does a degenerate triangle mean?

For this test, it means the three vertices have zero orientation: they lie on a line, including cases where vertices coincide. The helper returns false immediately. Without that guard, three zero edge signs could make a collinear point pass the inclusive comparison even though there is no triangle interior.

If your application wants a collapsed triangle to behave like a segment, that is a different containment policy. Decide it explicitly rather than removing the guard. The supplied program deliberately treats degenerate geometry as not containing any point.

An edge point on a nondegenerate triangle is different: one edge sign can be zero while the others still point inward. The program prints True for (2, 2) on the diagonal and for the vertex (0, 0). An outside point such as (3, 3) fails an edge test and returns False.

Keep coordinates within the documented predicate range. Exactness is guaranteed for zero or magnitudes between 2^-140 and 2^200. NaN and infinities throw. Finite inputs outside that range may throw when the exact path is needed; do not use whether they throw as a validity test. This recipe is for 2D coordinates, not a test that a 3D point lies in a triangle’s plane.

All onemath recipes