//http://isotopescreencapture.codeplex.com/
//The MIT License (MIT)
namespace Isotope.Math
{
public static class MathUtil
{
///
/// This is a variant of mod that wraps the mod result to avoid negative results. this is what Python's mod operator does
///
///
///
///
private static double mod_wrap_angle(double x, double y)
{
if (y == 0)
{
throw new System.DivideByZeroException();
}
double r = x%y;
if (r > 0 && y < 0)
{
r = r + y;
}
else if (r < 0 && y > 0)
{
r = r + y;
}
return r;
}
///
/// wraps a number around so that it always fits between 0.0 and 1.0. negative numbers will wrap around to the correct positive number
///
///
/// if the input number is already in the range, no change will occur
///
/// input value
/// the wrapped number
public static double WrapAngle_0_1(double v)
{
const double min = 0.0;
const double max = 1.0;
if (IsInRange(v, min, max))
{
// the number is already in the range so do nothing
return v;
}
return mod_wrap_angle(v, max);
}
///
/// Checks if a value is in a range (inclusive)
///
///
///
///
///
public static bool IsInRange(double val, double min, double max)
{
return ((min <= val) && (val <= max));
}
///
/// Checks if a value is in the range 0.0 to 1.0 inclusive
///
///
///
public static bool IsInRange_0_1(double val)
{
return IsInRange(val, 0.0, 1.0);
}
}
}