查找坐标是否位于给定折线中的最佳实践?

发布于 2024-12-18 05:56:46 字数 92 浏览 1 评论 0原文

对于导航应用程序,我需要检测用户何时偏离给定的驾驶路径(表示为坐标列表),所以我想要做的是每当我为用户获得新的位置更新时,我都会检查该位置是否在路径中。 是不是太复杂了?

For a navigation app, i need to detect when the user deviated from a given driving path(represented as a list of coordinates), so what i want to do is whenever i get a new location update for the user, ill check if this location is in the path.
is that too complicated?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小猫一只 2024-12-25 05:56:46

对于类似的问题,我使用 CGPath 创建一条路径,然后测试路径中是否有一个点。通过控制路径宽度,您可以相当轻松地确定偏差量。

以下是示例代码,用于测试触摸事件中的锥体:

- (void)createPath {
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(   path, nil, 400, 300);
    CGPathAddLineToPoint(path, nil, 500, 300);
    CGPathAddLineToPoint(path, nil, 500, 400);
    CGPathAddLineToPoint(path, nil, 400, 400);
    self.pathRef   = path;

    CGContextRef context = [self createOffscreenContext];
    CGContextSetLineWidth(context, self.pathWidth);

    CGContextBeginPath(context);
    CGContextAddPath(context, self.pathRef);    
}

- (CGContextRef)createOffscreenContext {
    CFMutableDataRef empty = CFDataCreateMutable(NULL, 0);
    CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData(empty);
    self.offscreenContext = CGPDFContextCreate(consumer, NULL, NULL);
    CGDataConsumerRelease(consumer);
    CFRelease(empty);

    return self.offscreenContext;
}

// Optional, not needed for the test to work
-(void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetStrokeColorWithColor(context, self.colorRef);
    CGContextSetLineWidth(context, self.pathWidth);

    CGContextAddPath(context, self.pathRef);
    CGContextStrokePath(context);
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];

    BOOL isPointInPath = CGContextPathContainsPoint(self.offscreenContext, touchPoint, kCGPathStroke);

    NSLog(@"pip: %d, x: %3.0f, y: %3.0f", isPointInPath, touchPoint.x, touchPoint.y);
}

For a similar problem I create a path with CGPath and then test if a point is in the path. By controlling the path width you can the amount of deviation rather easily.

Here is example code, the point to test cones from a touch event:

- (void)createPath {
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(   path, nil, 400, 300);
    CGPathAddLineToPoint(path, nil, 500, 300);
    CGPathAddLineToPoint(path, nil, 500, 400);
    CGPathAddLineToPoint(path, nil, 400, 400);
    self.pathRef   = path;

    CGContextRef context = [self createOffscreenContext];
    CGContextSetLineWidth(context, self.pathWidth);

    CGContextBeginPath(context);
    CGContextAddPath(context, self.pathRef);    
}

- (CGContextRef)createOffscreenContext {
    CFMutableDataRef empty = CFDataCreateMutable(NULL, 0);
    CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData(empty);
    self.offscreenContext = CGPDFContextCreate(consumer, NULL, NULL);
    CGDataConsumerRelease(consumer);
    CFRelease(empty);

    return self.offscreenContext;
}

// Optional, not needed for the test to work
-(void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetStrokeColorWithColor(context, self.colorRef);
    CGContextSetLineWidth(context, self.pathWidth);

    CGContextAddPath(context, self.pathRef);
    CGContextStrokePath(context);
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];

    BOOL isPointInPath = CGContextPathContainsPoint(self.offscreenContext, touchPoint, kCGPathStroke);

    NSLog(@"pip: %d, x: %3.0f, y: %3.0f", isPointInPath, touchPoint.x, touchPoint.y);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文