Last active
December 17, 2015 08:59
-
-
Save rtanote/5584142 to your computer and use it in GitHub Desktop.
processingのconstrain関数をc#でも使えるように。ジェネリック&拡張メソッドにしている。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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