From 271ea9eefaaff1447796b4262f877a8dfb2def2b Mon Sep 17 00:00:00 2001 From: nathan Date: Sun, 30 Jul 2017 22:21:11 +0200 Subject: [PATCH] Added clamp. --- src/dorkbox/util/MathUtil.java | 40 ++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/dorkbox/util/MathUtil.java b/src/dorkbox/util/MathUtil.java index 6b8d868..99588f0 100644 --- a/src/dorkbox/util/MathUtil.java +++ b/src/dorkbox/util/MathUtil.java @@ -258,4 +258,44 @@ class MathUtil { boolean intersectRange(double ax1, double ax2, double bx1, double bx2) { return Math.max(ax1, bx1) <= Math.min(ax2, bx2); } + + /** + * Clamps a value to min/max values, where it cannot exceed either. + * + * @return the new value, clamped + */ + public static + int clamp(int value, int min, int max) { + return Math.max(min, Math.min(max, value)); + } + + /** + * Clamps a value to min/max values, where it cannot exceed either. + * + * @return the new value, clamped + */ + public static + long clamp(long value, long min, long max) { + return Math.max(min, Math.min(max, value)); + } + + /** + * Clamps a value to min/max values, where it cannot exceed either. + * + * @return the new value, clamped + */ + public static + float clamp(float value, float min, float max) { + return Math.max(min, Math.min(max, value)); + } + + /** + * Clamps a value to min/max values, where it cannot exceed either. + * + * @return the new value, clamped + */ + public static + double clamp(double value, double min, double max) { + return Math.max(min, Math.min(max, value)); + } }