找到两个CG点之间的距离?

时间:2022-02-11 15:24:47

I am using these methods and these variables

我正在使用这些方法和这些变量

CGPoint touchBegan;
CGPoint touchEnd;

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

}

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

}

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

}

But I can not get the distance between two points. For example draw a line with your finger and get the distance between a CGPoint touchBegan and CGPoint touchEnd

但我无法得到两点之间的距离。例如,用手指画一条线,得到CGPoint touchBegan和CGPoint touchEnd之间的距离

Any help is appreciated thanks

任何帮助表示赞赏谢谢

2 个解决方案

#1


4  

It doesn't seem to exist any method or function that do this directly, you must take the difference of the two points coordinates and use pythagorean theorem:

似乎没有任何方法或函数直接执行此操作,您必须采用两点坐标的差异并使用毕达哥拉斯定理:

CGPoint touchBegan;
CGPoint touchEnd;

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch* touch= [touches anyObject];
    touchBegan= [touch locationInView: self.view];
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch* touch= [touches anyObject];
    touchEnd= [touch locationInView: self.view];
    CGFloat dx= touchBegan.x - touchEnd.x;
    CGFloat dy= touchBegan.y - touchEnd.y;
    CGFloat distance= sqrt(dx*dx + dy*dy);
    < Do stuff with distance >
}

#2


2  

Just implement your own rendition of the Pythagorean theorem. For example:

只需实现你自己的毕达哥拉斯定理的演绎。例如:

CGPoint translation = CGPointMake(endPoint.x - startPoint.x, endPoint.y - startPoint.y);
CGFloat distance = sqrtf(translation.x * translation.x + translation.y * translation.y);

Or, better, as Rob Mayoff pointed out, use the Math.h hypotf method:

或者,更好的是,正如Rob Mayoff指出的那样,使用Math.h hypotf方法:

CGFloat distance = hypotf(translation.x, translation.y);

#1


4  

It doesn't seem to exist any method or function that do this directly, you must take the difference of the two points coordinates and use pythagorean theorem:

似乎没有任何方法或函数直接执行此操作,您必须采用两点坐标的差异并使用毕达哥拉斯定理:

CGPoint touchBegan;
CGPoint touchEnd;

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch* touch= [touches anyObject];
    touchBegan= [touch locationInView: self.view];
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch* touch= [touches anyObject];
    touchEnd= [touch locationInView: self.view];
    CGFloat dx= touchBegan.x - touchEnd.x;
    CGFloat dy= touchBegan.y - touchEnd.y;
    CGFloat distance= sqrt(dx*dx + dy*dy);
    < Do stuff with distance >
}

#2


2  

Just implement your own rendition of the Pythagorean theorem. For example:

只需实现你自己的毕达哥拉斯定理的演绎。例如:

CGPoint translation = CGPointMake(endPoint.x - startPoint.x, endPoint.y - startPoint.y);
CGFloat distance = sqrtf(translation.x * translation.x + translation.y * translation.y);

Or, better, as Rob Mayoff pointed out, use the Math.h hypotf method:

或者,更好的是,正如Rob Mayoff指出的那样,使用Math.h hypotf方法:

CGFloat distance = hypotf(translation.x, translation.y);