summaryrefslogtreecommitdiff
path: root/include/mbgl/math/wrap.hpp
blob: 307622538461302a82adf1e6be9e697d400762b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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;
    }

    const T delta = max - min;
    const T wrapped = min + std::fmod(value - min, delta);
    return value < min ? wrapped + delta : wrapped;
}

} // namespace util
} // namespace mbgl