开发者

Detecting one point's location compared to two other points

开发者 https://www.devze.com 2022-12-24 17:44 出处:网络
EDIT I\'m looking for the actual one or two liner that does what the answer with a lot of upvote suggest.

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消