Skip to content

Instantly share code, notes, and snippets.

@rtanote
Last active December 17, 2015 08:59
Show Gist options
  • Save rtanote/5584142 to your computer and use it in GitHub Desktop.
Save rtanote/5584142 to your computer and use it in GitHub Desktop.
processingのconstrain関数をc#でも使えるように。ジェネリック&拡張メソッドにしている。
public static class MiscUtils
{
/// <summary>
/// Constrains a value to not exceed a maximum and minimum value
/// </summary>
/// <typeparam name="Type">IComparable-implementing type</typeparam>
/// <param name="value">the value to constrain</param>
/// <param name="min">minimum limit</param>
/// <param name="max">maximum limit</param>
/// <returns>constrained value</returns>
public static Type Constrain<Type>(this Type value, Type min, Type max) where Type : IComparable {
value = value.CompareTo(max) > 0 ? max : value;
value = value.CompareTo(min) < 0 ? min : value;
return value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment