计算两个cgpoint之间的距离

时间:2020-12-15 15:21:45

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

我需要计算两个点之间的距离。我提到过这个和这个,但我不明白。

7 个解决方案

#1


48  

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]

The distance is the variable distance.

距离是可变距离。

What is going on here:

这里发生了什么:

  1. So first off we make two points...
  2. 首先我们提出两点…
  3. Then we find the distance between x coordinates of the points.
  4. 然后求出点x坐标之间的距离。
  5. Now we find the distance between the y coordinates.
  6. 现在我们求出y坐标之间的距离。
  7. 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)
  8. 这些长度是三角形的两面,事实上他们是腿,找点时间,斜边这意味着后做一些数学rearragne c ^ 2 = ^ 2 + b ^ 2得到斜边等于√((xDist ^ 2)+(yDist ^ 2))。xDist ^ 2 =(xDist * xDist)。同样:yDist ^ 2 =(yDist * yDist)

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不能表示距离,距离没有x和y分量。它只是一个数字。

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

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

#2


21  

I've had to do this by hand 10,000 times so I wrote a function for it and stuck it in my personal library that I always dump in at the beginning of a new program so I forget it's not cannon.

我不得不手工做了10000次,所以我为它写了一个函数,并把它粘贴到我的个人库中,我总是在一个新程序开始的时候把它放进去,所以我忘记了它不是大炮。

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

so you call it like this (say you want to know how far you moved your finger):

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

float moveDistance = [self distanceBetween:touchStart and:touchEnd];

This is useful in movement functions as well for spot checking in a scrolling menu:

这对于移动函数以及滚动菜单中的点检很有用:

if([self distanceBetween:touchStart and:touchAt] > 20*scalePoints)
    isItATap = FALSE;

Set "isItATap" true in touchesBegan, put the above in touchesMoved, then you know the player moved their finger too far for the touch to be a "tap", so you can have it NOT select the object the player touched and instead scroll the object around.

在touchesBegan中设置“isItATap”true,将上面的设置为touchesMoved,然后你就知道玩家的手指移动得太远了,以至于触控不能成为“tap”,所以你可以让玩家不选择被触控对象,而是滚动对象。

As for scale, that should be based on whether or not you have retina display and what size of a device you're on (divide by 2 for retina display since a physical distance of 5 "points" on a regular screen as the user's finger feels it will come up as 10 "pixels" on a retina display screen, since each point is 4 pixels, so you'll wind up with a situation where the player has a very hard time tapping on retina display (which is a common oversight)

就规模而言,应该基于你是否有视网膜显示屏和设备大小你(除以2以来视网膜显示屏的物理距离5普通屏幕上的“点”作为用户的手指感觉它将出现10“像素”视网膜显示屏,因为每个点是4个像素,所以你会得情况下玩家对视网膜显示屏很难攻(这是一种常见的监督)

#3


18  

Sounds like you probably want the vector from p1 to p2 (or difference) rather than the distance.

听起来你可能想要的是从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}

#4


10  

Short Answer

CGPoint p1, p2; // Having two points
CGFloat distance = hypotf((p1.x-p2.x), (p1.y-p2.y));

Longer Explination

If you have two points p1 and p2 it is obviously easy to find the difference between their height and width (e.g. ABS(p1.x - p2.x)) but to find a true representation of their distance you really want the hypothenuse (H below).

如果你有两个点p1和p2,显然很容易找到它们的高度和宽度之间的差异(例如,ABS(p1)。但是要找到它们的距离的真实表示,你真的想要低强度(下面的H)。

 p1
  |\  
  | \  
  |  \ H
  |   \
  |    \
  |_ _ _\
         p2

Thankfully there is a built in macro for this: hypotf (or hypot for doubles):

值得庆幸的是,这里有一个内置的宏:

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

(original reference)

(原始引用)

#5


7  

In Swift, you can add an extension to CGPoint:

在Swift中,您可以向CGPoint添加一个扩展:

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

and use it like this:

然后像这样使用:

let distance = p1.distance(to: p2)

#6


2  

only this...

只有这样……

    float distance = ccpLength(ccpSub(p1,p2));

where p1 and p2 are objects of CGPoint

p1和p2是CGPoint的对象

#7


0  

Swift 4, Swift 3 solution

Swift 4, Swift 3解决方案

 extension CGPoint {
        static func distanceBetween(point p1: CGPoint,
                                    andPoint p2: CGPoint) -> CGFloat {
            return sqrt(pow((p2.x - p1.x), 2) + pow((p2.y - p1.y), 2))
        }
    }

#1


48  

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]

The distance is the variable distance.

距离是可变距离。

What is going on here:

这里发生了什么:

  1. So first off we make two points...
  2. 首先我们提出两点…
  3. Then we find the distance between x coordinates of the points.
  4. 然后求出点x坐标之间的距离。
  5. Now we find the distance between the y coordinates.
  6. 现在我们求出y坐标之间的距离。
  7. 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)
  8. 这些长度是三角形的两面,事实上他们是腿,找点时间,斜边这意味着后做一些数学rearragne c ^ 2 = ^ 2 + b ^ 2得到斜边等于√((xDist ^ 2)+(yDist ^ 2))。xDist ^ 2 =(xDist * xDist)。同样:yDist ^ 2 =(yDist * yDist)

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不能表示距离,距离没有x和y分量。它只是一个数字。

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

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

#2


21  

I've had to do this by hand 10,000 times so I wrote a function for it and stuck it in my personal library that I always dump in at the beginning of a new program so I forget it's not cannon.

我不得不手工做了10000次,所以我为它写了一个函数,并把它粘贴到我的个人库中,我总是在一个新程序开始的时候把它放进去,所以我忘记了它不是大炮。

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

so you call it like this (say you want to know how far you moved your finger):

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

float moveDistance = [self distanceBetween:touchStart and:touchEnd];

This is useful in movement functions as well for spot checking in a scrolling menu:

这对于移动函数以及滚动菜单中的点检很有用:

if([self distanceBetween:touchStart and:touchAt] > 20*scalePoints)
    isItATap = FALSE;

Set "isItATap" true in touchesBegan, put the above in touchesMoved, then you know the player moved their finger too far for the touch to be a "tap", so you can have it NOT select the object the player touched and instead scroll the object around.

在touchesBegan中设置“isItATap”true,将上面的设置为touchesMoved,然后你就知道玩家的手指移动得太远了,以至于触控不能成为“tap”,所以你可以让玩家不选择被触控对象,而是滚动对象。

As for scale, that should be based on whether or not you have retina display and what size of a device you're on (divide by 2 for retina display since a physical distance of 5 "points" on a regular screen as the user's finger feels it will come up as 10 "pixels" on a retina display screen, since each point is 4 pixels, so you'll wind up with a situation where the player has a very hard time tapping on retina display (which is a common oversight)

就规模而言,应该基于你是否有视网膜显示屏和设备大小你(除以2以来视网膜显示屏的物理距离5普通屏幕上的“点”作为用户的手指感觉它将出现10“像素”视网膜显示屏,因为每个点是4个像素,所以你会得情况下玩家对视网膜显示屏很难攻(这是一种常见的监督)

#3


18  

Sounds like you probably want the vector from p1 to p2 (or difference) rather than the distance.

听起来你可能想要的是从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}

#4


10  

Short Answer

CGPoint p1, p2; // Having two points
CGFloat distance = hypotf((p1.x-p2.x), (p1.y-p2.y));

Longer Explination

If you have two points p1 and p2 it is obviously easy to find the difference between their height and width (e.g. ABS(p1.x - p2.x)) but to find a true representation of their distance you really want the hypothenuse (H below).

如果你有两个点p1和p2,显然很容易找到它们的高度和宽度之间的差异(例如,ABS(p1)。但是要找到它们的距离的真实表示,你真的想要低强度(下面的H)。

 p1
  |\  
  | \  
  |  \ H
  |   \
  |    \
  |_ _ _\
         p2

Thankfully there is a built in macro for this: hypotf (or hypot for doubles):

值得庆幸的是,这里有一个内置的宏:

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

(original reference)

(原始引用)

#5


7  

In Swift, you can add an extension to CGPoint:

在Swift中,您可以向CGPoint添加一个扩展:

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

and use it like this:

然后像这样使用:

let distance = p1.distance(to: p2)

#6


2  

only this...

只有这样……

    float distance = ccpLength(ccpSub(p1,p2));

where p1 and p2 are objects of CGPoint

p1和p2是CGPoint的对象

#7


0  

Swift 4, Swift 3 solution

Swift 4, Swift 3解决方案

 extension CGPoint {
        static func distanceBetween(point p1: CGPoint,
                                    andPoint p2: CGPoint) -> CGFloat {
            return sqrt(pow((p2.x - p1.x), 2) + pow((p2.y - p1.y), 2))
        }
    }