[Math] Added MathHelper.Clamp

This commit is contained in:
Stefanos A. 2014-01-06 01:52:08 +01:00
parent a9ab3650da
commit 88c57db5b6

View file

@ -288,6 +288,46 @@ namespace OpenTK
#endregion
#region Clamp
/// <summary>
/// Clamps a number between a minimum and a maximum.
/// </summary>
/// <param name="n">The number to clamp.</param>
/// <param name="min">The minimum allowed value.</param>
/// <param name="max">The maximum allowed value.</param>
/// <returns>min, if n is lower than min; max, if n is higher than max; n otherwise.</returns>
public static int Clamp(int n, int min, int max)
{
return Math.Max(Math.Min(n, max), min);
}
/// <summary>
/// Clamps a number between a minimum and a maximum.
/// </summary>
/// <param name="n">The number to clamp.</param>
/// <param name="min">The minimum allowed value.</param>
/// <param name="max">The maximum allowed value.</param>
/// <returns>min, if n is lower than min; max, if n is higher than max; n otherwise.</returns>
public static float Clamp(float n, float min, float max)
{
return Math.Max(Math.Min(n, max), min);
}
/// <summary>
/// Clamps a number between a minimum and a maximum.
/// </summary>
/// <param name="n">The number to clamp.</param>
/// <param name="min">The minimum allowed value.</param>
/// <param name="max">The maximum allowed value.</param>
/// <returns>min, if n is lower than min; max, if n is higher than max; n otherwise.</returns>
public static double Clamp(double n, double min, double max)
{
return Math.Max(Math.Min(n, max), min);
}
#endregion
#endregion
}
}