summaryrefslogtreecommitdiff
path: root/include/mbgl/math/wrap.hpp
blob: 8a9303edc2a38a00b8557e49699e044f0ae9efd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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) {
    if (value >= min && value < max) {
        return value;
    } else if (value == max) {
        return min;
    }
    T d = max - min;
    return std::fmod((std::fmod((value - min), d) + d), d) + min;
}

} // namespace util
} // namespace mbgl