Sign in to follow this  
SuperNerd

Determining if a point is behind a line

Recommended Posts

I need to know witch side of the line a point is on. The lines arent perfectly horizontal or vertical either. The lines would be something like 2X - 3Y = 10. I understand the concept of making the = sign a < or > sign. It's just my line is defined as a point and an angle. So if my line was at point (3,3) and has an angle of (in degrees) 120 or something like that. Witch side of the like is point (5,9)

Share this post


Link to post
Share on other sites
Which side compared to what? Compared to the origin?

It seems that all you need is a way to transform your line representation (X,Y,phi) into a line equation (aX+bY=C). This is very easy if you realize that the ascent in y is (-sin phi) and the ascent in X is (cos phi) assuming 0 means right along the X axis, and clockwise rotations.

Or, to put it another way: you know that the line passes through (X,Y) and through (X+cos phi,Y-sin phi); given these two points you can easily derive the line equation, and turn it into an equality just like you say you want.

Share this post


Link to post
Share on other sites
Quote:
Original post by SuperNerd
I need to know witch side of the line a point is on. The lines arent perfectly horizontal or vertical either. The lines would be something like 2X - 3Y = 10. I understand the concept of making the = sign a < or > sign. It's just my line is defined as a point and an angle. So if my line was at point (3,3) and has an angle of (in degrees) 120 or something like that. Witch side of the like is point (5,9)


What exactly do you mean by which side of the line? For example, if your line is not vertical you could say "is my point above or below the line?" in which case you would do the following steps:

- substitute the x coordinate of your point into your line equation

- if the y coordinate of your point is greater than the value of y obtained from the line equation from the previous step, your point is above the line or else it is below.

Share this post


Link to post
Share on other sites
Here's one way to do it. Let's call the origin of your line O and the direction D. If you're representing the direction with an angle 'a', then D might be:

x = cos(a), y = sin(a)

In 2D, the vector perpendicular to a vector x, y is either -y, x or y, -x. Either form will work, but they will point in opposite directions. So imagine we have a vector function Perp() which returns this perpendicular vector.

We now have all the tools we need. Let's say we have a point P and a line represented by O and D, and we want to know which side of the line P is on:



Vector2 diff = P - O;
Vector2 perp = D.Perp();
float dot = diff.Dot(perp);

if (dot < 0.0f)
{
// Point is one one side
}
else if (dot > 0.0f)
{
// Point is on the other side
}
else
{
// Point is on the line
}





(You can also use an epsilon for checking whether the point is on the line.)

Note that if D is unit length (which it will be if constructed as above), D.Dot(P - O) will give the actual perpendicular distance from the point to the line. In fact, rather than representing the line as O and D, it could be represented by a normal and a distance, much as we represent a plane in 3D. Anyway, that's another way you could look at it.

'Hope some of that was helpful.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this