iphone - 寻找 CGPath 的中心

标签 iphone ios cocoa-touch cgpath

我有一个任意的 CGPath,我想找到它的地理中心。我可以使用 CGPathGetPathBoundingBox 获取路径边界框,然后找到该框的中心。但是有没有更好的方法来找到路径的中心?

更新给那些喜欢看代码的人:这里是使用亚当在答案中建议的平均点法的代码(不要错过下面答案中更好的技术)...

    BOOL moved = NO; // the first coord should be a move, the rest add lines
    CGPoint total = CGPointZero;
    for (NSDictionary *coord in [polygon objectForKey:@"coordinates"]) {
        CGPoint point = CGPointMake([(NSNumber *)[coord objectForKey:@"x"] floatValue], 
                                    [(NSNumber *)[coord objectForKey:@"y"] floatValue]);
        if (moved) {
            CGContextAddLineToPoint(context, point.x, point.y);
            // calculate totals of x and y to help find the center later
            // skip the first "move" point since it is repeated at the end in this data
            total.x = total.x + point.x;
            total.y = total.y + point.y;
        } else {
            CGContextMoveToPoint(context, point.x, point.y);
            moved = YES; // we only move once, then we add lines
        }
    }

    // the center is the average of the total points
    CGPoint center = CGPointMake(total.x / ([[polygon objectForKey:@"coordinates"] count]-1), total.y / ([[polygon objectForKey:@"coordinates"] count]-1));

如果你有更好的想法,请分享!

最佳答案

该技术有效,但您在问题中输入的代码无效。 AFAICS,这只适用于你只做直线多边形的少数情况,你有一个点列表,你还没有制作 CGPath 对象还没有。

我需要为任意 CGPath 对象执行此操作。使用 Adam(其他 Adam)的建议和 Apple 的 CGPathApply,我想到了这个,效果很好:

{
            float dataArray[3] = { 0, 0, 0 };
            CGPathApply( (CGPathRef) YOUR_PATH, dataArray, pathApplierSumCoordinatesOfAllPoints);

            float averageX = dataArray[0] / dataArray[2];
            float averageY = dataArray[1]  / dataArray[2];
            CGPoint centerOfPath = CGPointMake(averageX, averageY);
}

static void pathApplierSumCoordinatesOfAllPoints(void* info, const CGPathElement* element)
{
    float* dataArray = (float*) info;
    float xTotal = dataArray[0];
    float yTotal = dataArray[1];
    float numPoints = dataArray[2];


    switch (element->type)
    {
        case kCGPathElementMoveToPoint:
        {
            /** for a move to, add the single target point only */

            CGPoint p = element->points[0];
            xTotal += p.x;
            yTotal += p.y;
            numPoints += 1.0;

        }
            break;
        case kCGPathElementAddLineToPoint:
        {
            /** for a line to, add the single target point only */

            CGPoint p = element->points[0];
            xTotal += p.x;
            yTotal += p.y;
            numPoints += 1.0;

        }
            break;
        case kCGPathElementAddQuadCurveToPoint:
            for( int i=0; i<2; i++ ) // note: quad has TWO not THREE
            {
                /** for a curve, we add all ppints, including the control poitns */
                CGPoint p = element->points[i];
                xTotal += p.x;
                yTotal += p.y;
                numPoints += 1.0;
            }
            break;
        case kCGPathElementAddCurveToPoint:         
            for( int i=0; i<3; i++ ) // note: cubic has THREE not TWO
            {
                /** for a curve, we add all ppints, including the control poitns */
                CGPoint p = element->points[i];
                xTotal += p.x;
                yTotal += p.y;
                numPoints += 1.0;
            }
            break;
        case kCGPathElementCloseSubpath:
            /** for a close path, do nothing */
            break;
    }

    //NSLog(@"new x=%2.2f, new y=%2.2f, new num=%2.2f", xTotal, yTotal, numPoints);
    dataArray[0] = xTotal;
    dataArray[1] = yTotal;
    dataArray[2] = numPoints;
}

关于iphone - 寻找 CGPath 的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7186054/

相关文章:

ios - 用于存储自定义对象的 Userdefault 编码器类的数据

iphone - 为什么在[NSMutableArray array]之后需要此自动释放功能以避免内存泄漏?

ios - 关闭两个 View Controller 然后推送一个 View Controller - Swift

ios - 将 Microsoft Project Oxford 语音识别从 Objective-C 转换为 SWIFT

iphone - 用于 segue 动画的 CATransition(iOS 7 之前的样式)

ios - iOS slider 的默认缩略图大小?

ios - 使用 Alamofire 和 multipart/form-data

iphone - 在 View 中填充 UIScrollView w/PageControl

ios - MapBox iOS,我如何突出一个国家?或切换图层可见性?

iphone - iOS - 如何在选择单元格时禁用 UITableView 滚动到顶部?