Skip to main content
GameDev.net gamedev.net
🔒 Locked

Simple Point-Line question

Started by Dookie Mar 31, 2007 at 11:12 AM 4 replies 1.1k views
Original Post
Dookie
Dookie
Hello, here's an easy one for ya! This is a 2D question, and here's the scenario: I have a line, L1, passing through points P2 and P3. I also have a point, P1. How do I tell what side of the line P1 is at? I'd like to tell whether P1 is at one side or the other of L1. I really want to know how to do this so I can better understand how more advanced 2D collision-detection routines work. Thanks in advance for the help, I really appreciate it.
"The crows seemed to be calling his name, thought Caw"
Iarus
Iarus
you could do a cross product of vP2P3 x vP2P1 and see if the normal goes up or down
normal points up -> P1 is on the left of L1
normal points down -> P1 is on the right of L1

there might be other, better ways, but this one looks good enough
Bessi
Bessi
You could find the cartesian equation of the line that is ax + by + c = 0. Then you define the function f(x,y) = ax+by+c and if f(x,y) > 0 then the point lies "above" the line, if f(x,y) < 0 then the point lies "below" the line (and ofcourse if f(x,y) = 0 then the point lies on the line).
Bessi
oliii
oliii
E = P3 - P2;
D = P1 - P2;
N = Vector(-E.y, E.x);
side = D.DotProduct(N);
Everything is better with Metal.
Dookie
Dookie
Thanks for all the replies! I think I'm getting it, but I might just be getting myself more confused. I'm a math-tard. It looks like oliii's math may be the easiest for me to understand, but there are parts I don't quite get...

Here's what I've got so far (I think)...

E = P3 - P2;
or
Ex = P3x - P2x;
Ey = P3y - P2y;

D = P1 - P2;
or
Dx = P1x - P2x;
Dy = P1y - P2y;

N = Vector(-E.y, E.x);
or
Nx = -Ey;
Ny = Ex;

side = D.DotProduct(N);
or
side = D.N;
or
side = (Dx * Nx) + (Dy * Ny);

Is that correct? And if so, do I need to normalize 'Vector' or would the raw (-Ey, Ex) result work?

Thanks for the clarification!
"The crows seemed to be calling his name, thought Caw"
Kwizatz
Kwizatz
Quote:
Original post by Dookie
Thanks for all the replies! I think I'm getting it, but I might just be getting myself more confused. I'm a math-tard. It looks like oliii's math may be the easiest for me to understand, but there are parts I don't quite get...

Here's what I've got so far (I think)...

E = P3 - P2;
or
Ex = P3x - P2x;
Ey = P3y - P2y;

D = P1 - P2;
or
Dx = P1x - P2x;
Dy = P1y - P2y;

N = Vector(-E.y, E.x);
or
Nx = -Ey;
Ny = Ex;

side = D.DotProduct(N);
or
side = D.N;
or
side = (Dx * Nx) + (Dy * Ny);

Is that correct? And if so, do I need to normalize 'Vector' or would the raw (-Ey, Ex) result work?

Thanks for the clarification!


I think thats pretty much correct, you dont really need to normalize N since what gives you the side is the sign of the 'side' variable (negative is on one side, positive the other) rather than its value.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.