From a49cc0fca5c840c88a07b196498affdbc71384eb Mon Sep 17 00:00:00 2001 From: jules Date: Tue, 7 Jul 2015 15:22:00 +0100 Subject: [PATCH] Added methods degreesToRadians and radiansToDegrees --- modules/juce_core/javascript/juce_Javascript.cpp | 4 ++-- modules/juce_core/maths/juce_MathsFunctions.h | 16 ++++++++++++---- .../juce_gui_basics/drawables/juce_SVGParser.cpp | 8 ++++---- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/modules/juce_core/javascript/juce_Javascript.cpp b/modules/juce_core/javascript/juce_Javascript.cpp index 9e5fe3d9d0..f9a2585ef1 100644 --- a/modules/juce_core/javascript/juce_Javascript.cpp +++ b/modules/juce_core/javascript/juce_Javascript.cpp @@ -1584,8 +1584,8 @@ struct JavascriptEngine::RootObject : public DynamicObject static var Math_range (Args a) { return isInt (a, 0) ? var (jlimit (getInt (a, 1), getInt (a, 2), getInt (a, 0))) : var (jlimit (getDouble (a, 1), getDouble (a, 2), getDouble (a, 0))); } static var Math_min (Args a) { return (isInt (a, 0) && isInt (a, 1)) ? var (jmin (getInt (a, 0), getInt (a, 1))) : var (jmin (getDouble (a, 0), getDouble (a, 1))); } static var Math_max (Args a) { return (isInt (a, 0) && isInt (a, 1)) ? var (jmax (getInt (a, 0), getInt (a, 1))) : var (jmax (getDouble (a, 0), getDouble (a, 1))); } - static var Math_toDegrees (Args a) { return (180.0 / double_Pi) * getDouble (a, 0); } - static var Math_toRadians (Args a) { return (double_Pi / 180.0) * getDouble (a, 0); } + static var Math_toDegrees (Args a) { return radiansToDegrees (getDouble (a, 0)); } + static var Math_toRadians (Args a) { return degreesToRadians (getDouble (a, 0)); } static var Math_sin (Args a) { return sin (getDouble (a, 0)); } static var Math_asin (Args a) { return asin (getDouble (a, 0)); } static var Math_cos (Args a) { return cos (getDouble (a, 0)); } diff --git a/modules/juce_core/maths/juce_MathsFunctions.h b/modules/juce_core/maths/juce_MathsFunctions.h index 30fdd104c9..71386ab3e8 100644 --- a/modules/juce_core/maths/juce_MathsFunctions.h +++ b/modules/juce_core/maths/juce_MathsFunctions.h @@ -354,6 +354,15 @@ const double double_Pi = 3.1415926535897932384626433832795; const float float_Pi = 3.14159265358979323846f; +/** Converts an angle in degrees to radians. */ +template +inline FloatType degreesToRadians (FloatType degrees) noexcept { return degrees * static_cast (double_Pi / 180.0); } + +/** Converts an angle in radians to degrees. */ +template +inline FloatType radiansToDegrees (FloatType radians) noexcept { return radians * static_cast (180.0 / double_Pi); } + + //============================================================================== /** The isfinite() method seems to vary between platforms, so this is a platform-independent function for it. @@ -477,16 +486,14 @@ inline int roundFloatToInt (const float value) noexcept } //============================================================================== -/** Returns true if the specified integer is a power-of-two. -*/ +/** Returns true if the specified integer is a power-of-two. */ template bool isPowerOfTwo (IntegerType value) { return (value & (value - 1)) == 0; } -/** Returns the smallest power-of-two which is equal to or greater than the given integer. -*/ +/** Returns the smallest power-of-two which is equal to or greater than the given integer. */ inline int nextPowerOfTwo (int n) noexcept { --n; @@ -533,6 +540,7 @@ NumericType square (NumericType n) noexcept return n * n; } + //============================================================================== #if JUCE_INTEL || defined (DOXYGEN) /** This macro can be applied to a float variable to check whether it contains a denormalised diff --git a/modules/juce_gui_basics/drawables/juce_SVGParser.cpp b/modules/juce_gui_basics/drawables/juce_SVGParser.cpp index d5a926a91f..352616c9c6 100644 --- a/modules/juce_gui_basics/drawables/juce_SVGParser.cpp +++ b/modules/juce_gui_basics/drawables/juce_SVGParser.cpp @@ -272,7 +272,7 @@ public: if (parseNextNumber (d, num, false)) { - const float angle = num.getFloatValue() * (180.0f / float_Pi); + const float angle = degreesToRadians (num.getFloatValue()); if (parseNextNumber (d, num, false)) { @@ -1221,15 +1221,15 @@ private: } else if (t.startsWithIgnoreCase ("rotate")) { - trans = AffineTransform::rotation (numbers[0] / (180.0f / float_Pi), numbers[1], numbers[2]); + trans = AffineTransform::rotation (degreesToRadians (numbers[0]), numbers[1], numbers[2]); } else if (t.startsWithIgnoreCase ("skewX")) { - trans = AffineTransform::shear (std::tan (numbers[0] * (float_Pi / 180.0f)), 0.0f); + trans = AffineTransform::shear (std::tan (degreesToRadians (numbers[0])), 0.0f); } else if (t.startsWithIgnoreCase ("skewY")) { - trans = AffineTransform::shear (0.0f, std::tan (numbers[0] * (float_Pi / 180.0f))); + trans = AffineTransform::shear (0.0f, std::tan (degreesToRadians (numbers[0]))); } result = trans.followedBy (result);