4

If we have a line segment (A.x, A.y, B.x, B.y) and a point (C.x, C.y), how am I able to tell whether the point is on the left or right, or top or bottom, of the line segment? I have tried solutions involving cross products from previous posts, but it led me to another problem. Using cross products made that the order of A and B matter. What other method is there so that the order of A and B does not matter?

Solution from previous posts:

Use the sign of the determinant of vectors (AB,AM), where M(X,Y) is the query point:

position = sign((Bx - Ax) * (Y - Ay) - (By - Ay) * (X - Ax))

It is 0 on the line, and +1 on one side, -1 on the other side.

Source: How to tell whether a point is to the right or left side of a line

Community
  • 1
  • 1
  • 1
    What about finding the equation of the line (y = mx + c) and substituting in x and y, then comparing the LHS and RHS to see which one is bigger? – Sweeper May 08 '20 at 09:53
  • This sounds a bit weird... left and right are constructs that rely on orientation. Your left becomes your right, if you rotate 180 degrees... so calculating, whether something is on the left without considering orientation (order of A and B) sounds wrong. If what you mean by left and right is further and closer on x-axis, then you can simply use the old method and swap A and B if B.x < A.x. – Šimon Kocúrek May 08 '20 at 09:57
  • @ŠimonKocúrek question also asks for top or bottom. So, for a horizontal line you'd get left/right = N/A and one of top/bottom. – VLAZ May 08 '20 at 09:58

1 Answers1

4

(I assumed a Cartesian coordinate system with the positive x and y pointing to the right and up respectively)

Given a line going through the points (a, b), (c, d), its equation is:

enter image description here

Now you have a point (e, f). If you substitute x=e, y=f, and they satisfy the equation, that means the point is on the line.

If the left side of the equation is bigger, that means your point is on the top of the line. If the right side of the equation is bigger, that means your point is below the line.

To figure out left/right, you need to consider the gradient as well. If the gradient is positive, being below the line means you are to the right of it. If the gradient is negative, being below the line means you are to the left of it. Vice versa.

Note that you need to handle vertical lines as a special case.

In Java,

double lhs = e;
double gradient = (b - d)/(a - c);
if (Double.isInfinite(gradient)) {
    // vertical line, compare only x coordinates
    if (e < a) {
        // on the left
    } else if (e > a) {
        // on the right
    } else {
        // on the line
    }
}
double rhs = (gradient * (e - a)) + b;
if (lhs > rhs) {
    if (gradient > 0) {
        // on the left
    } else if (gradient < 0) {
        // on the right
    } else {
        // on the top
    }
} else if (lhs < rhs) {
    if (gradient > 0) {
        // on the right
    } else if (gradient < 0) {
        // on the left
    } else {
        // below
    }
} else {
    // on the line
}

To prove that rearranging the order of the points doesn't change the result of this is quite simple. Changing the order of points produces this equation (a changes to c, b changes to d):

enter image description here

If you expand both the above equation and the one at the start, you will see that they are actually the same equation, which is:

enter image description here

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • I tried to recreate this in Desmos and it doesn't quite work [link](https://www.desmos.com/calculator/hrpm8mlzcx) –  May 08 '20 at 10:47
  • @PinHeadLarry Did you read the edit that I did a few minutes ago? I just realised the slope had to be taken into account too when figuring out left/right? – Sweeper May 08 '20 at 10:51
  • +1 Another way to think of the left/right question is to rearrange the equation of the line so that x is the independent variable; then, use this same method (for a given y, is my point's x less than or greater than the line's corresponding x) and that will tell you whether you are to the left or right. If you did this, then horizontal lines would be the special case! – Patrick87 May 08 '20 at 12:20