I\'m working on a simple rotate routine which normalizes an objects rotation between 0 and 360 degrees. My C# code seems to be working but I\'m not entirely happy with it. Can a
This is one that normalizes to any range. Useful for normalizing between [-180,180], [0,180] or [0,360].
( it's in C++ though )
// Normalizes any number to an arbitrary range
// by assuming the range wraps around when going below min or above max
double normalize( const double value, const double start, const double end )
{
const double width = end - start ; //
const double offsetValue = value - start ; // value relative to 0
return ( offsetValue - ( floor( offsetValue / width ) * width ) ) + start ;
// + start to reset back to start of original range
}
For ints
// Normalizes any number to an arbitrary range
// by assuming the range wraps around when going below min or above max
int normalize( const int value, const int start, const int end )
{
const int width = end - start ; //
const int offsetValue = value - start ; // value relative to 0
return ( offsetValue - ( ( offsetValue / width ) * width ) ) + start ;
// + start to reset back to start of original range
}
So basically the same but without the floor. The version I personally use is a generic one that works for all numeric types and it also uses a redefined floor that does nothing in case of integral types.