Having a path that follows another one

白昼怎懂夜的黑 提交于 2019-12-04 13:31:25

问题


I have an array of coordinates (geographic coordinates, but that shouldn't matter) and I need to have a path that "follows" the path that we already have.

We need something like on the following image. You can see that the path is not exactly the same (not a simple offset) and we don't want it to scale either.

Is there some library that we could use to do that or some pointers on how to implement this?


回答1:


After spending way too much time trying to find a working solution I ended up coding my own:

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

Only caveat is that it doesn't handle acute angles very well yet.

But otherwise gives something pretty neat (right is original path, left is offset)




回答2:


What you want is called a parallel curve: http://en.wikipedia.org/wiki/Parallel_curve

One way to generate that is to compute the normal of your original curve at each point, and then offset those points using that normal. This is quite simple if you only have straight line segments. For arcs and bezier curves, you also need to figure out how to modify the control points.



来源:https://stackoverflow.com/questions/12134186/having-a-path-that-follows-another-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!