private static double CompareDistance(CartesianCoordinates referencePoint, CartesianCoordinates a, CartesianCoordinates b)
        {
            var distA = (referencePoint.X - a.X) * (referencePoint.X - a.X) + (referencePoint.Y - a.Y) * (referencePoint.Y - a.Y);
            var distB = (referencePoint.X - b.X) * (referencePoint.X - b.X) + (referencePoint.Y - b.Y) * (referencePoint.Y - b.Y);

            return(distA - distB);
        }
        public static void SortForNonSelfIntersectingPolygon(this List <CartesianCoordinates> points)
        {
            var refX           = points.Select(_ => _.X).Max();
            var refY           = points.Where(_ => _.X == refX).Select(_ => _.Y).Min();
            var referencePoint = new CartesianCoordinates(refX, refY);

            points.Sort((CartesianCoordinates a, CartesianCoordinates b) => CompareAngle(referencePoint, a, b));
        }
        private static int CompareAngle(CartesianCoordinates referencePoint, CartesianCoordinates a, CartesianCoordinates b)
        {
            var left            = IsLeft(referencePoint, a, b);
            var compareDistance = CompareDistance(referencePoint, a, b);

            if (left == 0)
            {
                return(compareDistance == 0 ? 0 : compareDistance > 0 ? 1 : -1);
            }
            return(left == 0 ? 0 : left > 0 ? 1 : -1);
        }
 private static double IsLeft(CartesianCoordinates referencePoint, CartesianCoordinates a, CartesianCoordinates b)
 {
     return((a.X - referencePoint.X) * (b.Y - referencePoint.Y) - (b.X - referencePoint.X) * (a.Y - referencePoint.Y));
 }