From 99806c99a486f9112f95b64f339dc4284ec949ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=A2=C2=80=C2=9Cosana=C3=A2=C2=80=C2=9D?= Date: Mon, 12 Feb 2018 12:36:04 -0500 Subject: [PATCH] Moved MathUtils from mapbox-java2.x telemetry to mapbox-java3.0 services-core --- .../java/com/mapbox/core/utils/MathUtils.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 services-core/src/main/java/com/mapbox/core/utils/MathUtils.java diff --git a/services-core/src/main/java/com/mapbox/core/utils/MathUtils.java b/services-core/src/main/java/com/mapbox/core/utils/MathUtils.java new file mode 100644 index 000000000..cc2006c81 --- /dev/null +++ b/services-core/src/main/java/com/mapbox/core/utils/MathUtils.java @@ -0,0 +1,48 @@ +package com.mapbox.core.utils; + +public class MathUtils { + + /** + * Test a value in specified range, returning minimum if it's below, and maximum if it's above + * + * @param value Value to test + * @param min Minimum value of range + * @param max Maximum value of range + * @return value if it's between min and max, min if it's below, max if it's above + */ + public static double clamp(double value, double min, double max) { + return Math.max(min, Math.min(max, value)); + } + + /** + * Test a value in specified range, returning minimum if it's below, and maximum if it's above + * + * @param value Value to test + * @param min Minimum value of range + * @param max Maximum value of range + * @return value if it's between min and max, min if it's below, max if it's above + */ + public static float clamp(float value, float min, float max) { + return Math.max(min, Math.min(max, value)); + } + + /** + * Constrains value to the given range (including min, excluding max) via modular arithmetic. + *

+ * Same formula as used in Core GL (wrap.hpp) + * std::fmod((std::fmod((value - min), d) + d), d) + min; + * + * @param value Value to wrap + * @param min Minimum value + * @param max Maximum value + * @return Wrapped value + */ + public static double wrap(double value, double min, double max) { + double delta = max - min; + + double firstMod = (value - min) % delta; + double secondMod = (firstMod + delta) % delta; + + return secondMod + min; + } +}