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:
Now how do I know whether point B is on the left or the right side of point A? Answers in JavaScript highly appreciated.
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)
Edit: for coordinate system OX north, clockwise it is necessary to exchange cos and sin
cross = (B.x - A.x)*Math.cos(angle)-(B.y - A.y)*Math.sin(angle)
Positive value for right side, negative value for left side (or vice versa depending on your coordinate system orientation)
You have:
(A.x, A.y);A.angle;(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"
Let's break this up.
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:
It would be easier to find the right math if your 0°-angle would be 'east, 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 from 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)