In this example I’m assuming that no_1
and no_2
are within the range (0, 360) and the calculated difference should be unsigned.
func calculate_difference(no_1, no_2):
var angle = abs(no_1 - no_2)
if angle > 180.0:
return 360.0 - angle
return angle
So you just take the difference between the inputs to get an angle, and if the angle goes the long way around the circle, take its compliment.
If you want to have symmetry, so that 0 and 180 degrees are the same thing, just halve the values.
func calculate_difference(no_1, no_2):
var angle = abs(no_1 - no_2)
if angle > 90.0:
return 180.0 - angle
return angle
Forcing the angles to a specific range can be easily done with fposmod().
no_1 = fposmod(no_1, 180.0)
no_2 = fposmod(no_2, 180.0)