tags:

views:

75

answers:

1

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?

+8  A: 

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.

Drew Hall
@Drew Hall: oh ok, that's what I was thinking when I wrote about some dot-product magic, so it's a bit like detection of hidden 3D surface when working with convex 3D objects, except that here I take the Z to be zero.
Webinator