Detecting one point's location compared to two other points.

Posted by WizardOfOdds on Stack Overflow See other posts from Stack Overflow or by WizardOfOdds
Published on 2010-03-28T12:08:28Z Indexed on 2010/03/28 12:13 UTC
Read the original article Hit count: 246

Filed under:
|
|

Hi all,

you can check my profile, this is not homework.

I've got an interesting 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 grab an object, whose 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?

Example:

     a
c   /
   /    (c cannot be dragged here) 
  /
b

Or:

            c

   b --------------    a

           (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?

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about 2d