Definition of "reference angle":
An angle ϑr in the interval [0, 2π) corresponding to an angle ϑ outside of this interval, satisfying the conditions
sin(ϑ) = sin(ϑr) and cos(ϑ) = cos(ϑr).
For example (thinking in degrees for simplicity):
ref(390o) = 30o
ref(360o) = 0o
ref(-40o) = 320o
ref(540o) = 180o
An example code for finding reference angle is:
double FindReferenceAngle(double Angle)
{
const double TWOPI = 2.0 * 3.1415926535897932384626433832795;
while(true)
{
if (Angle >= TWOPI)
{
Angle -= TWOPI;
}
else if (Angle < 0)
{
Angle += TWOPI;
}
else
{
return Angle;
}
}
}
However, I don't think that this code is optimal. For instance, if the user calls it with a very large angle value (e.g.; FindReferenceAngle(1e10)
) it will take quite a time in the while loop.
Is there any other more efficient algorithm to find reference angle, like a built in standard C++ library routine?
This should do it.
double FindReferenceAngle(double Angle)
{
const double TWOPI = 2.0 * 3.1415926535897932384626433832795;
return fmod(Angle, TWOPI);
}
Though it would probably be a good idea to globalize the definition of pi and put it in a header somewhere, or perhaps this:
#define _USE_MATH_DEFINES
#ifdef M_PI
#define TWOPI 2*M_PI
#else
#define TWOPI 2.0*3.1415926535897932384626433832795
#endif