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

当我们用两个手指在 UIScrollView 中进行多点触摸时,我们得到两个 CG 点。我想知道他们之间的距离。然后当我们再次做夹(内部或外部) ,然后我们将再次得到两分。然后,在再次找到这两个点之间的距离之后,我想决定是否要掐进去或者掐出来。如果我缩短了距离,那么新的距离肯定会缩短,反之亦然。

但不知道如何找到一个精确的测量两点之间的距离进行比较?有人知道这件事吗?

56388 次浏览

Distance between p1 and p2:

CGFloat xDist = (p2.x - p1.x);
CGFloat yDist = (p2.y - p1.y);
CGFloat distance = sqrt(xDist * xDist + yDist * yDist);

Put in a function:

func distance(_ a: CGPoint, _ b: CGPoint) -> CGFloat {
let xDist = a.x - b.x
let yDist = a.y - b.y
return CGFloat(sqrt(xDist * xDist + yDist * yDist))
}

Background: Pythagorean theorem

If you only need to calculate if the distance between the points increases or decreases, you can omit the sqrt() which will make it a little faster.

-(float)distanceFrom:(CGPoint)point1 to:(CGPoint)point2
{
CGFloat xDist = (point2.x - point1.x);
CGFloat yDist = (point2.y - point1.y);
return sqrt((xDist * xDist) + (yDist * yDist));
}

If you are using cocos2d

float distance = ccpDistance(point1, point2);

You can use the hypot() or hypotf() function to calculate the hypotenuse. Given two points p1 and p2:

CGFloat distance = hypotf(p1.x - p2.x, p1.y - p2.y);

And that's it.

If you want to find the absolute distance value between two points then you can use (for Cocos2d):

float distance = abs(ccpDistance(point1, point2));

I wrote this, I use it a lot:

- (float) distanceBetween : (CGPoint) p1 and: (CGPoint) p2
{
return sqrt(pow(p2.x-p1.x,2)+pow(p2.y-p1.y,2));
}

Call like this:

float distanceMoved = [self distanceBetween touchStart and: touchEnd];

I normally use cocos2d, but I still use my own function for some things because when I was learning I wrote a bunch of my own functions for simple stuff rather than searching for the "official" higher order functions, and additionally I'm not a big fan of functions(vars, vars), I prefer [self functions vars and: vars]

#define rw_pointOffset(point1, point2) CGPointMake(point2.x - point1.x, point2.y - point1.y)
#define rw_pointDistance(point1, point2) sqrtf( powf(point2.x - point1.x, 2.0f) + powf(point2.y - point1.y, 2.0f))

And that´s how you use it:

CGPoint offset = rw_pointOffset(view1.center, view2.center);
float distance = rw_pointDistance(view1.center, view2.center);

For swift users

extension CGPoint {
func distance(to point: CGPoint) -> CGFloat {
return sqrt(pow(x - point.x, 2) + pow(y - point.y, 2))
}
}

With Swift 4, you may choose one of the 5 following Playground codes in order to get the distance between two CGPoint instances.


1. Using Darwin sqrt(_:) function

import CoreGraphics


func distance(from lhs: CGPoint, to rhs: CGPoint) -> CGFloat {
let xDistance = lhs.x - rhs.x
let yDistance = lhs.y - rhs.y
return sqrt(xDistance * xDistance + yDistance * yDistance)
}


let point1 = CGPoint(x: -10, y: -100)
let point2 = CGPoint(x: 30, y: 600)


distance(from: point1, to: point2) // 701.141925718324

2. Using CGFloat squareRoot() method

import CoreGraphics


func distance(from lhs: CGPoint, to rhs: CGPoint) -> CGFloat {
let xDistance = lhs.x - rhs.x
let yDistance = lhs.y - rhs.y
return (xDistance * xDistance + yDistance * yDistance).squareRoot()
}


let point1 = CGPoint(x: -10, y: -100)
let point2 = CGPoint(x: 30, y: 600)


distance(from: point1, to: point2) // 701.141925718324

3. Using CGFloat squareRoot() method and Core Graphics pow(_:_:) function

import CoreGraphics


func distance(from lhs: CGPoint, to rhs: CGPoint) -> CGFloat {
return (pow(lhs.x - rhs.x, 2) + pow(lhs.y - rhs.y, 2)).squareRoot()
}


let point1 = CGPoint(x: -10, y: -100)
let point2 = CGPoint(x: 30, y: 600)


distance(from: point1, to: point2) // 701.141925718324

4. Using Core Graphics hypot(_:_:) function

import CoreGraphics


func distance(from lhs: CGPoint, to rhs: CGPoint) -> CGFloat {
return hypot(lhs.x - rhs.x, lhs.y - rhs.y)
}


let point1 = CGPoint(x: -10, y: -100)
let point2 = CGPoint(x: 30, y: 600)


distance(from: point1, to: point2) // 701.141925718324

5. Using Core Graphics hypot(_:_:) function and CGFloat distance(to:) method

import CoreGraphics


func distance(from lhs: CGPoint, to rhs: CGPoint) -> CGFloat {
return hypot(lhs.x.distance(to: rhs.x), lhs.y.distance(to: rhs.y))
}


let point1 = CGPoint(x: -10, y: -100)
let point2 = CGPoint(x: 30, y: 600)


distance(from: point1, to: point2) // 701.141925718324