gpt4 book ai didi

ios - 找到 MKPolygon 的精确质心(作为 MKMapPoint)

转载 作者:行者123 更新时间:2023-11-29 12:37:59 26 4
gpt4 key购买 nike

这意味着排除任何 interiorPolygons 的区域。

一旦有了外部 points 多边形的质心,如何(即以 Objective-C 示例的形式)通过减法 interiorPolygons?或者是否有更优雅的方法来一次计算质心?

如果您帮助代码运行,它将开源 (WIP here)。

可能有帮助:

  1. > http://www.ecourses.ou.edu/cgi-bin/eBook.cgi?topic=st&chap_sec=07.2&page=case_sol
  2. > https://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

最佳答案

今天想想,将每个内部质心按面积加权添加到外部质心会得到合理的结果,这在定性上是有意义的。 (左侧具有内部多边形(孔)的正方形会将质心向右移动,与孔的面积成正比。)

不按比例:nothing to scale

- (MKMapPoint)calculateCentroid
{
switch (self.pointCount) {
case 0: return MKMapPointMake(0.0,
0.0);
case 1: return MKMapPointMake(self.points[0].x,
self.points[0].y);
case 2: return MKMapPointMake((self.points[0].x + self.points[1].x) / 2.0,
(self.points[0].y + self.points[1].y) / 2.0);

}

// onward implies pointCount >= 3

MKMapPoint centroid;
MKMapPoint *previousPoint = &(self.points[self.pointCount-1]); // for i=0, wrap around to the last point

for (NSUInteger i = 0; i < self.pointCount; ++i) {
MKMapPoint *point = &(self.points[i]);

double delta = (previousPoint->x * point->y) - (point->x * previousPoint->y); // x[i-1]*y[i] + x[i]*y[i-1]

centroid.x += (previousPoint->x + point->x) * delta; // (x[i-1] + x[i]) / delta
centroid.y += (previousPoint->y + point->y) * delta; // (y[i-1] + y[i]) / delta

previousPoint = point;
}

centroid.x /= 6.0 * self.area;
centroid.y /= 6.0 * self.area;

// interiorPolygons are holes (subtractive geometry model)
for (MKPolygon *interiorPoly in self.interiorPolygons) {
if (interiorPoly.area == 0.0) {
continue; // avoid div-by-zero
}

centroid.x += interiorPoly.centroid.x / interiorPoly.area;
centroid.y += interiorPoly.centroid.y / interiorPoly.area;
}

return centroid;
}

关于ios - 找到 MKPolygon 的精确质心(作为 MKMapPoint),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25755865/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com