Download

How to check if a point is in the left or right of line ?

If there’s a Point FVector2D P(100, 50), Line L (FVector2D A - FVector2D B), how to check if Point P is in the left or right(top or bottom) of line ?

Does anyone know how to solve this issue?

To discern left from right we need the concept of ‘forward’, so I’m going to assume that line L can be thought of as a forward direction of some description, in your example the direction of line L would be thought of as representing a location B facing forward towards location A.

With that out of the way, we have to assume that the magnitude of L is > 0.

Make another line L1 = P - B (that’s your point P and your original B location).
The next step requires 3D vectors, so L and L1 need to be converted to FVectors, with the Z component = 0.
Now take the cross product of these two lines, FVector::CrossProduct(L, L1).

The result is a new FVector L2, and you basically want to look at the Z component of it. If it is greater than 0, then P is to the left of L. Conversely, if it is less than 0, then P is to the right of L. And if it is exactly 0, then P was actually right on top of the line L, which makes the idea of left/right undefined.

EDIT: Also note that I haven’t actually implemented or tested this, so it’s possible that left and right should be switched around. Should be easy enough to test though.

Thx so much! Your suggestion is very careful. Have a nice day!

I think you could do a dot product of two unit vectors(direction) to check if they are in the same direction. Negative if they arent, postive if they are. You might need to do some tests on these as i am a math idiot. Watch this video around 1.46.

https://youtube.com/watch?v=h0NJK4mEIJU

Dot product alone cannot tell the difference between left and right, only indicate to which degree two vectors are parallel/antiparallel.