Skip to content

Commit

Permalink
[Math] Added MathHelper.Clamp
Browse files Browse the repository at this point in the history
  • Loading branch information
thefiddler committed Jan 6, 2014
1 parent a9ab365 commit 88c57db
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Source/OpenTK/Math/MathHelper.cs
Expand Up @@ -288,6 +288,46 @@ public static void Swap(ref float a, ref float b)

#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
}
}

0 comments on commit 88c57db

Please sign in to comment.