java - Is angle to the left or right of second angle -


i need know if angle on right or on left of source angle.

i tried subtract angles , absolute value of them, range -180 180 meaning when go 180 , go on -180 give me opposite answer.

if wondering for, java game developing there tank turret controlled mouse.

it depends on whether angles specify rotation in clockwise or anti-clockwise direction. if looking in direction of tank turret, object on right if need rotate turret clockwise point @ possible, , on left if need rotate anti-clockwise.

obviously, can rotate in opposite direction go "the long way around": if object 10 degrees right can either rotate 10 degrees clockwise or 350 degrees anti-clockwise point @ it. let's consider short way around, assuming angles specified in clockwise direction:

// returns 1 if otherangle right of sourceangle, //         0 if angles identical //         -1 if otherangle left of sourceangle int compareangles(float sourceangle, float otherangle) {     // sourceangle , otherangle should in range -180 180     float difference = otherangle - sourceangle;      if(difference < -180.0f)         difference += 360.0f;     if(difference > 180.0f)         difference -= 360.0f;      if(difference > 0.0f)         return 1;     if(difference < 0.0f)         return -1;      return 0; } 

after subtracting angles, result can in range of -360 (-180 minus 180) 360 (180 minus -180). can bring them range of -180 180 adding or subtracting 360 degrees, compare 0 , return result.

angles absolute value between 180 , 360 correspond "long way around" rotations, , adding or subtracting 360 converts them "short way around". example -350 degrees clockwise long way around (i.e. 350 degrees anti-clockwise) plus 360 equates 10 degrees clockwise short way around.

if angles specified in anti-clockwise direction, meaning of return value reversed (1 means left, -1 means right)


Comments