33template <
typename>
struct Tag {};
39template <
typename ValueType>
42 return static_cast<ValueType
> (
getNumericValue (s.text.findEndOfWhitespace(),
46inline int floorAsInt (
int n)
noexcept {
return n; }
50inline int ceilAsInt (
int n)
noexcept {
return n; }
65template <
typename ValueType>
80 ValueType width, ValueType height) noexcept
87 Rectangle (ValueType width, ValueType height) noexcept
88 : w (width), h (height)
99 if (w < ValueType()) w = -w;
100 if (h < ValueType()) h = -h;
108 ValueType right, ValueType bottom)
noexcept
110 return { left, top, right - left, bottom - top };
121 bool isEmpty()
const noexcept {
return w <= ValueType() || h <= ValueType(); }
127 inline ValueType
getX()
const noexcept {
return pos.x; }
130 inline ValueType
getY()
const noexcept {
return pos.y; }
152 pos.y + h / (ValueType) 2 }; }
242 newCentre.y - h / (ValueType) 2, w, h }; }
320 ValueType deltaY)
noexcept
328 ValueType deltaY)
const noexcept
330 return { pos.x + deltaX, pos.y + deltaY, w, h };
364 template <
typename FloatType>
377 template <
typename FloatType>
381 (FloatType) pos.y * scaleFactor,
382 (FloatType) w * scaleFactor,
383 (FloatType) h * scaleFactor).copyWithRounding (*
this);
392 template <
typename FloatType>
396 (FloatType) pos.y * scaleFactor.y,
397 (FloatType) w * scaleFactor.x,
398 (FloatType) h * scaleFactor.y).copyWithRounding (*
this);
403 template <
typename FloatType>
412 template <
typename FloatType>
416 (FloatType) pos.y / scaleFactor,
417 (FloatType) w / scaleFactor,
418 (FloatType) h / scaleFactor).copyWithRounding (*
this);
423 template <
typename FloatType>
427 (FloatType) pos.y / scaleFactor.y,
428 (FloatType) w / scaleFactor.x,
429 (FloatType) h / scaleFactor.y).copyWithRounding (*
this);
439 ValueType deltaY)
noexcept
441 auto nw =
jmax (ValueType(), w + deltaX * 2);
442 auto nh =
jmax (ValueType(), h + deltaY * 2);
452 ValueType deltaY)
const noexcept
454 auto nw =
jmax (ValueType(), w + deltaX * 2);
455 auto nh =
jmax (ValueType(), h + deltaY * 2);
456 return { pos.x - deltaX, pos.y - deltaY,
nw,
nh };
475 ValueType deltaY)
noexcept
477 expand (-deltaX, -deltaY);
486 ValueType deltaY)
const noexcept
513 pos.y += r.h; h -= r.h;
529 pos.x += r.w; w -= r.w;
571 return {
jlimit (pos.x, pos.x + w, point.x),
572 jlimit (pos.y, pos.y + h, point.y) };
580 template <
typename FloatType>
583 return { pos.x +
static_cast<ValueType
> ((FloatType) w *
relativeX),
584 pos.y +
static_cast<ValueType
> ((FloatType) h *
relativeY) };
588 template <
typename FloatType>
591 return static_cast<ValueType
> ((FloatType) w *
proportion);
595 template <
typename FloatType>
598 return static_cast<ValueType
> ((FloatType) h *
proportion);
605 template <
typename FloatType>
619 return tie (*
this) == tie (
other);
634 return point.x >= pos.x && point.y >= pos.y && point.x < pos.x + w && point.y < pos.y + h;
640 return pos.x <=
other.pos.x && pos.y <=
other.pos.y
647 return pos.x + w >
other.pos.x
648 && pos.y + h >
other.pos.y
651 && w > ValueType() && h > ValueType()
652 &&
other.w > ValueType() &&
other.h > ValueType();
674 if (
nw >= ValueType())
678 if (
nh >= ValueType())
725 if (
other.isEmpty())
return *
this;
785 case 1 + 2 + 8: w = r -
otherR; pos.x =
otherR;
return true;
786 case 1 + 2 + 4: h = b -
otherB; pos.y =
otherB;
return true;
787 case 2 + 4 + 8: w =
other.pos.x - pos.x;
return true;
788 case 1 + 4 + 8: h =
other.pos.y - pos.y;
return true;
822 auto x1 =
static_cast<FloatType
> (pos.x),
y1 =
static_cast<FloatType
> (pos.y);
823 auto x2 =
static_cast<FloatType
> (pos.x + w), y2 =
static_cast<FloatType
> (pos.y);
824 auto x3 =
static_cast<FloatType
> (pos.x), y3 =
static_cast<FloatType
> (pos.y + h);
825 auto x4 =
static_cast<FloatType
> (x2),
y4 =
static_cast<FloatType
> (y3);
827 transform.transformPoints (x1,
y1, x2, y2);
828 transform.transformPoints (x3, y3,
x4,
y4);
830 auto rx1 =
jmin (x1, x2, x3,
x4);
831 auto rx2 =
jmax (x1, x2, x3,
x4);
847 detail::floorAsInt (pos.y),
848 detail::ceilAsInt (pos.x + w),
849 detail::ceilAsInt (pos.y + h));
879 return {
static_cast<float> (pos.x),
static_cast<float> (pos.y),
880 static_cast<float> (w),
static_cast<float> (h) };
888 return {
static_cast<double> (pos.x),
static_cast<double> (pos.y),
889 static_cast<double> (w),
static_cast<double> (h) };
896 template <
typename TargetType>
900 copyWithRounding (r);
932 ValueType x2, ValueType y2, ValueType
w2, ValueType
h2)
noexcept
934 auto x =
jmax (x1, x2);
937 if (
w1 > ValueType())
942 if (
h1 > ValueType())
966 s << pos.x <<
' ' << pos.y <<
' ' << w <<
' ' << h;
985 return { detail::parseAfterSpace<ValueType> (
toks[0]),
986 detail::parseAfterSpace<ValueType> (
toks[1]),
987 detail::parseAfterSpace<ValueType> (
toks[2]),
988 detail::parseAfterSpace<ValueType> (
toks[3]) };
992 [[
deprecated (
"This has been renamed to transformedBy in order to match the method names used in the Point class.")]]
997 template <
typename OtherType>
friend class Rectangle;
999 Point<ValueType> pos;
1000 ValueType w {}, h {};
1003 void copyWithRounding (Rectangle<float>& result)
const noexcept { result =
toFloat(); }
1004 void copyWithRounding (Rectangle<double>& result)
const noexcept { result =
toDouble(); }
A pair of (x, y) coordinates.
A general-purpose range object, that simply represents any linear range with a start and end point.
static Range withStartAndLength(const ValueType startValue, const ValueType length) noexcept
Returns a range with a given start and length.
Manages a rectangle and allows geometric operations to be performed on it.
Rectangle(ValueType initialX, ValueType initialY, ValueType width, ValueType height) noexcept
Creates a rectangle with a given position and size.
ValueType getRight() const noexcept
Returns the x coordinate of the rectangle's right-hand-side.
Range< ValueType > getHorizontalRange() const noexcept
Returns the rectangle's left and right positions as a Range.
Range< ValueType > getVerticalRange() const noexcept
Returns the rectangle's top and bottom positions as a Range.
Rectangle operator+(Point< ValueType > deltaPosition) const noexcept
Returns a rectangle which is the same as this one moved by a given amount.
void setLeft(ValueType newLeft) noexcept
Moves the x position, adjusting the width so that the right-hand edge remains in the same place.
String toString() const
Creates a string describing this rectangle.
Point< ValueType > getCentre() const noexcept
Returns the centre point of the rectangle.
Point< ValueType > getBottomRight() const noexcept
Returns the rectangle's bottom-right position as a Point.
bool intersects(Rectangle other) const noexcept
Returns true if any part of another rectangle overlaps this one.
Rectangle(Point< ValueType > corner1, Point< ValueType > corner2) noexcept
Creates a Rectangle from the positions of two opposite corners.
void setY(ValueType newY) noexcept
Changes the rectangle's Y coordinate.
Rectangle withX(ValueType newX) const noexcept
Returns a rectangle which has the same size and y-position as this one, but with a different x-positi...
Rectangle withHeight(ValueType newHeight) const noexcept
Returns a rectangle which has the same position and width as this one, but with a different height.
Rectangle withPosition(ValueType newX, ValueType newY) const noexcept
Returns a rectangle with the same size as this one, but a new position.
Rectangle withTrimmedTop(ValueType amountToRemove) const noexcept
Returns a version of this rectangle with the given amount removed from its top edge.
Rectangle withLeft(ValueType newLeft) const noexcept
Returns a new rectangle with a different x position, but the same right-hand edge as this one.
Rectangle operator/=(FloatType scaleFactor) noexcept
Scales this rectangle by the given amount, centred around the origin.
Rectangle withBottomY(ValueType newBottomY) const noexcept
Returns a rectangle which has the same size and x-position as this one, but whose bottom edge has the...
Rectangle< float > toFloat() const noexcept
Casts this rectangle to a Rectangle<float>.
bool contains(ValueType xCoord, ValueType yCoord) const noexcept
Returns true if this coordinate is inside the rectangle.
Rectangle removeFromRight(ValueType amountToRemove) noexcept
Removes a strip from the right-hand edge of this rectangle, reducing this rectangle by the specified ...
ValueType getX() const noexcept
Returns the x coordinate of the rectangle's left-hand-side.
void setVerticalRange(Range< ValueType > range) noexcept
Changes the position of the rectangle's top and bottom edges.
Point< ValueType > getPosition() const noexcept
Returns the rectangle's top-left position as a Point.
Point< ValueType > getBottomLeft() const noexcept
Returns the rectangle's bottom-left position as a Point.
ValueType getCentreX() const noexcept
Returns the x coordinate of the rectangle's centre.
Rectangle< int > getSmallestIntegerContainer() const noexcept
Returns the smallest integer-aligned rectangle that completely contains this one.
Rectangle< double > toDouble() const noexcept
Casts this rectangle to a Rectangle<double>.
Rectangle< TargetType > toType() const noexcept
Casts this rectangle to a Rectangle with the given type.
Rectangle()=default
Creates a rectangle of zero size.
void setHorizontalRange(Range< ValueType > range) noexcept
Changes the position of the rectangle's left and right edges.
Rectangle removeFromBottom(ValueType amountToRemove) noexcept
Removes a strip from the bottom of this rectangle, reducing this rectangle by the specified amount an...
bool contains(Rectangle other) const noexcept
Returns true if this other rectangle is completely inside this one.
static Rectangle leftTopRightBottom(ValueType left, ValueType top, ValueType right, ValueType bottom) noexcept
Creates a Rectangle from a set of left, right, top, bottom coordinates.
Rectangle & operator-=(Point< ValueType > deltaPosition) noexcept
Moves this rectangle by a given amount.
Rectangle operator*(FloatType scaleFactor) const noexcept
Returns a rectangle that has been scaled by the given amount, centred around the origin.
Rectangle< int > toNearestInt() const noexcept
Casts this rectangle to a Rectangle<int>.
Point< ValueType > getTopLeft() const noexcept
Returns the rectangle's top-left position as a Point.
Rectangle getIntersection(Rectangle other) const noexcept
Returns the region that is the overlap between this and another rectangle.
static bool intersectRectangles(ValueType &x1, ValueType &y1, ValueType &w1, ValueType &h1, ValueType x2, ValueType y2, ValueType w2, ValueType h2) noexcept
Static utility to intersect two sets of rectangular coordinates.
Rectangle(ValueType width, ValueType height) noexcept
Creates a rectangle with a given size, and a position of (0, 0).
void setRight(ValueType newRight) noexcept
Adjusts the width so that the right-hand edge of the rectangle has this new value.
void setHeight(ValueType newHeight) noexcept
Changes the rectangle's height.
void setBottom(ValueType newBottom) noexcept
Adjusts the height so that the bottom edge of the rectangle has this new value.
ValueType getCentreY() const noexcept
Returns the y coordinate of the rectangle's centre.
Rectangle constrainedWithin(Rectangle areaToFitWithin) const noexcept
Tries to fit this rectangle within a target area, returning the result.
void setSize(ValueType newWidth, ValueType newHeight) noexcept
Changes the rectangle's size, leaving the position of its top-left corner unchanged.
void setPosition(ValueType newX, ValueType newY) noexcept
Changes the position of the rectangle's top-left corner (leaving its size unchanged).
Rectangle reduced(ValueType delta) const noexcept
Returns a rectangle that is smaller than this one by a given amount.
Rectangle< int > toNearestIntEdges() const noexcept
Casts this rectangle to a Rectangle<int>.
ValueType getBottom() const noexcept
Returns the y coordinate of the rectangle's bottom edge.
ValueType getWidth() const noexcept
Returns the width of the rectangle.
void setTop(ValueType newTop) noexcept
Moves the y position, adjusting the height so that the bottom edge remains in the same place.
Rectangle translated(ValueType deltaX, ValueType deltaY) const noexcept
Returns a rectangle which is the same as this one moved by a given amount.
Rectangle withCentre(Point< ValueType > newCentre) const noexcept
Returns a rectangle with the same size as this one, but a new centre position.
Rectangle withPosition(Point< ValueType > newPos) const noexcept
Returns a rectangle with the same size as this one, but a new position.
Rectangle withSizeKeepingCentre(ValueType newWidth, ValueType newHeight) const noexcept
Returns a rectangle with the same centre position as this one, but a new size.
Rectangle(const Rectangle &)=default
Creates a copy of another rectangle.
ValueType getAspectRatio(bool widthOverHeight=true) const noexcept
Returns the aspect ratio of the rectangle's width / height.
Point< ValueType > getTopRight() const noexcept
Returns the rectangle's top-right position as a Point.
Rectangle removeFromTop(ValueType amountToRemove) noexcept
Removes a strip from the top of this rectangle, reducing this rectangle by the specified amount and r...
void expand(ValueType deltaX, ValueType deltaY) noexcept
Expands the rectangle by a given amount.
static Rectangle fromString(StringRef stringVersion)
Parses a string containing a rectangle's details.
Rectangle withTrimmedRight(ValueType amountToRemove) const noexcept
Returns a version of this rectangle with the given amount removed from its right-hand edge.
static Rectangle findAreaContainingPoints(const Point< ValueType > *points, int numPoints) noexcept
Returns the smallest Rectangle that can contain a set of points.
Rectangle getUnion(Rectangle other) const noexcept
Returns the smallest rectangle that contains both this one and the one passed-in.
void setCentre(Point< ValueType > newCentre) noexcept
Changes the position of the rectangle's centre (leaving its size unchanged).
bool operator!=(const Rectangle &other) const noexcept
Returns true if the two rectangles are not identical.
bool enlargeIfAdjacent(Rectangle other) noexcept
If this rectangle merged with another one results in a simple rectangle, this will set this rectangle...
bool intersects(const Line< ValueType > &line) const noexcept
Returns true if any part of the given line lies inside this rectangle.
bool isFinite() const noexcept
Returns true if the rectangle's values are all finite numbers, i.e.
Rectangle reduced(ValueType deltaX, ValueType deltaY) const noexcept
Returns a rectangle that is smaller than this one by a given amount.
Rectangle removeFromLeft(ValueType amountToRemove) noexcept
Removes a strip from the left-hand edge of this rectangle, reducing this rectangle by the specified a...
Point< ValueType > getConstrainedPoint(Point< ValueType > point) const noexcept
Returns the nearest point to the specified point that lies within this rectangle.
Rectangle operator/(FloatType scaleFactor) const noexcept
Scales this rectangle by the given amount, centred around the origin.
bool contains(Point< ValueType > point) const noexcept
Returns true if this coordinate is inside the rectangle.
Rectangle operator*=(FloatType scaleFactor) noexcept
Scales this rectangle by the given amount, centred around the origin.
void translate(ValueType deltaX, ValueType deltaY) noexcept
Moves the rectangle's position by adding amount to its x and y coordinates.
void setPosition(Point< ValueType > newPos) noexcept
Changes the position of the rectangle's top-left corner (leaving its size unchanged).
Rectangle expanded(ValueType delta) const noexcept
Returns a rectangle that is larger than this one by a given amount.
Rectangle operator-(Point< ValueType > deltaPosition) const noexcept
Returns a rectangle which is the same as this one moved by a given amount.
ValueType getY() const noexcept
Returns the y coordinate of the rectangle's top edge.
Rectangle & operator=(const Rectangle &)=default
Creates a copy of another rectangle.
Rectangle withSize(ValueType newWidth, ValueType newHeight) const noexcept
Returns a rectangle with the same top-left position as this one, but a new size.
Point< ValueType > getRelativePoint(FloatType relativeX, FloatType relativeY) const noexcept
Returns a point within this rectangle, specified as proportional coordinates.
Rectangle getProportion(Rectangle< FloatType > proportionalRect) const noexcept
Returns a rectangle based on some proportional coordinates relative to this one.
~Rectangle()=default
Destructor.
bool reduceIfPartlyContainedIn(Rectangle other) noexcept
If after removing another rectangle from this one the result is a simple rectangle,...
bool intersectRectangle(Rectangle< ValueType > &rectangleToClip) const noexcept
Clips a rectangle so that it lies only within this one.
Rectangle withTrimmedLeft(ValueType amountToRemove) const noexcept
Returns a version of this rectangle with the given amount removed from its left-hand edge.
ValueType proportionOfWidth(FloatType proportion) const noexcept
Returns a proportion of the width of this rectangle.
Rectangle withWidth(ValueType newWidth) const noexcept
Returns a rectangle which has the same position and height as this one, but with a different width.
bool isEmpty() const noexcept
Returns true if the rectangle's width or height are zero or less.
Rectangle & operator+=(Point< ValueType > deltaPosition) noexcept
Moves this rectangle by a given amount.
Rectangle withRightX(ValueType newRightX) const noexcept
Returns a rectangle which has the same size and y-position as this one, but whose right-hand edge has...
void setX(ValueType newX) noexcept
Changes the rectangle's X coordinate.
Rectangle withZeroOrigin() const noexcept
Returns a rectangle whose size is the same as this one, but whose top-left position is (0,...
void setWidth(ValueType newWidth) noexcept
Changes the rectangle's width.
void reduce(ValueType deltaX, ValueType deltaY) noexcept
Shrinks the rectangle by a given amount.
Rectangle transformedBy(const AffineTransform &transform) const noexcept
Returns the smallest rectangle that can contain the shape created by applying a transform to this rec...
Rectangle withRight(ValueType newRight) const noexcept
Returns a new rectangle with a different right-hand edge position, but the same left-hand edge as thi...
bool operator==(const Rectangle &other) const noexcept
Returns true if the two rectangles are identical.
ValueType proportionOfHeight(FloatType proportion) const noexcept
Returns a proportion of the height of this rectangle.
Rectangle withTop(ValueType newTop) const noexcept
Returns a new rectangle with a different y position, but the same bottom edge as this one.
ValueType getHeight() const noexcept
Returns the height of the rectangle.
Rectangle withY(ValueType newY) const noexcept
Returns a rectangle which has the same size and x-position as this one, but with a different y-positi...
Rectangle withBottom(ValueType newBottom) const noexcept
Returns a new rectangle with a different bottom edge position, but the same top edge as this one.
void setCentre(ValueType newCentreX, ValueType newCentreY) noexcept
Changes the position of the rectangle's centre (leaving its size unchanged).
Rectangle withTrimmedBottom(ValueType amountToRemove) const noexcept
Returns a version of this rectangle with the given amount removed from its bottom edge.
bool intersectRectangle(ValueType &otherX, ValueType &otherY, ValueType &otherW, ValueType &otherH) const noexcept
Clips a set of rectangle coordinates so that they lie only within this one.
Rectangle expanded(ValueType deltaX, ValueType deltaY) const noexcept
Returns a rectangle that is larger than this one by a given amount.
void setBounds(ValueType newX, ValueType newY, ValueType newWidth, ValueType newHeight) noexcept
Changes all the rectangle's coordinates.
A special array for holding a list of strings.
int addTokens(StringRef stringToTokenise, bool preserveQuotedStrings)
Breaks up a string into tokens and adds them to this array.
A simple class for holding temporary references to a string literal or String.
void preallocateBytes(size_t numBytesNeeded)
Increases the string's internally allocated storage.
constexpr Type jmin(Type a, Type b)
Returns the smaller of two values.
constexpr bool exactlyEqual(Type a, Type b)
Equivalent to operator==, but suppresses float-equality warnings.
constexpr Type jmax(Type a, Type b)
Returns the larger of two values.
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Constrains a value to keep it within a given range.
bool juce_isfinite(NumericType value) noexcept
The isfinite() method seems to vary between platforms, so this is a platform-independent function for...
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
int roundToInt(const FloatType value) noexcept
Fast floating-point-to-integer conversion.