if you have a look at the angular circle, you will see that there are 2 opposite "angles" that corresponds to your "average".
So both 185° and 5° are correct.
But you mentionned the closer average. So in that case, you may choose the angle that is closer.
Usually, the "average" of angles concerns the counterclockwise direction. The "average" is not the same if you switch your two angles (or if you use the clockwise direction).
For example, with a=20°
and b=350°
, you are looking for the angle that comes after a
and before b
in the counterclockwise direction, 185°
is the answer. If you are looking for the angle that comes before a
and after b
in the counterclockwise direction (or after a
and before b
in the counterclock wise direction), 5°
is the answer.
The answer of this post is the right way to do.
So the pseudo-code for the solution is
if (a+180)mod 360 == b then
return (a+b)/2 mod 360 and ((a+b)/2 mod 360) + 180 (they are both the solution, so you may choose one depending if you prefer counterclockwise or clockwise direction)
else
return arctan( (sin(a)+sin(b)) / (cos(a)+cos(b) )