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

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)

就是这样.

  • 你的意思是`让距离= hypotf(浮动(p1.x - p2.x),浮动(p1.y - p2.y)) (6认同)
  • 你的意思是`让距离= hypot(p1.x - p2.x,p1.y - p2.y)`自Swift 2.1起 (6认同)
  • 如果使用swift它的距离= hypotf(浮点数(p1.x) - 浮点数(p2.x),浮点数(p1.y) - 浮点数(p2.y)) (4认同)

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(),这会使它更快一些.

  • 对此没有方便的方法吗? (30认同)
  • 计算几何公理#1:如果你*比较*距离,就不需要承担sqrt()操作的成本. (18认同)
  • @SpaceyRicochet有方法hypotf和ccpDistance.见下面的答案. (4认同)

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实例之间的距离.


1.使用达尔文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)

2.使用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)

3.使用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)

4.使用核心图形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)

5.使用Core Graphics 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)

  • 似乎ccpDistance是cocos2d的一部分,而不是核心的一部分.包括它以避免毕达哥拉斯定理可能是矫枉过正. (2认同)