I would like to make a function that, given an angle and a range, obtains an angle that doesn't fall in the ranges $[0º, ±range]$ and $[180º, ±range]$.
I think that the following picture better explains the problem:
If the input angle falls inside the red area, I need to return the nearest angle from the green area.
I need to support both clock- and counterclockwise (negative angles).
The following method does the trick for a range of 30º, but I'm sure that there is a better solution using trigonometric functions:
internal static double GetAngleOutOfRange(double angle, double range)
{
if (angle >= 0 && angle <= 30)
return 30;
if (angle < 0 && angle >= -30)
return -30;
if (angle <= -150 && angle >= -180)
return -150;
if (angle >= 150 && angle <= 180)
return 150;
if (angle <= -180 && angle >= -210)
return -210;
if (angle >= 180 && angle <= 210)
return 210;
if (angle <= -330 && angle >= -360)
return -330;
if (angle >= 330 && angle <= 360)
return 330;
return angle;
}
Thanks for your help.

Working with sin and cos is indeed easier
|x| is $|x|$ and
cond ? a : bgivesaifcondis true,botherwise.