I have a list of angles (in radians) in the range [-pi, pi]
. The angles can be assumed to ordered as follows:
- every increasing index in the list will be counter-clockwise to the previous
- there are no duplicates in the list
- the values in the list will never surpass a full
2*pi
rotation from the first value in the list
Now given a random angle in the [-pi, pi]
range, I want to determine which two of the angles in the list the random angle is between. Example usage of this is_between
function would be like so:
# doesn't work for angle == pi!
def is_between(angle, angles):
for i in range(len(angles)):
min = angles[i]
max = angles[(i + 1) % len(angles)]
if min <= angle <= max:
return min, max
angles = [-pi/6, pi/4, 5*pi/6, -2*pi/3]
bounds = is_between(pi, angles)
print(bounds) # should print (5*pi/6, -2*pi/3)
My initial though would be to just iterate through the list, picking two adjacent angles, and check if the provided angle is between the two values, but this doesn't work in cases near pi
. I've seen a solution similar to this in degrees, but I would like a solution that doesn't involve converting the units for the entire angles
list as a prerequisite step.