wol*_*ine 72 iphone cocoa-touch
当我们在UIScrollView中用两个手指进行多点触控时,我们得到两个CG点.我想找到它们之间的距离.然后我们再次进行捏(内部或外部),然后我们将再次得到两个点.然后在找到这两点之间的距离之后,我想决定是否捏入或捏出.如果我已经收入,肯定新的距离会更小,反之亦然.
但是不知道如何找到2点之间距离的准确测量值来进行比较?有人对此有所了解吗?
luc*_*ius 181
您可以使用hypot()或hypotf()函数来计算斜边.给出两点p1并且p2:
CGFloat distance = hypotf(p1.x - p2.x, p1.y - p2.y);
Run Code Online (Sandbox Code Playgroud)
就是这样.
Ole*_*ann 117
p1和之间的距离p2:
CGFloat xDist = (p2.x - p1.x);
CGFloat yDist = (p2.y - p1.y);
CGFloat distance = sqrt(xDist * xDist + yDist * yDist);
Run Code Online (Sandbox Code Playgroud)
放入一个功能:
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))
}
Run Code Online (Sandbox Code Playgroud)
背景:毕达哥拉斯定理
如果您只需要计算点之间的距离是增加还是减少,您可以省略sqrt(),这会使它更快一些.
Dan*_*rom 24
对于快速用户
extension CGPoint {
func distance(to point: CGPoint) -> CGFloat {
return sqrt(pow(x - point.x, 2) + pow(y - point.y, 2))
}
}
Run Code Online (Sandbox Code Playgroud)
Ima*_*tit 13
使用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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
haa*_*awa 12
-(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));
}
Run Code Online (Sandbox Code Playgroud)
如果您使用的是cocos2d
float distance = ccpDistance(point1, point2);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
47347 次 |
| 最近记录: |