summaryrefslogtreecommitdiff
path: root/include/mbgl/math/wrap.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'include/mbgl/math/wrap.hpp')
-rw-r--r--include/mbgl/math/wrap.hpp17
1 files changed, 17 insertions, 0 deletions
diff --git a/include/mbgl/math/wrap.hpp b/include/mbgl/math/wrap.hpp
new file mode 100644
index 0000000000..16dc598c22
--- /dev/null
+++ b/include/mbgl/math/wrap.hpp
@@ -0,0 +1,17 @@
+#pragma once
+
+#include <cmath>
+
+namespace mbgl {
+namespace util {
+
+// Constrains n to the given range (including min, excluding max) via modular
+// arithmetic.
+template <typename T>
+T wrap(T value, T min, T max) {
+ T d = max - min;
+ return std::fmod((std::fmod((value - min), d) + d), d) + min;
+}
+
+} // namespace util
+} // namespace mbgl