0

ナビゲーション アプリの場合、ユーザーが特定の運転経路 (座標のリストとして表される) から逸脱したことを検出する必要があるため、ユーザーの新しい場所の更新を取得するたびに、この場所を確認する必要がありますパスにあります。それはあまりにも複雑ですか?

4

1 に答える 1

1

同様の問題について、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);
}
于 2011-11-26T02:49:25.740 に答える