[轉]多邊形點集排序--針對凸多邊形,按逆時針方向進行排序
阿新 • • 發佈:2019-01-10
public static void ClockwiseSortPoints(List<Point3D> vPoints)
{
//計算重心
Point3D center = new Point3D();
double X = 0, Y = 0;
for (int i = 0; i < vPoints.Count; i++) {
X += vPoints[i].X;
Y += vPoints[i].Y;
}
center.X = (int)X / vPoints.Count;
center.Y = (int)Y / vPoints.Count;
//氣泡排序
for (int i = 0; i < vPoints.Count - 1; i++) {
for (int j = 0; j < vPoints.Count - i - 1; j++) {
if (PointCmp(vPoints[j], vPoints[j + 1], center)) {
Point3D tmp = vPoints[j];
vPoints[j] = vPoints[j + 1];
vPoints[j + 1] = tmp;
}
}
}
}
{
//計算重心
Point3D center = new Point3D();
double X = 0, Y = 0;
for (int i = 0; i < vPoints.Count; i++) {
X += vPoints[i].X;
Y += vPoints[i].Y;
}
center.X = (int)X / vPoints.Count;
center.Y = (int)Y / vPoints.Count;
//氣泡排序
for (int i = 0; i < vPoints.Count - 1; i++) {
for (int j = 0; j < vPoints.Count - i - 1; j++) {
if (PointCmp(vPoints[j], vPoints[j + 1], center)) {
Point3D tmp = vPoints[j];
vPoints[j] = vPoints[j + 1];
vPoints[j + 1] = tmp;
}
}
}
}