检测一个点与其他两点相比的位置
编辑我正在寻找实际的一两个衬里,它可以完成大量赞成票所建议的答案。
我在一个非常真实的软件中需要解决一个小问题,我正在寻找一种简单的方法来解决它。
我在屏幕上有两个固定点(它们是固定的,但我事先不知道它们的位置),但它们不在同一位置。这两个固定点形成一条假想线。现在我有了第三个点,它位于该线的“一侧”(它不能在线上)。用户可以抓取该点(用户实际上抓取了我通过其中心跟踪的对象,这是我感兴趣的点)并拖动它。但它无法“跨越”假想线。检测用户是否越过假想线的最简单方法是什么?
点可以是:
a[] = new int { 30, 50};
b[] = new int { 0, 100 };
c[] = new int { 20, 22 };
示例(与上面的点不对应):
/ a c / / / (c cannot be dragged here) b /
或者:
c ---b--------------c--- -- -- -- - - (c cannot be dragged here)
那么,如果 c 停留在直线的正确“一侧”,那么什么是容易检测的(我在这里画了线段,但它确实可以被认为是一条线)?
检测这一点的一种方法是获取目标点 d 并查看线段 (c,d) 是否与线 (a,b) 相交,但是是否有更简单的方法呢?我不能在这里做一些 2D 点积魔术,并且基本上用一两个衬垫来解决我的问题吗?
EDIT I'm looking for the actual one or two liner that does what the answer with a lot of upvote suggest.
I've got a little problem to solve in a very real software and I'm looking for an easy way to solve it.
I've got two fixed points on screen (they're fixed, but I don't know beforehand their position) that are not at the same location. These two fixed points form an imaginary line. Now I've got a third point that is "on one side" of that line (it cannot be on the line). The user can grab the point (the user actually grabs an object that I track by its center, which is the point I'm interested in) and drag it. But it cannot "cross" the imaginary line. What is the easiest way to detect if the user is crossing the imaginary line?
Points could be:
a[] = new int { 30, 50};
b[] = new int { 0, 100 };
c[] = new int { 20, 22 };
Examples (not corresponding to the points above):
/ a c / / / (c cannot be dragged here) b /
Or:
c ---b--------------c--- -- -- -- - - (c cannot be dragged here)
So what is an easy to detect if c is staying on the correct "side" of the line (I draw segments here, but it really can be thought of as a line)?
One way to detect this is to take the destination point d and see if segment (c,d) intersects with line (a,b), but isn't there an easier way? Can't I just do some 2D dot-product magic here and have basically a one or two liner solving my issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用叉积 AB x AC 的 Z 分量的符号(将 A 和 B 的 Z 分量隐式地视为 0)作为指示符,指示 C 点当前位于线段 AB 的哪一侧。当该符号发生变化时,阻力已经穿过或正在穿过线段 AB。
You can use the sign of the Z-component of the cross product AB x AC (taking A & B's Z-components implicitly as 0) as an indicator of which side of segment AB point C is currently on. When that sign changes, the drag has crossed or is crossing the segment AB.