计算两个CGPoints之间的距离

iCo*_*r86 26 iphone box2d

我需要计算两个CGPoints 之间的距离.我提到这个这个,但我不明白.

Dai*_*air 52

好吧,你提到的东西也是完整的代码:

CGPoint p2; //[1]
CGPoint p1;
//Assign the coord of p2 and p1...
//End Assign...
CGFloat xDist = (p2.x - p1.x); //[2]
CGFloat yDist = (p2.y - p1.y); //[3]
CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist)); //[4]
Run Code Online (Sandbox Code Playgroud)

距离是可变距离.

这里发生了什么:

  1. 首先,我们提出两点......
  2. 然后我们找到点的x坐标之间的距离.
  3. 现在我们找到y坐标之间的距离.
  4. 这些长度是三角形的两边,实际上它们是腿,找到斜边的时间,这意味着在做一些数学后重新划分c ^ 2 = a ^ 2 + b ^ 2我们得到斜边等于sqrt((xDist ^) 2)+(yDist ^ 2)).xDist ^ 2 =(xDist*xDist).同样:yDist ^ 2 =(yDist*yDist)

你不能真正使CGPoint成为距离,距离没有x和y成分.这只是一个数字.

如果您认为CGPoint是一个度量单位(例如,英尺是一个度量单位),则不是.

  • @Houcine:(xy)^ 2 = / =(x ^ 2-y ^ 2)。他们不一样。发布Wolfram Alpha链接无效,但是如果转到Wolfram alpha并在其中输入两个方程式,则会发现两者之间存在差异。 (2认同)
  • 我的数学老师总是教我用2和1进行简单的测试来仔细检查你的数学.使X 2和Y 1:2 ^ 2 - 1 ^ 2 = 4-1 = 3.(2-1)^ 2 = 1 ^ 2 = 1. 1!= 3,所以(xy)^ 2!=(x ^ 2 - y ^ 2) (2认同)

red*_*dux 22

我必须手工完成这一次10,000次,所以我为它编写了一个函数并将其粘贴在我个人库中,我总是在新程序开始时将其丢弃,所以我忘记它不是大炮.

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

所以你这样称呼它(说你想知道你的手指移动了多远):

float moveDistance = [self distanceBetween:touchStart and:touchEnd];
Run Code Online (Sandbox Code Playgroud)

这在移动功能以及滚动菜单中的点检查中非常有用:

if([self distanceBetween:touchStart and:touchAt] > 20*scalePoints)
    isItATap = FALSE;
Run Code Online (Sandbox Code Playgroud)

在touchesBegan中设置"isItATap"为true,将上面的内容放在touchesMoved中,然后你就知道玩家将他们的手指移动得太远,触摸不是"点击",所以你可以让它不选择玩家触摸的对象而是滚动对象周围.

至于比例,这应该取决于你是否有视网膜显示和你所使用的设备的大小(视网膜显示除以2,因为用户的手指在普通屏幕上的物理距离为5"点"感觉它会在视网膜显示屏上显示为10"像素",因为每个点都是4个像素,所以你最终会遇到玩家很难点击视网膜显示屏的情况(这是一种常见的疏忽) )


And*_*iot 20

听起来你可能想要从p1到p2(或差异)的矢量而不是距离.

const CGPoint p1 = {10, 10};
const CGPoint p2 = {510, 310};

const CGPoint diff = {p2.x - p1.x, p2.y - p1.y} // == (CGPoint){500, 300}
Run Code Online (Sandbox Code Playgroud)


Fir*_*iro 15

简答

CGPoint p1, p2; // Having two points
CGFloat distance = hypotf((p1.x-p2.x), (p1.y-p2.y));
Run Code Online (Sandbox Code Playgroud)

更长的爆炸

如果你有两个点p1,p2很明显很容易找到他们heightwidth(例如ABS(p1.x - p2.x))之间的差异,但要找到他们距离的真实表示,你真的想要连字符(H下图).

 p1
  |\  
  | \  
  |  \ H
  |   \
  |    \
  |_ _ _\
         p2
Run Code Online (Sandbox Code Playgroud)

值得庆幸的是,有一个内置的宏:( hypotfhypotfor doubles):

// Returns the hypothenuse (the distance between p1 & p2)
CGFloat dist = hypotf((p1.x-p2.x), (p1.y-p2.y));
Run Code Online (Sandbox Code Playgroud)

(原始参考)


Chr*_*lli 11

在Swift中,您可以为CGPoint添加扩展:

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

并像这样使用它:

let distance = p1.distance(to: p2)
Run Code Online (Sandbox Code Playgroud)


Ozg*_*hin 7

在Apple的示例项目中,他们使用hypot。这将返回两点之间的斜边(距离),如本答案中所述。

extension CGPoint {

    func distance(from point: CGPoint) -> CGFloat {
        return hypot(point.x - x, point.y - y)
    }
}
Run Code Online (Sandbox Code Playgroud)