GSharker / G-Shark

G-Shark is a free and open-source geometry library designed for computational designers and software developers in the Architecture, Engineering, and Construction (AEC) industry.
https://gsharker.github.io/G-Shark/
MIT License
215 stars 37 forks source link

Determine if a list of points are in clockwise order #409

Closed sonomirco closed 1 year ago

sonomirco commented 1 year ago

https://stackoverflow.com/questions/1165647/how-to-determine-if-a-list-of-polygon-points-are-in-clockwise-order.

public static bool IsClockwise(IList<Point3> points)
{

    double sum = 0;

    for (int i = 0; i < points.Count - 1; i++)
    {

        sum += (points[i + 1].X - points[i].X) * (points[i + 1].Y + points[i].Y);

    if (sum == 0.0)
    {
        throw new ArgumentException("The points are on a straight line or are creating a closed loop.");
    }

    return sum > 0;
}

public static bool IsClockwise(IList<Point3> points, Plane pl)
{
    var projectedPts = points.Select(pt => pl.ClosestPoint(pt, out _)).ToList();
    return IsClockwise(projectedPts);
}