From d48167a0b759f9ec6eba9946a098ce1b7d3f8652 Mon Sep 17 00:00:00 2001 From: jules Date: Mon, 1 Feb 2016 11:22:04 +0000 Subject: [PATCH] Added some length-squared methods to Line and Point --- modules/juce_core/maths/juce_MathsFunctions.h | 4 ++-- modules/juce_graphics/geometry/juce_Line.h | 3 +++ modules/juce_graphics/geometry/juce_Point.h | 6 ++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/modules/juce_core/maths/juce_MathsFunctions.h b/modules/juce_core/maths/juce_MathsFunctions.h index c961c02898..de92d97182 100644 --- a/modules/juce_core/maths/juce_MathsFunctions.h +++ b/modules/juce_core/maths/juce_MathsFunctions.h @@ -324,9 +324,9 @@ template <> inline float juce_hypot (float a, float b) noexcept { #if JUCE_MSVC - return (_hypotf (a, b)); + return _hypotf (a, b); #else - return (hypotf (a, b)); + return hypotf (a, b); #endif } #endif diff --git a/modules/juce_graphics/geometry/juce_Line.h b/modules/juce_graphics/geometry/juce_Line.h index 02f91699bf..a6dfadf378 100644 --- a/modules/juce_graphics/geometry/juce_Line.h +++ b/modules/juce_graphics/geometry/juce_Line.h @@ -126,6 +126,9 @@ public: /** Returns the length of the line. */ ValueType getLength() const noexcept { return start.getDistanceFrom (end); } + /** Returns the length of the line. */ + ValueType getLengthSquared() const noexcept { return start.getDistanceSquaredFrom (end); } + /** Returns true if the line's start and end x coordinates are the same. */ bool isVertical() const noexcept { return start.x == end.x; } diff --git a/modules/juce_graphics/geometry/juce_Point.h b/modules/juce_graphics/geometry/juce_Point.h index 33dc1c7740..9e1bb0fa0c 100644 --- a/modules/juce_graphics/geometry/juce_Point.h +++ b/modules/juce_graphics/geometry/juce_Point.h @@ -147,6 +147,12 @@ public: /** Returns the straight-line distance between this point and another one. */ ValueType getDistanceFrom (Point other) const noexcept { return juce_hypot (x - other.x, y - other.y); } + /** Returns the square of the straight-line distance between this point and the origin. */ + ValueType getDistanceSquaredFromOrigin() const noexcept { return x * x + y * y; } + + /** Returns the square of the straight-line distance between this point and another one. */ + ValueType getDistanceSquaredFrom (Point other) const noexcept { return (*this - other).getDistanceSquaredFromOrigin(); } + /** Returns the angle from this point to another one. The return value is the number of radians clockwise from the 12 o'clock direction,