通过添加两个CG点的距离来找到CG点

发布于 2024-10-25 03:05:52 字数 528 浏览 1 评论 0原文

我有两个 CGPoints A 和 B。A 和 B 之间的距离可以通过 ccpDistance(A,B) 找到。

有了这些信息,我需要通过将小距离添加到 AB 的距离中,以编程方式找到 X 的 CGPoint(x,y)。 AB可以在水平、垂直、对角线或任何方向上形成连接线。目的是延长假想线并找到其末端的点。

iOS 版 Box2D 可以吗?如果是,那么如何?

          X            Y   X
         /               .  \ 
        /                  . \
       B                     .B
      /                        \
     /                          \
    /                            \
   /                              \
  A                                A

I have two CGPoints A and B. Distance between A and B can be found with ccpDistance(A,B).

With this information, I need to find programmatically CGPoint(x,y) of X by adding small distance into distance of A-B. A-B may can make a connecting line in horizontal, vertical, diagonal or any orientation. Purpose is to extend the imaginary line and find a point at the end of it.

Is it possible in Box2D for iOS? If Yes, then how?

          X            Y   X
         /               .  \ 
        /                  . \
       B                     .B
      /                        \
     /                          \
    /                            \
   /                              \
  A                                A

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

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

发布评论

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

评论(3

恍梦境° 2024-11-01 03:05:52

使用以下函数(将其放入您的 .h 文件之一):

static inline CGPoint
ccpLineExtend(const CGPoint v1, const CGPoint v2, const CGFloat extension)
{
    double angle = atan2(v2.y - v1.y, v2.x - v1.x);
    return ccp(v2.x + extension * cos(angle), v2.y + extension * sin(angle));
}

Use the following function (put it in one of your .h files):

static inline CGPoint
ccpLineExtend(const CGPoint v1, const CGPoint v2, const CGFloat extension)
{
    double angle = atan2(v2.y - v1.y, v2.x - v1.x);
    return ccp(v2.x + extension * cos(angle), v2.y + extension * sin(angle));
}
生死何惧 2024-11-01 03:05:52

我建议使用以下不使用任何三角函数的方法。我想这比卢克曼的建议要快,但我没有测量时间。

static inline CGPoint ccpLineExtend(const CGPoint from, const CGPoint to, const CGFloat factor)
{
    return CGPointMake(from.x + factor * (to.x - from.x), from.y + factor * (to.y - from.y));
}

factor = 1.0 表示结果点等于点 tofactor = 2.0 表示结果点距 from 的距离是 to 距离的两倍。

I suggest the following method which doesn't make use of any triangular function. I suppose it's faster than Lukman's suggestion but I didn't measure times.

static inline CGPoint ccpLineExtend(const CGPoint from, const CGPoint to, const CGFloat factor)
{
    return CGPointMake(from.x + factor * (to.x - from.x), from.y + factor * (to.y - from.y));
}

factor = 1.0 means that the resulting point is equal to point to. factor = 2.0 means that the resulting point is as twice as far from from as to is.

你的往事 2024-11-01 03:05:52

@Lars 的答案对我来说是最好和最简单的。这是 Swift 4 版本:

let newPoint = CGPoint(x: origin.x + factor * (end.x - origin.x), y: origin.y + factor * (end.y - origin.y))

如果您有当前距离,您可以对其进行添加和减去,或者通过计算完全更改它:

let factor = newDistance/currentDistance

在我的例子中,我使用扩展程序有一个从 CGPoint 到 CGPoint 的距离计算器:

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

@Lars's answer was best and simplest for me. Here is the Swift 4 version:

let newPoint = CGPoint(x: origin.x + factor * (end.x - origin.x), y: origin.y + factor * (end.y - origin.y))

If you have the current distance, you can add and subtract from it or change it altogether by calculating:

let factor = newDistance/currentDistance

In my case, I had a distance calculator from CGPoint to CGPoint using an extension:

extension CGPoint {
    func distance(to point: CGPoint) -> CGFloat {
        let x = self.x - point.x
        let y = self.y - point.y
        return sqrt(pow(x, 2) + pow(y, 2))
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文