当我们在UIScrollView中用两个手指进行多点触控时,我们得到两个CG点。我想找到它们之间的距离。然后我们再次进行捏(内部或外部),然后我们将再次得到两个点。然后在找到这两点之间的距离后,我想决定是否捏入或捏出。如果我已经收入,肯定新的距离会更小,反之亦然。
但是不知道如何找到2点之间距离的准确测量值来进行比较?有人对此有所了解吗?
p1
和p2
之间的距离:
CGFloat xDist = (p2.x - p1.x);
CGFloat yDist = (p2.y - p1.y);
CGFloat distance = sqrt(xDist * xDist + yDist * yDist);
放入一个功能:
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))
}
如果您只需要计算点之间的距离是增加还是减少,您可以省略sqrt(),这会使它更快一些。
您可以使用hypot()
或hypotf()
函数来计算斜边。鉴于两点p1
和p2
:
CGFloat distance = hypotf(p1.x - p2.x, p1.y - p2.y);
就是这样。
对于快速用户
extension CGPoint {
func distance(toPoint p:CGPoint) -> CGFloat {
return sqrt(pow(x - p.x, 2) + pow(y - p.y, 2))
}
}
-(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));
}
如果您使用的是cocos2d
float distance = ccpDistance(point1, point2);
使用Swift 4,您可以选择以下5个Playground代码中的一个,以获得两个CGPoint
实例之间的距离。
sqrt(_:)
函数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
CGFloat
squareRoot()
方法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
CGFloat
squareRoot()
方法和Core Graphics pow(_:_:)
函数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
hypot(_:_:)
函数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
hypot(_:_:)
函数和CGFloat
distance(to:)
方法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
我写了这个,我经常使用它:
- (float) distanceBetween : (CGPoint) p1 and: (CGPoint) p2
{
return sqrt(pow(p2.x-p1.x,2)+pow(p2.y-p1.y,2));
}
像这样打电话:
float distanceMoved = [self distanceBetween touchStart and: touchEnd];
我通常使用cocos2d,但我仍然使用自己的函数来处理某些事情,因为在我学习的时候,我为了简单的东西写了一堆我自己的函数而不是搜索“官方”高阶函数,另外我不是功能的大粉丝(vars,vars),我更喜欢[自我功能变量和: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))
这就是你如何使用它:
CGPoint offset = rw_pointOffset(view1.center, view2.center);
float distance = rw_pointDistance(view1.center, view2.center);
如果要查找两点之间的绝对距离值,则可以使用(对于Cocos2d):
float distance = abs(ccpDistance(point1, point2));