Given 2 angles in the range -PI -> PI around a coordinate, what is the value of the smallest of the 2 angles between them?
Taking into account that the difference between PI and -PI is not 2 PI but zero
Given 2 angles in the range -PI -> PI around a coordinate, what is the value of the smallest of the 2 angles between them?
Taking into account that the difference between PI and -PI is not 2 PI but zero
If your two angles are x and y, then one of the angles between them is abs(x - y). The other angle is (2 * PI) - abs(x - y). So the value of the smallest of the 2 angles is:
min((2 * PI) - abs(x - y), abs(x - y))
x is the target angle. y is the source or starting angle:
atan2(sin(x-y), cos(x-y))
It returns the signed delta angle. Note that depending on your API the order of the parameters for the atan2() function might be different.
I rise to the challenge of providing the signed answer:
def f(x,y):
import math
return min(y-x, y-x+2*math.pi, y-x-2*math.pi, key=abs)