Test which way three 2D points turn, even when they’re nearly on a straight line.
dotnet add package RecursiveAI.OneMathRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneMath.Predicates; const double n = 134_217_728; int turn = ExactPredicates.Orient2D( n, n + 1, n + 1, n + 2, 0, 0); Console.WriteLine(turn); // clockwise
With y up: −1 = clockwise, +1 = counterclockwise, 0 = collinear.
For these nearly collinear points, the plain double cross product rounds to 0, but Orient2D returns the exact sign.
Unzip the example, then dotnet run.
Prints:
-1The full program
using RecursiveAI.OneMath.Predicates; const double n = 134_217_728; var a = (X: n, Y: n + 1); var b = (X: n + 1, Y: n + 2); var c = (X: 0.0, Y: 0.0); PrintTurn("a, b, c", a, b, c); PrintTurn("b, a, c", b, a, c); PrintTurn("a, twice a, c", a, (2 * a.X, 2 * a.Y), c); double rounded = a.X * b.Y - a.Y * b.X; Console.WriteLine($"Rounded cross product: {rounded}"); static void PrintTurn(string label, (double X, double Y) a, (double X, double Y) b, (double X, double Y) c) { int sign = ExactPredicates.Orient2D( a.X, a.Y, b.X, b.Y, c.X, c.Y); string turn = sign switch { -1 => "clockwise", 1 => "counterclockwise", _ => "collinear" }; Console.WriteLine($"{label}: {turn} ({sign})"); }
a, b, c: clockwise (-1) b, a, c: counterclockwise (1) a, twice a, c: collinear (0) Rounded cross product: 0
The full program swaps the first two points to reverse the turn, then uses an exactly collinear triple. It also prints the rounded cross product for the first triple, which incorrectly comes out zero.
Orient2D throws ArgumentOutOfRangeException for NaN or infinity; for finite coordinates, the exact sign is guaranteed when each coordinate is zero or has magnitude from 2^-140 through 2^200.
Variations
Use screen coordinates, where y increases downward. Keep the coordinates as they are and reverse the sign before interpreting it:
using RecursiveAI.OneMath.Predicates; int turn = -ExactPredicates.Orient2D( 0, 0, 4, 0, 4, 2); Console.WriteLine(turn); // clockwise
Related recipes
The short answer
Call ExactPredicates.Orient2D(ax, ay, bx, by, cx, cy) with three points in order. With x increasing rightward and y upward, −1 means clockwise, +1 means counterclockwise, and 0 means collinear. The predicate contract defines the answer as the sign of the determinant of the input doubles treated as exact values.
How can you tell if three points are clockwise or counterclockwise?
Follow the path from A to B to C. If C lies to the right of the directed line A→B, the path turns clockwise; if it lies to the left, it turns counterclockwise. This is the sign of the 2D cross product (bx − ax)(cy − ay) − (by − ay)(cx − ax). Swapping any two points changes the sign. Cycling the points, for example from A, B, C to B, C, A, keeps it. Those rules are useful when comparing the winding of triangles even if different parts of your code start at different vertices.
How do you tell if three points are collinear?
A zero sign means the stored coordinates lie on the same straight line, or that at least two of the points coincide. Check for zero separately if you need to distinguish a degenerate triangle from one with clockwise winding.
“On the line” isn’t the same as “close enough to the line.” The sample uses integer coordinates that fit exactly in double. With C at the origin, the two products are n(n + 2) and (n + 1)². The second is exactly one larger, but they round to the same double. The predicate implementation uses a floating-point error bound to decide when the rounded sign is reliable and evaluates the determinant exactly when it isn’t.
If you’re working with measured coordinates and want to treat points within a certain distance of the line as collinear, that distance is a separate rule in your coordinate units. An exact orientation test can’t recover digits already lost when coordinates were converted to double, and it doesn’t apply a measurement tolerance for you.