4

我有一个坐标数组(地理坐标,但这不重要),我需要有一条“遵循”我们已经拥有的路径的路径。

我们需要类似下图的东西。您可以看到路径并不完全相同(不是简单的偏移),我们也不希望它缩放。

是否有一些我们可以使用的库或一些关于如何实现它的指针?

路径

4

2 回答 2

4

在花了太多时间试图找到一个可行的解决方案之后,我最终编写了自己的代码:

CGContextBeginPath(context);
CGMutablePathRef path = CGPathCreateMutable();

MKMapPoint *mapPoints = itineraryPath.points;
CGPoint previousEdgeNormal = CGPointZero;
CGPoint previousDrawnPoint = CGPointZero;
float offsetDistance = self.pathWidth*2.5;

for(int i = 0; i < itineraryPath.pointCount; i++) {
    if(i < itineraryPath.pointCount-1) {
        MKMapPoint mapPoint = mapPoints[i];
        CGPoint point = [self pointForMapPoint:mapPoint];
        MKMapPoint secondMapPoint = mapPoints[i+1];
        CGPoint secondPoint = [self pointForMapPoint:secondMapPoint];

        float xDelta = point.x-secondPoint.x;
        float yDelta = point.y-secondPoint.y;
        float factor = xDelta > 0 ? -1 : 1;
        
        float segmentLength = sqrt(pow(xDelta, 2.0)+pow(yDelta, 2.0));
        
        float yDeltaAngle = asin(sin(M_PI/2*factor)*yDelta/segmentLength);
        float opposedAngle = M_PI/2-yDeltaAngle;
        float remainingAngle = M_PI/2-opposedAngle;
        float yOffset = sin(opposedAngle)*offsetDistance/sin(M_PI/2)*factor;
        float xOffset = sin(remainingAngle)*offsetDistance/sin(M_PI/2)*factor;
        
        CGPoint offsetFirstPoint = CGPointMake(point.x+xOffset, point.y+yOffset);
        CGPoint offsetSecondPoint = CGPointMake(secondPoint.x+xOffset, secondPoint.y+yOffset);
        
        if(i == mapPointIndex) {
            CGPathMoveToPoint(path, NULL, offsetFirstPoint.x, offsetFirstPoint.y);
            previousDrawnPoint = offsetFirstPoint;
        }
        else {
            float xNormalDifference = previousEdgeNormal.x-offsetFirstPoint.x;
            float yNormalDifference = previousEdgeNormal.y-offsetFirstPoint.y;
            
            float xAverage = (xNormalDifference)/2;
            float yAverage = (yNormalDifference)/2;
            CGPoint normalAveragePoint = CGPointMake(offsetFirstPoint.x+xAverage, offsetFirstPoint.y+yAverage);

            CGPathAddLineToPoint(path, NULL, normalAveragePoint.x, normalAveragePoint.y);
            previousDrawnPoint = normalAveragePoint;
        }
        
        previousEdgeNormal = offsetSecondPoint;
    }
    else
        CGPathAddLineToPoint(path, NULL, previousEdgeNormal.x, previousEdgeNormal.y);
}

唯一需要注意的是,它还不能很好地处理锐角。

但否则会给出一些非常整洁的东西(右边是原始路径,左边是偏移)

地图

于 2013-03-08T11:58:07.180 回答
2

你想要的叫做平行曲线:http ://en.wikipedia.org/wiki/Parallel_curve

一种生成方法是在每个点计算原始曲线的法线,然后使用该法线偏移这些点。如果您只有直线段,这非常简单。对于圆弧和贝塞尔曲线,您还需要弄清楚如何修改控制点。

于 2012-08-27T10:15:15.533 回答