I'm having an extremely difficult time getting heading calculations correctly.
This is quite simple once you realize that headings first need to be normalized to +/- 180 degrees (as opposed to 0-360). Shortest turn is then the normalized difference between the new and current heading. Direction to turn is left for a negative difference and right otherwise.
Current heading: 270 (normalized to -90)
New heading: 100 (normalized to 100)
Turn = 100 – (-90) = 190 (normalized to -170)
Shortest turn to reach 100 from 270 is then to turn 170 degrees left.
To convert from 0-360 to +/-180 can be done as follows:
While (heading > 180) heading -= 360
While (heading < - 180) heading += 360