diff options
| author | Zak Cohen <zakcohen@google.com> | 2019-02-11 14:54:56 -0800 |
|---|---|---|
| committer | Zak Cohen <zakcohen@google.com> | 2019-02-11 15:01:52 -0800 |
| commit | c730de9118badc1bc45dbacbb7415f59d405a47e (patch) | |
| tree | 5cd6f017f1377577dc1780fd29958a8fcb4b95de /core/java/android/util/MathUtils.java | |
| parent | 6ad5f0d3edc54abd94f0d4174972512bc91140ea (diff) | |
Adds lerpInv, saturate, lerpInvSat and constrainedMap to MathUtils.
Change-Id: I525c9c691cb8f3eb61022e98b45997981c067018
Test: part of bigger change.
Diffstat (limited to 'core/java/android/util/MathUtils.java')
| -rw-r--r-- | core/java/android/util/MathUtils.java | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/core/java/android/util/MathUtils.java b/core/java/android/util/MathUtils.java index 56558d04e50d..0eeef70a79a6 100644 --- a/core/java/android/util/MathUtils.java +++ b/core/java/android/util/MathUtils.java @@ -166,6 +166,26 @@ public final class MathUtils { } /** + * Returns the interpolation scalar (s) that satisfies the equation: {@code value = }{@link + * #lerp}{@code (a, b, s)} + * + * <p>If {@code a == b}, then this function will return 0. + */ + public static float lerpInv(float a, float b, float value) { + return a != b ? ((value - a) / (b - a)) : 0.0f; + } + + /** Returns the single argument constrained between [0.0, 1.0]. */ + public static float saturate(float value) { + return constrain(value, 0.0f, 1.0f); + } + + /** Returns the saturated (constrained between [0, 1]) result of {@link #lerpInv}. */ + public static float lerpInvSat(float a, float b, float value) { + return saturate(lerpInv(a, b, value)); + } + + /** * Returns an interpolated angle in degrees between a set of start and end * angles. * <p> @@ -195,6 +215,32 @@ public final class MathUtils { } /** + * Calculates a value in [rangeMin, rangeMax] that maps value in [valueMin, valueMax] to + * returnVal in [rangeMin, rangeMax]. + * <p> + * Always returns a constrained value in the range [rangeMin, rangeMax], even if value is + * outside [valueMin, valueMax]. + * <p> + * Eg: + * constrainedMap(0f, 100f, 0f, 1f, 0.5f) = 50f + * constrainedMap(20f, 200f, 10f, 20f, 20f) = 200f + * constrainedMap(20f, 200f, 10f, 20f, 50f) = 200f + * constrainedMap(10f, 50f, 10f, 20f, 5f) = 10f + * + * @param rangeMin minimum of the range that should be returned. + * @param rangeMax maximum of the range that should be returned. + * @param valueMin minimum of range to map {@code value} to. + * @param valueMax maximum of range to map {@code value} to. + * @param value to map to the range [{@code valueMin}, {@code valueMax}]. Note, can be outside + * this range, resulting in a clamped value. + * @return the mapped value, constrained to [{@code rangeMin}, {@code rangeMax}. + */ + public static float constrainedMap( + float rangeMin, float rangeMax, float valueMin, float valueMax, float value) { + return lerp(rangeMin, rangeMax, lerpInvSat(valueMin, valueMax, value)); + } + + /** * Perform Hermite interpolation between two values. * Eg: * smoothStep(0, 0.5f, 0.5f) = 1f |
