Поиск центра 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));

Если у вас есть идея получше, поделитесь!

9
задан EFC 1 October 2013 в 21:57
поделиться