有一条跟随另一条路的路径[英] Having a path that follows another one

本文是小编为大家收集整理的关于有一条跟随另一条路的路径的处理方法,想解了有一条跟随另一条路的路径的问题怎么解决?有一条跟随另一条路的路径问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

我有一系列坐标(地理坐标,但这没关系),我需要有一条"遵循"我们已经拥有的路径的路径.

我们需要以下图像之类的东西.您可以看到该路径并不完全相同(不是简单的偏移),我们也不希望它扩展.

有些库可以用来做到这一点,或者有一些有关如何实施的库?

 paths

推荐答案

花了太多时间尝试找到工作解决方案后,我最终编码了自己的编码:

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);
}

唯一警告是它还不能很好地处理急性角度.

,但否则给出一些漂亮的东西(右图是原始路径,左为偏移)

 map

其他推荐答案

您想要的称为平行曲线: http://en.wikipedia.org/wiki/parallel_curve

生成的一种方法是计算每个点处原始曲线的正常值,然后使用正常值来抵消这些点.如果您只有直线段,这很简单.对于ARCS和Bezzier曲线,您还需要弄清楚如何修改控制点.

本文地址:https://www.itbaoku.cn/post/359414.html