diff --git a/modules/juce_core/maths/juce_MathsFunctions.h b/modules/juce_core/maths/juce_MathsFunctions.h index 78a9dd995c..bd84e25314 100644 --- a/modules/juce_core/maths/juce_MathsFunctions.h +++ b/modules/juce_core/maths/juce_MathsFunctions.h @@ -518,6 +518,13 @@ namespace TypeHelpers */ #define PARAMETER_TYPE(a) typename TypeHelpers::ParameterType::type #endif + + + /** These templates are designed to take a type, and if it's a double, they return a double + type; for anything else, they return a float type. + */ + template struct SmallestFloatType { typedef float type; }; + template <> struct SmallestFloatType { typedef double type; }; } diff --git a/modules/juce_graphics/geometry/juce_Line.h b/modules/juce_graphics/geometry/juce_Line.h index 048b752e73..4d86e54512 100644 --- a/modules/juce_graphics/geometry/juce_Line.h +++ b/modules/juce_graphics/geometry/juce_Line.h @@ -137,10 +137,10 @@ public: /** Returns the line's angle. - This value is the number of radians clockwise from the 3 o'clock direction, + This value is the number of radians clockwise from the 12 o'clock direction, where the line's start point is considered to be at the centre. */ - ValueType getAngle() const noexcept { return start.getAngleToPoint (end); } + typename Point::FloatType getAngle() const noexcept { return start.getAngleToPoint (end); } //============================================================================== /** Compares two lines. */ @@ -214,8 +214,8 @@ public: if (length <= 0) return start; - return Point (start.x + (ValueType) ((delta.x * distanceFromStart - delta.y * perpendicularDistance) / length), - start.y + (ValueType) ((delta.y * distanceFromStart + delta.x * perpendicularDistance) / length)); + return Point (start.x + static_cast ((delta.x * distanceFromStart - delta.y * perpendicularDistance) / length), + start.y + static_cast ((delta.y * distanceFromStart + delta.x * perpendicularDistance) / length)); } /** Returns the location of the point which is a given distance along this line @@ -257,7 +257,7 @@ public: if (prop >= 0 && prop <= 1.0) { - pointOnLine = start + delta * (ValueType) prop; + pointOnLine = start + delta * static_cast (prop); return targetPoint.getDistanceFrom (pointOnLine); } } @@ -291,9 +291,9 @@ public: const double length = delta.x * delta.x + delta.y * delta.y; return length <= 0 ? 0 - : jlimit ((ValueType) 0, (ValueType) 1, - (ValueType) (((point.x - start.x) * delta.x - + (point.y - start.y) * delta.y) / length)); + : jlimit (ValueType(), static_cast (1), + static_cast ((((point.x - start.x) * delta.x + + (point.y - start.y) * delta.y) / length))); } /** Finds the point on this line which is nearest to a given point. @@ -365,40 +365,40 @@ private: { const ValueType along = (p1.y - p3.y) / d2.y; intersection = p1.withX (p3.x + along * d2.x); - return along >= 0 && along <= (ValueType) 1; + return along >= 0 && along <= static_cast (1); } else if (d2.y == 0 && d1.y != 0) { const ValueType along = (p3.y - p1.y) / d1.y; intersection = p3.withX (p1.x + along * d1.x); - return along >= 0 && along <= (ValueType) 1; + return along >= 0 && along <= static_cast (1); } else if (d1.x == 0 && d2.x != 0) { const ValueType along = (p1.x - p3.x) / d2.x; intersection = p1.withY (p3.y + along * d2.y); - return along >= 0 && along <= (ValueType) 1; + return along >= 0 && along <= static_cast (1); } else if (d2.x == 0 && d1.x != 0) { const ValueType along = (p3.x - p1.x) / d1.x; intersection = p3.withY (p1.y + along * d1.y); - return along >= 0 && along <= (ValueType) 1; + return along >= 0 && along <= static_cast (1); } } - intersection = (p2 + p3) / (ValueType) 2; + intersection = (p2 + p3) / static_cast (2); return false; } const ValueType along1 = ((p1.y - p3.y) * d2.x - (p1.x - p3.x) * d2.y) / divisor; intersection = p1 + d1 * along1; - if (along1 < 0 || along1 > (ValueType) 1) + if (along1 < 0 || along1 > static_cast (1)) return false; const ValueType along2 = ((p1.y - p3.y) * d1.x - (p1.x - p3.x) * d1.y) / divisor; - return along2 >= 0 && along2 <= (ValueType) 1; + return along2 >= 0 && along2 <= static_cast (1); } }; diff --git a/modules/juce_graphics/geometry/juce_Point.h b/modules/juce_graphics/geometry/juce_Point.h index 879a3eafd4..f7f80fb454 100644 --- a/modules/juce_graphics/geometry/juce_Point.h +++ b/modules/juce_graphics/geometry/juce_Point.h @@ -125,26 +125,33 @@ public: /** Returns the straight-line distance between this point and another one. */ ValueType getDistanceFrom (const Point& other) const noexcept { return juce_hypot (x - other.x, y - other.y); } + /** This type will be double if the Point's type is double, otherwise it will be float. */ + typedef typename TypeHelpers::SmallestFloatType::type FloatType; + /** Returns the angle from this point to another one. - The return value is the number of radians clockwise from the 3 o'clock direction, + The return value is the number of radians clockwise from the 12 o'clock direction, where this point is the centre and the other point is on the circumference. */ - ValueType getAngleToPoint (const Point& other) const noexcept { return (ValueType) std::atan2 (other.x - x, other.y - y); } + FloatType getAngleToPoint (const Point& other) const noexcept + { return static_cast (std::atan2 (other.x - x, y - other.y)); } /** Taking this point to be the centre of a circle, this returns a point on its circumference. @param radius the radius of the circle. @param angle the angle of the point, in radians clockwise from the 12 o'clock position. */ - Point getPointOnCircumference (const float radius, const float angle) const noexcept { return Point (x + radius * std::sin (angle), - y - radius * std::cos (angle)); } + Point getPointOnCircumference (const float radius, const float angle) const noexcept + { return Point (static_cast (x + radius * std::sin (angle)), + static_cast (y - radius * std::cos (angle))); } + /** Taking this point to be the centre of an ellipse, this returns a point on its circumference. @param radiusX the horizontal radius of the circle. @param radiusY the vertical radius of the circle. @param angle the angle of the point, in radians clockwise from the 12 o'clock position. */ - Point getPointOnCircumference (const float radiusX, const float radiusY, const float angle) const noexcept { return Point (x + radiusX * std::sin (angle), - y - radiusY * std::cos (angle)); } + Point getPointOnCircumference (const float radiusX, const float radiusY, const float angle) const noexcept + { return Point (static_cast (x + radiusX * std::sin (angle)), + static_cast (y - radiusY * std::cos (angle))); } /** Uses a transform to change the point's co-ordinates. This will only compile if ValueType = float! @@ -153,8 +160,9 @@ public: void applyTransform (const AffineTransform& transform) noexcept { transform.transformPoint (x, y); } /** Returns the position of this point, if it is transformed by a given AffineTransform. */ - Point transformedBy (const AffineTransform& transform) const noexcept { return Point (transform.mat00 * x + transform.mat01 * y + transform.mat02, - transform.mat10 * x + transform.mat11 * y + transform.mat12); } + Point transformedBy (const AffineTransform& transform) const noexcept + { return Point (transform.mat00 * x + transform.mat01 * y + transform.mat02, + transform.mat10 * x + transform.mat11 * y + transform.mat12); } /** Casts this point to a Point object. */ Point toInt() const noexcept { return Point (static_cast (x), static_cast (y)); }