计算两个CGPoint之间的距离

发布于 2024-11-16 08:18:24 字数 148 浏览 4 评论 0原文

我需要计算两个 CGPoint 之间的距离。我看到这个,但我不明白它。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

情何以堪。 2024-11-23 08:18:24

好吧,你也提到的东西完整的代码在哪里:

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)

你不能真正让 CGPoint 成为距离,距离没有 x 和 y 分量。这只是 1 个数字。

如果您认为 CGPoint 是一种测量单位(例如英尺是一种测量单位),那么事实并非如此。

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. 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)

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

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

无边思念无边月 2024-11-23 08:18:24

简短答案

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

较长的解释

如果您有两个点 p1p2,显然很容易找到它们的 heightwidth< 之间的差异/code> (例如 ABS(p1.x - p2.x)),但要找到它们距离的真实表示,您确实需要假设(下面的 H)。

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

值得庆幸的是,有一个内置宏:hypotf(或hypot for doubles):(

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

原始参考

Short Answer

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

Longer Explanation

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
  |\  
  | \  
  |  \ 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)

李白 2024-11-23 08:18:24

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

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

并像这样使用它:

let distance = p1.distance(to: p2)

In Swift, you can add an extension to 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)
梦里兽 2024-11-23 08:18:24

我必须手动执行此操作 10,000 次,所以我为它编写了一个函数并将其保存在我的个人库中,我总是在新程序开始时转储该库,所以我忘记了它不是大炮。

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

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

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

这对于移动功能以及滚动菜单中的点检查很有用:

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

在touchesBegan中设置“isItATap”true,将以上内容放入touchesMoved中,那么您就知道玩家将手指移动得太远,触摸无法成为“轻击”,因此您可以让它不选择玩家触摸的对象,而是滚动该对象。

至于比例,这应该基于您是否有视网膜显示屏以及您所使用的设备尺寸(视网膜显示屏除以 2,因为在常规屏幕上,用户手指的物理距离为 5 个“点”)感觉它会在视网膜显示屏上显示为 10 个“像素”,因为每个点都是 4 个像素,所以您最终会遇到玩家很难点击视网膜显示屏的情况(这是一个常见的疏忽) )

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.

- (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.

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)

堇色安年 2024-11-23 08:18:24

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

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

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

const CGPoint diff = {p2.x - p1.x, p2.y - p1.y} // == (CGPoint){500, 300}
南七夏 2024-11-23 08:18:24

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

extension CGPoint {

    func distance(from point: CGPoint) -> CGFloat {
        return hypot(point.x - x, point.y - y)
    }
}

In Apple's sample projects, they use hypot. This returns hypothenuse (distance) between two points as explained in this answer.

extension CGPoint {

    func distance(from point: CGPoint) -> CGFloat {
        return hypot(point.x - x, point.y - y)
    }
}
你如我软肋 2024-11-23 08:18:24

只有这个...

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

其中 p1 和 p2 是 CGPoint 的对象

only this...

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

where p1 and p2 are objects of CGPoint

江挽川 2024-11-23 08:18:24
extension CGPoint {
    func magnitude() -> CGFloat {
        return sqrt(x * x + y * y)
    }
    
    func distance(to: CGPoint) -> CGFloat {
        return CGPoint(x: to.x - x, y: to.y - y).magnitude()
    }
}
extension CGPoint {
    func magnitude() -> CGFloat {
        return sqrt(x * x + y * y)
    }
    
    func distance(to: CGPoint) -> CGFloat {
        return CGPoint(x: to.x - x, y: to.y - y).magnitude()
    }
}
清风不识月 2024-11-23 08:18:24

斯威夫特 4、斯威夫特 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))
        }
    }

Swift 4, Swift 3 solution

 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))
        }
    }
美胚控场 2024-11-23 08:18:24

使用 Vision api 到另一个点的欧几里德距离

iOS 14开始。


import Vision

extension CGPoint {
        
    public func distance(to point: CGPoint) -> Double {
        VNPoint(location: self).distance(VNPoint(location: point))
    }
}


print(CGPoint(x: 1, y: 1).distance(to: .zero)) // 1.4142135623730951

Euclidean distance to another point with Vision api.

Starting from iOS 14.


import Vision

extension CGPoint {
        
    public func distance(to point: CGPoint) -> Double {
        VNPoint(location: self).distance(VNPoint(location: point))
    }
}


print(CGPoint(x: 1, y: 1).distance(to: .zero)) // 1.4142135623730951

一抹微笑 2024-11-23 08:18:24

我扩展了上面的函数来计算两个 CGRect 之间的距离。我通过计算两个 CGRect 的角之间的距离来计算它,然后返回最小的距离。我从以下位置复制了计算两条线之间交点的函数:

func distanceBetweenRectangles(r1: CGRect, r2: CGRect) -> CGFloat { // returns distance between boundaries of two rectangles or -1 if they intersect
    let cornerPointsR1: [CGPoint] = [
        CGPoint(x: r1.minX, y: r1.minY),CGPoint(x: r1.minX, y: r1.maxY),CGPoint(x: r1.maxX, y: r1.minY),CGPoint(x: r1.maxX, y: r1.maxY)]
    let cornerPointsR2: [CGPoint] = [
        CGPoint(x: r2.minX, y: r2.minY),CGPoint(x: r2.minX, y: r2.maxY),CGPoint(x: r2.maxX, y: r2.minY),CGPoint(x: r2.maxX, y: r2.maxY)]
    for i in 0..<cornerPointsR1.count {
        if (r2.contains(cornerPointsR1[i])) {
            return -1
        }
    }        
    var distances: [CGFloat] = []
    for i in 0..<cornerPointsR1.count {
        for j in 0..<cornerPointsR2.count {
            distances.append(distanceBetweenPoints(p1: cornerPointsR1[i], p2: cornerPointsR2[j]))
        }
    }
    distances.sort()
    return distances[0]
}

I extended above function to count distance between two CGRects. I count it by counting distance between coners of both CGRects and then returning the smallest distance. I copied function counting intersection point between two lines from:

func distanceBetweenRectangles(r1: CGRect, r2: CGRect) -> CGFloat { // returns distance between boundaries of two rectangles or -1 if they intersect
    let cornerPointsR1: [CGPoint] = [
        CGPoint(x: r1.minX, y: r1.minY),CGPoint(x: r1.minX, y: r1.maxY),CGPoint(x: r1.maxX, y: r1.minY),CGPoint(x: r1.maxX, y: r1.maxY)]
    let cornerPointsR2: [CGPoint] = [
        CGPoint(x: r2.minX, y: r2.minY),CGPoint(x: r2.minX, y: r2.maxY),CGPoint(x: r2.maxX, y: r2.minY),CGPoint(x: r2.maxX, y: r2.maxY)]
    for i in 0..<cornerPointsR1.count {
        if (r2.contains(cornerPointsR1[i])) {
            return -1
        }
    }        
    var distances: [CGFloat] = []
    for i in 0..<cornerPointsR1.count {
        for j in 0..<cornerPointsR2.count {
            distances.append(distanceBetweenPoints(p1: cornerPointsR1[i], p2: cornerPointsR2[j]))
        }
    }
    distances.sort()
    return distances[0]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文