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"