I need a function to clamp an angle (in degrees) into an arbitrary range [min,max]. Here are some examples:

Colored areas represent the actual range of angles.
- In image # 1, ang should be clamped to max (-90)
- In image # 2, ang should be clamped to mines (135)
- In image No. 3, ang should be clamped to mines (135)
This is what I have so far:
static float clamp_angle(float ang,float min,float max)
{
ang = normalize_angle(ang);
min = normalize_angle(min);
max = normalize_angle(max);
if(angle_in_range(ang,min,max) == false)
{
if(abs(get_angle_difference(ang,min)) < abs(get_angle_difference(ang,max))
ang = min;
else
ang = max;
}
return ang;
}
I miss the function angle_in_range( trueif the angle is within the range, otherwise false).
What would be the easiest way to determine if an angle is within a range or not?
source
share