当我们在UIScrollView中用两根手指进行多点触摸时,会得到两个CG点。我想找到它们之间的距离。然后,当我们再次捏(内部或外部)时,我们将再次获得两分。然后,在再次找到这两个点之间的距离之后,我想确定是捏还是捏。如果我捏了一下,新距离肯定会更短,反之亦然。
但是不知道如何找到2个点之间的距离的准确测量值以进行比较吗?有人对此有想法吗?
Answers:
之间的距离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);
就是这样。
let distance = hypotf(Float(p1.x - p2.x), Float(p1.y - p2.y))
let distance = hypot(p1.x - p2.x, p1.y - p2.y)
自Swift 2.1
使用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()
方法和核心图形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)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);
我写了这个,我经常使用它:
- (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)的忠实拥护者,我更喜欢[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))
这就是您的使用方式:
CGPoint offset = rw_pointOffset(view1.center, view2.center);
float distance = rw_pointDistance(view1.center, view2.center);
CGPoint
s之间的距离。