我遇到了问题,并且不知道如何解决它。
我正在尝试对点列表进行排序,以便所有点都是为了形成路径。到目前为止我所做的是计算列表中所有点的中心点,然后我使用this post中的代码进行排序。这是借用的代码片段:
library(stringr)
datacsv$fte <- str_extract_all(sapply(strsplit(datacsv$tekst, "fte "), "[", 2), '\\d+\\.*\\d*')
在某些情况下它工作正常,但有时会产生奇迹,请参阅附图,黑点计算中心点:
在图片A中一切正常,但如果我决定向上移动形成两条水平线的点,我会遇到这个:
绿线是它应该是什么样子,黑线是它的真实外观,我无法弄清楚为什么我会这样。我也尝试了public int Compare(Point3D pointA, Point3D pointB)
{
if (pointA.X - CenterPoint.X >= 0 && pointB.X - CenterPoint.X < 0)
return 1;
if (pointA.X - CenterPoint.X < 0 && pointB.X - CenterPoint.X >= 0)
return -1;
if (pointA.X - CenterPoint.X == 0 && pointB.X - CenterPoint.X == 0)
{
if (pointA.Y - CenterPoint.Y >= 0 || pointB.Y - CenterPoint.Y >= 0)
if (pointA.Y > pointB.Y)
return 1;
else return -1;
if (pointB.Y > pointA.Y)
return 1;
else return -1;
}
// compute the cross product of vectors (CenterPoint -> a) x (CenterPoint -> b)
double det = (pointA.X - CenterPoint.X)*(pointB.Y - CenterPoint.Y) -
(pointB.X - CenterPoint.X)*(pointA.Y - CenterPoint.Y);
if (det < 0)
return 1;
if (det > 0)
return -1;
// points a and b are on the same line from the CenterPoint
// check which point is closer to the CenterPoint
double d1 = (pointA.X - CenterPoint.X)*(pointA.X - CenterPoint.X) +
(pointA.Y - CenterPoint.Y)*(pointA.Y - CenterPoint.Y);
double d2 = (pointB.X - CenterPoint.X)*(pointB.X - CenterPoint.X) +
(pointB.Y - CenterPoint.Y)*(pointB.Y - CenterPoint.Y);
if (d1 > d2)
return 1;
else return -1;
}
个解决方案但结果相同。任何帮助都会非常感激。