I have vectors on a canvas which display a heading.
I also calculated the current heading with which the user is moving his mouse.
When he moves over vectors, i only want to select those that match the heading of the users mouse movements.
e.g. consider vector 1 has 180 degree heading
the user moves his mouse and has an approx heading of 170.
Since the user cannot exactly match the vectors heading, i want to add a margin.
E.g. is heading of vector between (170 – 45) and (170 + 45) ?
The problem arises when the vector has a heading e.g. 350 and the users mouse has a heading of 10 – that would be between (10-45) and (10+45) – now obviously -35 is not good so i add 360 degrees again, in this case 325 < 350 but 350 is not > 55
I obviously need some deeper understanding how to calculate this case.
tldr:
Given degree X, how to measure if it is between degree A and B if you imagine a circle.
Use the difference. Subtract the actual heading from your desired heading. While the answer is
<=-180or>180, add or subtract360as appropriate. Then compare the result to your error band (in your example>-45, <45)