From 46a6173bd744c7d2a65fc6533e0d6a37a93e7e5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguy=E1=BB=85n?= Date: Thu, 2 Jul 2015 13:03:32 -0700 Subject: [PATCH] [wrap) If (wrap] is desired, it is the responsibility of the caller to handle the case in which min is returned. --- include/mbgl/util/math.hpp | 4 +++- src/mbgl/map/transform.cpp | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/include/mbgl/util/math.hpp b/include/mbgl/util/math.hpp index 5fcf3dce8..37b1dd343 100644 --- a/include/mbgl/util/math.hpp +++ b/include/mbgl/util/math.hpp @@ -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 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 diff --git a/src/mbgl/map/transform.cpp b/src/mbgl/map/transform.cpp index 7ea437606..14b3611a1 100644 --- a/src/mbgl/map/transform.cpp +++ b/src/mbgl/map/transform.cpp @@ -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;