For supported coordinates, InCircle gives an exact sign even for points on or very near the circumcircle.
dotnet add package RecursiveAI.OneMathRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
With vertices in counterclockwise order: 1 inside, -1 outside, 0 on the circle.
using RecursiveAI.OneMath.Predicates; int Test(double x, double y) => ExactPredicates.InCircle( 0, 0, 4, 0, 0, 4, x, y); Console.WriteLine(Test(2, 2)); // inside Console.WriteLine(Test(5, 2)); // outside Console.WriteLine(Test(4, 4)); // on circle
Unzip the example, then dotnet run.
Prints:
1 -1 0
The full program
using RecursiveAI.OneMath.Predicates; int Test(double x, double y) => ExactPredicates.InCircle( 0, 0, 4, 0, 0, 4, x, y); Console.WriteLine(Test(2, 2)); // inside Console.WriteLine(Test(5, 2)); // outside Console.WriteLine(Test(4, 4)); // on circle var clockwise = ExactPredicates.Orient2D(0, 0, 0, 4, 4, 0); var raw = ExactPredicates.InCircle(0, 0, 0, 4, 4, 0, 2, 2); Console.WriteLine($"Clockwise raw sign: {raw}"); Console.WriteLine($"Orientation-adjusted: {raw * clockwise}");
1 -1 0 Clockwise raw sign: -1 Orientation-adjusted: 1
The three vertices in the snippet run counterclockwise in XY coordinates with Y pointing up. In that order 1 means strictly inside, -1 strictly outside, and 0 on the circle. The full program swaps two vertices to show the raw sign flip for a clockwise triangle.
If Orient2D returns 0, the vertices don’t define a unique circle; don’t interpret the InCircle sign for that triple. Nonfinite coordinates throw ArgumentOutOfRangeException.
Variations
Test the nearest representable doubles on either side of x = 4 at y = 4. Append this to the snippet at the top:
Console.WriteLine(Test(Math.BitDecrement(4), 4)); Console.WriteLine(Test(4, 4)); Console.WriteLine(Test(Math.BitIncrement(4), 4));
The added lines are 1, 0, and -1, respectively. A point close to the circle is not automatically treated as being on it.
Related recipes
The short answer
In C#, call ExactPredicates.InCircle(ax, ay, bx, by, cx, cy, px, py) with the three triangle vertices first and the test point last. If the vertices are counterclockwise, a positive result means inside, a negative result means outside, and zero means on the circle. To handle either vertex order, find the orientation with ExactPredicates.Orient2D. Reject zero orientation and multiply the in-circle sign by the orientation for the other two cases.
What is the circumcircle of a triangle?
It’s the circle that passes through all three vertices, not the circle tangent to its three sides. In the example, (0, 0), (4, 0), and (0, 4) define a circle centered at (2, 2). The test point (4, 4) is not inside that triangle, but it is on the circumcircle. That’s why a point-in-triangle test can’t substitute for an in-circle test. If the defining vertices are collinear or repeated, there is no unique circumcircle to test.
How do you test whether a point is inside the circle through three points?
InCircle translates the three vertices by subtracting the test point. For each translated vertex, the determinant uses its X coordinate, its Y coordinate, and the sum of their squares. Only the sign matters. Exchanging two vertices swaps two determinant rows and reverses that sign, without changing the circle. In screen coordinates with Y increasing downward, use the result from Orient2D rather than guessing the orientation from how the triangle looks on screen.
If you test many points against the same triangle, compute its orientation once. The example’s full program shows what the correction does: the clockwise raw sign of -1 becomes 1 after multiplication by the orientation -1. An orientation of zero is a separate case, not a point on the boundary.
How do you make a circumcircle test robust near the boundary?
A rounded center and radius can make a point near the circle appear on the wrong side. An arbitrary distance tolerance also changes the meaning of “on the circle.” OneMath first checks whether the floating-point determinant is far enough from zero to trust its sign. When it isn’t, it evaluates the determinant with exact arithmetic. The sign describes the actual double coordinates you passed in, not decimal values before they were rounded to doubles.
Use finite coordinates that are either zero or have magnitudes from 2⁻¹⁴⁰ through 2²⁰⁰, inclusive. Outside that range, a finite input may return a sign or throw ArgumentOutOfRangeException, depending on whether exact evaluation is needed. The predicate docs give the input bounds and determinant for InCircle.