1

I need to do this for AI and also range calculation over a curved surface.

I have two angles, a1 and a2, which can be in the range from 0 to 359. I need to calculate the difference between them, and it has to be the inside angle (smallest angle), not the "reflexive" outside angle. And it has to preserve the direction from a1 to a2, which will be positive if going counterclockwise and negative if going clockwise.

Examples of what the function should yield:

scr_ang_diff(30, 50) == 20

scr_ang_diff(80, 310) == -130 // NOT 230 !!!

scr_ang_diff(130, 130) == 0

Now I could do this with test cases, but it seems there should be a way to do this in one mathematical expression. I can no longer use the simple min of abs of the two differences (without test cases) because that doesn't preserve direction.

EDIT: in order to differentiate between this post and this, I've decided to make it mandatory to preserve direction between a1 and a2.

Super bonus points if a1 and a2 can have range -359 to +359.

NOTE: yes efficiency matters. That doesn't mean you can't use trig functions, it just means I'll pick the least complex solution.

DrZ214
  • 348
  • 2
  • 12
  • scr_ang_diff(80, 310) should equal 130 I think – Ryan Aug 29 '15 at 22:24
  • @Ryan Yep, fixed. – DrZ214 Aug 29 '15 at 22:55
  • @ratchetfreak No. For the super bonus points, I wanted negative angles from 0 to -359 to be compatible with positive angles from 0 to +359, so I just put the range as -359 to +359 to include "everything" possible. – DrZ214 Aug 29 '15 at 23:01

0 Answers0