通过添加两个CG点的距离来找到CG点
我有两个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用以下函数(将其放入您的
.h
文件之一):Use the following function (put it in one of your
.h
files):我建议使用以下不使用任何三角函数的方法。我想这比卢克曼的建议要快,但我没有测量时间。
factor = 1.0
表示结果点等于点to
。factor = 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.
factor = 1.0
means that the resulting point is equal to pointto
.factor = 2.0
means that the resulting point is as twice as far fromfrom
asto
is.@Lars 的答案对我来说是最好和最简单的。这是 Swift 4 版本:
如果您有当前距离,您可以对其进行添加和减去,或者通过计算完全更改它:
在我的例子中,我使用扩展程序有一个从 CGPoint 到 CGPoint 的距离计算器:
@Lars's answer was best and simplest for me. Here is the Swift 4 version:
If you have the current distance, you can add and subtract from it or change it altogether by calculating:
In my case, I had a distance calculator from CGPoint to CGPoint using an extension: