Files
MapLibreParticleLab/include/mln/math/wrap.hpp
Bart Louwers 2a8ebc4906 core: rename mbgl directories to mln (#4511)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-08-22 20:50:14 +02:00

25 lines
518 B
C++

#pragma once
#include <cmath>
namespace mln {
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 mln