---
title: "Test if three points are clockwise in C#"
url: https://recursiveai.org/onemath/cookbook/test-clockwise-order-of-three-points/
description: "Use OneMath to tell whether three 2D points are clockwise, counterclockwise, or collinear in C#, even when rounding hides a nearly straight turn."
version: "1.0.0"
---

# Test if three points are clockwise 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-clockwise-order-of-three-points/test-clockwise-order-of-three-points.zip)

Test which way three 2D points turn, even when they’re nearly on a straight line.

```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.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](https://recursiveai.org/onemath/cookbook/test-clockwise-order-of-three-points/test-clockwise-order-of-three-points.zip), then `dotnet run`.

Prints:

```text
-1
```

## The full program

```csharp
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})");
}
```

```text
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:

```csharp
using RecursiveAI.OneMath.Predicates;

int turn = -ExactPredicates.Orient2D(
    0, 0, 4, 0, 4, 2);
Console.WriteLine(turn); // clockwise
```

## Related recipes

- [Use exact geometric predicates](https://recursiveai.org/onemath/cookbook/exact-predicates/)
- [Classify which side of a plane a point is on](https://recursiveai.org/onemath/cookbook/classify-point-side-of-plane/)

## 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](https://recursiveai.org/onemath/docs/predicates/) 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](https://recursiveai.org/onemath/docs/predicates/) 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.
