public static List <Point> Sort(List <Point> Points) { Point CenterPoint = new Point { x = Points.Sum(m => m.x) / Points.Count, y = Points.Sum(m => m.y) / Points.Count }; List <SortPoint> SortedPoint = new List <SortPoint>(); SortedPoint = (from p in Points select new SortPoint { Angle = Math.Atan2((p.y - CenterPoint.y), (p.x - CenterPoint.x)), Point = p }).OrderBy(m => m.Angle).ToList(); for (int i = 0; i < SortedPoint.Count; i++) { if (i != SortedPoint.Count - 1) { if (SortedPoint[i].Point.y > SortedPoint[i + 1].Point.y) { SortPoint temp = new SortPoint(); temp = SortedPoint[i]; SortedPoint[i] = SortedPoint[i + 1]; SortedPoint[i + 1] = temp; } } else { break; } } return(SortedPoint.Select(m => m.Point).ToList()); }
public static Polygon SortPoints(List <Point> Points) { Point CenterPoint = new Point { x = Points.Sum(m => m.x) / Points.Count, y = Points.Sum(m => m.y) / Points.Count }; List <SortPoint> SortedPoint = new List <SortPoint>(); SortedPoint = (from p in Points select new SortPoint { Angle = Math.Atan2((p.y - CenterPoint.y), (p.x - CenterPoint.x)), Point = p }).OrderBy(m => m.Angle).ToList(); for (int i = 0; i < SortedPoint.Count; i++) { if (i != SortedPoint.Count - 1) { if (SortedPoint[i].Angle == SortedPoint[i + 1].Angle) { if (SortedPoint[i].Point.y > SortedPoint[i + 1].Point.y) { SortPoint temp = new SortPoint(); temp = SortedPoint[i]; SortedPoint[i] = SortedPoint[i + 1]; SortedPoint[i + 1] = temp; } } } else { break; } } Polygon polygon = new Polygon(); foreach (var item in SortedPoint) { Console.WriteLine("({0},{1})\t", item.Point.x, item.Point.y); polygon.Vertex.AddLast(item.Point); } return(polygon); }