List<PointF> SortPoints(PointF[] points) { List<PointF> result = new List<PointF>(); PointF center = GetGravityPoint(points.ToList()); PointF x = new PointF(center.X+1, center.Y); PointF OX = new PointF(1,0); double OX_len = 1; List<double> angle = new List<double>(); foreach (PointF point in points) { PointF tmpO = new PointF(point.X-center.X,point.Y-center.Y); double tmp_len = Math.Sqrt((point.X - center.X)*(point.X - center.X)+(point.Y - center.Y)*(point.Y - center.Y)); angle.Add(Math.Asin((tmpO.X)/tmp_len)); } List<double> tmpAngle = new List<double>(angle); tmpAngle.Sort(); List<double> doubles = new List<double>(); for(int i = 0;i< tmpAngle.Count;i++) { double tmp = tmpAngle[i]; int v = angle.FindIndex(item => item.Equals(tmp)); result.Add(points[v]); } return result; } private PointF GetGravityPoint(List<PointF> lstPol) { double n, i; double x1, y1, x2, y2, x3, y3; double sum_x = 0, sum_y = 0, sum_s = 0; x1 = lstPol[0].X; y1 = lstPol[0].Y; x2 = lstPol[1].X; y2 = lstPol[1].Y; n = lstPol.Count; int k = 2; for (i = 1; i <= n - 2; i++) { x3 = lstPol[k].X; y3 = lstPol[k].Y; double s = ((x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1)) / 2.0; sum_x += (x1 + x2 + x3) * s; sum_y += (y1 + y2 + y3) * s; sum_s += s; x2 = x3; y2 = y3; k++; } PointF center = PointF.Empty; center.X = Convert.ToSingle(sum_x / sum_s / 3.0); center.Y = Convert.ToSingle(sum_y / sum_s / 3.0); //MessageBox.Show("("+center.X.ToString()+","+center.Y.ToString()+")"); return center; }
标签:center,PointF,point,List,多点,lstPol,new,排序,任意 From: https://www.cnblogs.com/echo-efun/p/17921621.html