计算两个CGPoints之间的距离 [英] Calculate the distance between two CGPoints

查看:117
本文介绍了计算两个CGPoints之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算两个 CGPoint 之间的距离。我提到这个这个,但我不知道得到它。

i need to calculate the distance between two CGPoints. I refered this and this, but I don't get it.

推荐答案

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

Well, with stuff your refering too where is the full code:

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]

距离是可变距离。

这里发生了什么:


  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)

  1. So first off we make two points...
  2. Then we find the distance between x coordinates of the points.
  3. Now we find the distance between the y coordinates.
  4. These lengths are two sides of the triangle, infact they are the legs, time to find the hypotenuse which means after doing some math to rearragne c^2 = a^2 + b^2 we get the hypotenuse to equal sqrt((xDist^2) + (yDist^2)). xDist^2 = (xDist * xDist). And likewise: yDist^2 = (yDist * yDist)

你真的不能让CGPoint成为距离,距离不是' t有一个x和y分量。它只是1个数字。

You can't really make a CGPoint be the distance, distance doesn't have an x and y component. It is just 1 number.

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

If you think CGPoint is a unit of measurement (for example feet is a unit of measurement) it is not.

这篇关于计算两个CGPoints之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆