I need to calculate which side of a point another point is on and I've been googling around but I have absolutely no idea what all those math equations mean or how they translate into code or JavaScript more specifically.
I have the following information:
- Point A: x, y and angle (direction which the point is going to)
- Point B: x and y
Now how do I know whether point B is on the left or the right side of point A? Answers in JavaScript highly appreciated.
CodePudding user response:
You have:
- a point A
(A.x, A.y)
; - a moving direction for A given as an angle
A.angle
; - a point B
(B.x, B.y)
.
You want to compare the moving direction of A with the direction of vector AB.
Coordinates of vector AB can be computed with a simple subtraction:
AB.x = B.x - A.x
AB.y = B.y - A.y
You can compute the angle corresponding to direction vector AB using atan2. Conveniently, that function is part of most programming languages' standard math library.
When using atan2, we have to be careful about the convention. In the comments, you specified that you wanted the north-clockwise convention. For other conventions, see Wikipedia: atan2 and conventions.
We also have to convert from radians to degrees, which can be done easily with the conversion factor 180 / pi
.
AB.angle = atan2(AB.x, AB.y) * 180 / pi
if AB.angle < 0:
AB.angle = AB.angle 360
Then all we have to do is check whether AB.angle is in interval [A.angle - 180°, A.angle] (left), or in interval [A.angle, A.angle 180°] (right), while being careful because all calculations are modulo 180°.
// assuming A.angle > 0 && A.angle < 360
if A.angle > 180:
if AB.angle > A.angle - 180 && AB.angle < A.angle:
return "Left"
else:
return "Right"
else: // A.angle < 180
if AB.angle > A.angle && AB.angle < A.angle 180:
return "Right"
else:
return "Left"
CodePudding user response:
Let's break this up.
- We've got a directed line L, going through point A in direction a (for angle).
- We've got a point B.
- And the question is: Is B left or right of L?
What we need is the angle of the line (against 'north', a.k.a. positive y-axis) that goes through A and B. This allows us to compare those angles:
- if the difference is in -180°..0° or 180°..360° (-pi..0 or pi..2pi in radians), B is to the left of L,
- if the difference is in -360°..-180° or 0°..180° (-2pi..-pi or 0..pi in radians), B is to the right of L.
It would be easier to find the right math if your 0°-angle would be 'west, counterclockwise' (as is mathematical convention), but we'll just swap x and y and we're good to go (they're already swapped in the following lines!).
We get that angle (let's call it b) from the points alone, but at first, it'll be in radians (not in degrees):
b_rad = Math.atan((A.x - B.x) / (A.y - B.y));
You did not specify whether the angle is in degrees or radians. Assuming degrees, the result needs to be converted to radians:
b_deg = b_rad * (180 / Math.PI);
Now you only need to take the difference:
delta = a_deg - b_deg;
and use that delta in the comparison I outlined above.
(If I didn't think right, this math gives the opposite results of what is needed -- in this case, you need to swap a_deg and b_deg in the delta calculation)
CodePudding user response:
Consider sign of expression (if angle
is in degrees, multiply it by Math.PI/180
to get radians)
cross = (B.x - A.x)*Math.sin(angle)-(B.y - A.y)*Math.cos(angle)
Positive value for right side, negative value for left side (or vice versa depending on your coordinate system orientation)
CodePudding user response:
if pointB.x < pointA.x
it's on the left side.
if pointB.x > pointA.x
it's on the right side.
if they are equal, you can't really say who's on the left or on the right.
(that is, of course assuming your coordinate space goes left to right on the x axis)