在iPhone屏幕上测量绘制线的距离

3
我正在尝试创建一个应用程序,允许用户在屏幕上画一条线,并测量所绘制线的距离。我已经成功地画出了这条线,但是不知道如何测量它。这条线也不必完全笔直,它基本上是一个“弯曲”。如果有人能指点我正确的方向或帮助我,那就太好了。我正在使用Xcode 5.1.1和objective-c。今年夏天我才开始涉足这门语言。
编辑:我希望以英寸或厘米为单位测量距离。我希望测量整条线的长度,而不仅仅是位移距离。
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    mouseSwipe = YES; //swipe declared in header

    UITouch *touch = [touches anyObject];
    currentPoint = [touch locationInView:self.view]; //tracking finger movement on screen

    UIGraphicsBeginImageContext(CGSizeMake(320, 568)); // 568 iphone 5, 480 is iphone 4 (320,525)
    [drawImage.image drawInRect:CGRectMake(0, 0, 320, 568)]; // 0,0 centered in corner
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //round line end
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); // width of line

    CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor redColor] CGColor]); //sets color to red (change red to any color for that color)

    //CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0,1,0,1); //green color
    CGContextBeginPath(UIGraphicsGetCurrentContext()); //start of when drawn path
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());

    [drawImage setFrame:CGRectMake(0, 0, 320, 568)]; //(320, 568)
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); //importnant
    UIGraphicsEndImageContext(); //finished drawing for time period
    lastPoint = currentPoint;

    [self.view addSubview:drawImage]; //adds to page
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [[event allTouches] anyObject]; //touch fires of touch

    location = [touch locationInView:touch.view];
    lastClick = [NSDate date];

    lastPoint = [touch locationInView:self.view]; //stops connecting to previous line
    lastPoint.y -= 0;

    [super touchesEnded:touches withEvent: event];
}

1
请澄清您所说的距离是指物理屏幕上线条的厘米/英寸测量,还是指点数/像素等距离。此外,您想要测量的长度是起点和终点之间的直线长度,还是要沿着线条的曲线进行测量? - Mike Mertsock
1
为了澄清@esker的问题,您感兴趣的是什么数量 - 直线距离还是线的长度(如果您沿着绘制的线行走,您将走过的距离)。 可以安全地假设两个答案都是在输入的坐标系中(可能是像素)。 - danh
好的。在这个问题中,“长度”比“距离”更合适。你是通过touchesMoved来绘制它的吗?那么你将从上一个位置绘制到当前位置。计算连续点之间的笛卡尔距离并累加它们。 - danh
我正在使用touchesMoved。我该如何做到这一点? - bolencki13
@bolencki13,你能在gist上分享一下可用的代码吗?我有类似的需求。谢谢。 - inox
显示剩余2条评论
1个回答

1

首先添加一个属性,累计路径的长度。

@property(nonatomic, assign) CGFloat pathLength;

当用户开始绘制路径时,请将其初始化为0.0。可以在touchesBegan中执行此操作,或者在代码的其他位置意识到您要开始绘制时执行此操作。添加一个计算点之间笛卡尔距离的方法:

- (CGFloat)distanceFrom:(CGPoint)p1 to:(CGPoint)p2 {
    CGFloat x = (p2.x - p1.x);
    CGFloat y = (p2.y - p1.y);
    return sqrt(x*x + y*y);
}

当你移动触摸时,你已经处理了当前和上一个触摸位置。现在你需要做的就是累计相邻点之间的距离:

// in touches moved, after you have lastPoint and currentPoint
self.pathLength += [self distanceFrom:currentPoint to:lastPoint];

这里和其他地方都有很多关于将这些点转换为英寸或厘米的参考资料。据我所知,所有这些参考资料都无法从SDK中在运行时获取设备分辨率。如果您愿意在代码中添加一个(有风险的)常量获取PPI, 并将其除以上面计算得出的pathLength。


我实现了你写的代码,并将其输出到标签和调试日志(NSLog)。它一直保持在0.0。我不确定如何更新它。 - bolencki13
你能在distanceFrom方法中使用NSLog(@"%f,%f", x,y);吗? - danh
点是相等的。输入的点为:263.500000(p2.y),263.500000(p1.y),89.000000(p2.x),89.000000(p1.x)。 - bolencki13
如果点相等,则距离为零。 - Milo
是的,我意识到了,但我不知道计算线长的正确公式。@Milo - bolencki13
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接