diff --git a/modules/juce_graphics/geometry/juce_Rectangle.h b/modules/juce_graphics/geometry/juce_Rectangle.h index 8f00a8110e..ddb53d4efa 100644 --- a/modules/juce_graphics/geometry/juce_Rectangle.h +++ b/modules/juce_graphics/geometry/juce_Rectangle.h @@ -538,6 +538,53 @@ public: return r; } + //============================================================================== + /** Returns the nearest point to the specified point that lies within this rectangle. */ + Point getConstrainedPoint (Point point) const noexcept + { + return Point (jlimit (pos.x, pos.x + w, point.x), + jlimit (pos.y, pos.y + h, point.y)); + } + + /** Returns a point within this rectangle, specified as proportional coordinates. + The relative X and Y values should be between 0 and 1, where 0 is the left or + top of this rectangle, and 1 is the right or bottom. (Out-of-bounds values + will return a point outside the rectangle). + */ + template + Point getRelativePoint (FloatType relativeX, FloatType relativeY) const noexcept + { + return Point (pos.x + static_cast (w * relativeX), + pos.y + static_cast (h * relativeY)); + } + + /** Returns a proportion of the width of this rectangle. */ + template + ValueType proportionOfWidth (FloatType proportion) const noexcept + { + return static_cast (w * proportion); + } + + /** Returns a proportion of the height of this rectangle. */ + template + ValueType proportionOfHeight (FloatType proportion) const noexcept + { + return static_cast (h * proportion); + } + + /** Returns a rectangle based on some proportional coordinates relative to this one. + So for example getProportion ({ 0.25f, 0.25f, 0.5f, 0.5f }) would return a rectangle + of half the original size, with the same centre. + */ + template + Rectangle getProportion (Rectangle proportionalRect) const noexcept + { + return Rectangle (pos.x + static_cast (w * proportionalRect.pos.x), + pos.y + static_cast (h * proportionalRect.pos.y), + proportionOfWidth (proportionalRect.w), + proportionOfWidth (proportionalRect.h)); + } + //============================================================================== /** Returns true if the two rectangles are identical. */ bool operator== (const Rectangle& other) const noexcept { return pos == other.pos && w == other.w && h == other.h; } @@ -564,24 +611,6 @@ public: && pos.x + w >= other.pos.x + other.w && pos.y + h >= other.pos.y + other.h; } - /** Returns the nearest point to the specified point that lies within this rectangle. */ - Point getConstrainedPoint (Point point) const noexcept - { - return Point (jlimit (pos.x, pos.x + w, point.x), - jlimit (pos.y, pos.y + h, point.y)); - } - - /** Returns a point within this rectangle, specified as proportional coordinates. - The relative X and Y values should be between 0 and 1, where 0 is the left or - top of this rectangle, and 1 is the right or bottom. (Out-of-bounds values - will return a point outside the rectangle). - */ - Point getRelativePoint (double relativeX, double relativeY) const noexcept - { - return Point (pos.x + static_cast (w * relativeX), - pos.y + static_cast (h * relativeY)); - } - /** Returns true if any part of another rectangle overlaps this one. */ bool intersects (const Rectangle& other) const noexcept {