From 9445bd56a30811132e2e839c940ecdfc878b5a29 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Fri, 4 Aug 2017 01:27:23 +0200 Subject: [PATCH] Ref T111, normalize utility functions --- src/blackmisc/math/mathutils.cpp | 10 ++++++++-- src/blackmisc/math/mathutils.h | 5 ++++- src/blackmisc/pq/angle.cpp | 13 +++++++++++++ src/blackmisc/pq/angle.h | 6 ++++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/blackmisc/math/mathutils.cpp b/src/blackmisc/math/mathutils.cpp index 23387c017..859222f24 100644 --- a/src/blackmisc/math/mathutils.cpp +++ b/src/blackmisc/math/mathutils.cpp @@ -79,9 +79,15 @@ namespace BlackMisc return radians * 180.0 / CMathUtils::PI(); } - double CMathUtils::normalizeDegrees(double degrees) + double CMathUtils::normalizeDegrees180(double degrees) { - double result = std::fmod(degrees, 360.0); + const double result = CMathUtils::normalizeDegrees360(degrees + 180.0) - 180.0; + return (result <= -180.0) ? 180.0 : result; // -180->180 + } + + double CMathUtils::normalizeDegrees360(double degrees) + { + const double result = std::fmod(degrees, 360.0); return (result >= 0.0) ? result : result + 360.0; } diff --git a/src/blackmisc/math/mathutils.h b/src/blackmisc/math/mathutils.h index 16ae90b09..83ebb3d2d 100644 --- a/src/blackmisc/math/mathutils.h +++ b/src/blackmisc/math/mathutils.h @@ -96,8 +96,11 @@ namespace BlackMisc //! Radians to degrees static double rad2deg(double radians); + //! Normalize: -180< degrees ≤180 + static double normalizeDegrees180(double degrees); + //! Normalize: 0≤ degrees <360 - static double normalizeDegrees(double degrees); + static double normalizeDegrees360(double degrees); //! Random number between low and high static int randomInteger(int low, int high); diff --git a/src/blackmisc/pq/angle.cpp b/src/blackmisc/pq/angle.cpp index 33a104189..6ca2c7fd6 100644 --- a/src/blackmisc/pq/angle.cpp +++ b/src/blackmisc/pq/angle.cpp @@ -126,5 +126,18 @@ namespace BlackMisc { return std::tan(this->value(CAngleUnit::rad())); } + + double CAngle::normalizeDegrees180(double degrees, int roundDigits) + { + double d = CMathUtils::normalizeDegrees360(degrees + 180.0) - 180.0; + if (d <= -180.0) { d = 180.0; } // -180 -> 180 + return roundDigits < 0 ? d : CMathUtils::round(d, roundDigits); + } + + double CAngle::normalizeDegrees360(double degrees, int roundDigits) + { + const double d = CMathUtils::normalizeDegrees360(degrees); + return roundDigits < 0 ? d : CMathUtils::round(d, roundDigits); + } } // ns } // ns diff --git a/src/blackmisc/pq/angle.h b/src/blackmisc/pq/angle.h index afb7d1b0a..c04f126eb 100644 --- a/src/blackmisc/pq/angle.h +++ b/src/blackmisc/pq/angle.h @@ -96,6 +96,12 @@ namespace BlackMisc //! Tangent of angle double tan() const; + + //! Normalize: -180< degrees ≤180 + static double normalizeDegrees180(double degrees, int roundDigits = -1); + + //! Normalize: 0≤ degrees <360 + static double normalizeDegrees360(double degrees, int roundDigits = -1); }; } // ns } // ns