If (wrap] is desired, it is the responsibility of the caller to handle the case in which min is returned.
This commit is contained in:
Minh Nguyễn
2015-07-02 13:03:32 -07:00
parent 78126f3b6b
commit 46a6173bd7
2 changed files with 4 additions and 1 deletions

View File

@@ -112,10 +112,12 @@ T clamp(T value, T min, T max) {
return value < min ? min : (value > max ? max : value);
}
// 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 value == max ? value : std::fmod((std::fmod((value - min), d) + d), d) + min;
return std::fmod((std::fmod((value - min), d) + d), d) + min;
}
template <typename T>

View File

@@ -15,6 +15,7 @@ using namespace mbgl;
static double _normalizeAngle(double angle, double anchorAngle)
{
angle = util::wrap(angle, -M_PI, M_PI);
if (angle == -M_PI) angle = M_PI;
double diff = std::abs(angle - anchorAngle);
if (std::abs(angle - util::M2PI - anchorAngle) < diff) {
angle -= util::M2PI;