Browse Source

Added a couple of proportional Rectangle methods

tags/2021-05-28
jules 9 years ago
parent
commit
97e058bbd2
1 changed files with 47 additions and 18 deletions
  1. +47
    -18
      modules/juce_graphics/geometry/juce_Rectangle.h

+ 47
- 18
modules/juce_graphics/geometry/juce_Rectangle.h View File

@@ -538,6 +538,53 @@ public:
return r;
}
//==============================================================================
/** Returns the nearest point to the specified point that lies within this rectangle. */
Point<ValueType> getConstrainedPoint (Point<ValueType> point) const noexcept
{
return Point<ValueType> (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 <typename FloatType>
Point<ValueType> getRelativePoint (FloatType relativeX, FloatType relativeY) const noexcept
{
return Point<ValueType> (pos.x + static_cast<ValueType> (w * relativeX),
pos.y + static_cast<ValueType> (h * relativeY));
}
/** Returns a proportion of the width of this rectangle. */
template <typename FloatType>
ValueType proportionOfWidth (FloatType proportion) const noexcept
{
return static_cast<ValueType> (w * proportion);
}
/** Returns a proportion of the height of this rectangle. */
template <typename FloatType>
ValueType proportionOfHeight (FloatType proportion) const noexcept
{
return static_cast<ValueType> (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 <typename FloatType>
Rectangle getProportion (Rectangle<FloatType> proportionalRect) const noexcept
{
return Rectangle (pos.x + static_cast<ValueType> (w * proportionalRect.pos.x),
pos.y + static_cast<ValueType> (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<ValueType> getConstrainedPoint (Point<ValueType> point) const noexcept
{
return Point<ValueType> (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<ValueType> getRelativePoint (double relativeX, double relativeY) const noexcept
{
return Point<ValueType> (pos.x + static_cast<ValueType> (w * relativeX),
pos.y + static_cast<ValueType> (h * relativeY));
}
/** Returns true if any part of another rectangle overlaps this one. */
bool intersects (const Rectangle& other) const noexcept
{


Loading…
Cancel
Save