Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Clamps a value between a pair of minimum and maximum values
- /// </summary>
- /// <typeparam name="T">The value type of the object to clamp. Type must use IComparable interface</typeparam>
- /// <param name="val">The value to clamp</param>
- /// <param name="min">The minimum value returned</param>
- /// <param name="max">The maximum value returned</param>
- /// <returns>The clamped value</returns>
- public static T Clamp<T>(T val, T min, T max) where T : IComparable<T>
- {
- if (val.CompareTo(min) < 0) return min;
- else if (val.CompareTo(max) > 0) return max;
- else return val;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement