JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_Rectangle.h
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
29#ifndef DOXYGEN
30namespace detail
31{
32
33template <typename> struct Tag {};
34
35inline auto getNumericValue (StringRef s, Tag<int>) { return s.text.getIntValue32(); }
36inline auto getNumericValue (StringRef s, Tag<double>) { return s.text.getDoubleValue(); }
37inline auto getNumericValue (StringRef s, Tag<float>) { return static_cast<float> (s.text.getDoubleValue()); }
38
39template <typename ValueType>
40ValueType parseAfterSpace (StringRef s) noexcept
41{
42 return static_cast<ValueType> (getNumericValue (s.text.findEndOfWhitespace(),
44}
45
46inline int floorAsInt (int n) noexcept { return n; }
47inline int floorAsInt (float n) noexcept { return n > (float) std::numeric_limits<int>::min() ? (int) std::floor (n) : std::numeric_limits<int>::min(); }
48inline int floorAsInt (double n) noexcept { return n > (double) std::numeric_limits<int>::min() ? (int) std::floor (n) : std::numeric_limits<int>::min(); }
49
50inline int ceilAsInt (int n) noexcept { return n; }
51inline int ceilAsInt (float n) noexcept { return n < (float) std::numeric_limits<int>::max() ? (int) std::ceil (n) : std::numeric_limits<int>::max(); }
52inline int ceilAsInt (double n) noexcept { return n < (double) std::numeric_limits<int>::max() ? (int) std::ceil (n) : std::numeric_limits<int>::max(); }
53
54} // namespace detail
55#endif
56
57//==============================================================================
65template <typename ValueType>
67{
68public:
69 //==============================================================================
73 Rectangle() = default;
74
76 Rectangle (const Rectangle&) = default;
77
79 Rectangle (ValueType initialX, ValueType initialY,
80 ValueType width, ValueType height) noexcept
81 : pos (initialX, initialY),
82 w (width), h (height)
83 {
84 }
85
87 Rectangle (ValueType width, ValueType height) noexcept
88 : w (width), h (height)
89 {
90 }
91
94 : pos (jmin (corner1.x, corner2.x),
95 jmin (corner1.y, corner2.y)),
96 w (corner1.x - corner2.x),
97 h (corner1.y - corner2.y)
98 {
99 if (w < ValueType()) w = -w;
100 if (h < ValueType()) h = -h;
101 }
102
107 static Rectangle leftTopRightBottom (ValueType left, ValueType top,
108 ValueType right, ValueType bottom) noexcept
109 {
110 return { left, top, right - left, bottom - top };
111 }
112
114 Rectangle& operator= (const Rectangle&) = default;
115
117 ~Rectangle() = default;
118
119 //==============================================================================
121 bool isEmpty() const noexcept { return w <= ValueType() || h <= ValueType(); }
122
124 inline bool isFinite() const noexcept { return pos.isFinite() && juce_isfinite (w) && juce_isfinite (h); }
125
127 inline ValueType getX() const noexcept { return pos.x; }
128
130 inline ValueType getY() const noexcept { return pos.y; }
131
133 inline ValueType getWidth() const noexcept { return w; }
134
136 inline ValueType getHeight() const noexcept { return h; }
137
139 inline ValueType getRight() const noexcept { return pos.x + w; }
140
142 inline ValueType getBottom() const noexcept { return pos.y + h; }
143
145 ValueType getCentreX() const noexcept { return pos.x + w / (ValueType) 2; }
146
148 ValueType getCentreY() const noexcept { return pos.y + h / (ValueType) 2; }
149
151 Point<ValueType> getCentre() const noexcept { return { pos.x + w / (ValueType) 2,
152 pos.y + h / (ValueType) 2 }; }
153
157 ValueType getAspectRatio (bool widthOverHeight = true) const noexcept { return widthOverHeight ? w / h : h / w; }
158
159 //==============================================================================
161 inline Point<ValueType> getPosition() const noexcept { return pos; }
162
164 inline void setPosition (Point<ValueType> newPos) noexcept { pos = newPos; }
165
167 inline void setPosition (ValueType newX, ValueType newY) noexcept { pos.setXY (newX, newY); }
168
170 Point<ValueType> getTopLeft() const noexcept { return pos; }
171
173 Point<ValueType> getTopRight() const noexcept { return { pos.x + w, pos.y }; }
174
176 Point<ValueType> getBottomLeft() const noexcept { return { pos.x, pos.y + h }; }
177
179 Point<ValueType> getBottomRight() const noexcept { return { pos.x + w, pos.y + h }; }
180
183
186
188 void setSize (ValueType newWidth, ValueType newHeight) noexcept { w = newWidth; h = newHeight; }
189
191 void setBounds (ValueType newX, ValueType newY,
192 ValueType newWidth, ValueType newHeight) noexcept { pos.x = newX; pos.y = newY; w = newWidth; h = newHeight; }
193
195 inline void setX (ValueType newX) noexcept { pos.x = newX; }
196
198 inline void setY (ValueType newY) noexcept { pos.y = newY; }
199
201 inline void setWidth (ValueType newWidth) noexcept { w = newWidth; }
202
204 inline void setHeight (ValueType newHeight) noexcept { h = newHeight; }
205
207 inline void setCentre (ValueType newCentreX, ValueType newCentreY) noexcept { pos.x = newCentreX - w / (ValueType) 2;
208 pos.y = newCentreY - h / (ValueType) 2; }
209
212
214 void setHorizontalRange (Range<ValueType> range) noexcept { pos.x = range.getStart(); w = range.getLength(); }
215
217 void setVerticalRange (Range<ValueType> range) noexcept { pos.y = range.getStart(); h = range.getLength(); }
218
220 [[nodiscard]] Rectangle withX (ValueType newX) const noexcept { return { newX, pos.y, w, h }; }
221
223 [[nodiscard]] Rectangle withY (ValueType newY) const noexcept { return { pos.x, newY, w, h }; }
224
226 [[nodiscard]] Rectangle withRightX (ValueType newRightX) const noexcept { return { newRightX - w, pos.y, w, h }; }
227
229 [[nodiscard]] Rectangle withBottomY (ValueType newBottomY) const noexcept { return { pos.x, newBottomY - h, w, h }; }
230
232 [[nodiscard]] Rectangle withPosition (ValueType newX, ValueType newY) const noexcept { return { newX, newY, w, h }; }
233
235 [[nodiscard]] Rectangle withPosition (Point<ValueType> newPos) const noexcept { return { newPos.x, newPos.y, w, h }; }
236
238 [[nodiscard]] Rectangle withZeroOrigin() const noexcept { return { w, h }; }
239
241 [[nodiscard]] Rectangle withCentre (Point<ValueType> newCentre) const noexcept { return { newCentre.x - w / (ValueType) 2,
242 newCentre.y - h / (ValueType) 2, w, h }; }
243
245 [[nodiscard]] Rectangle withWidth (ValueType newWidth) const noexcept { return { pos.x, pos.y, jmax (ValueType(), newWidth), h }; }
246
248 [[nodiscard]] Rectangle withHeight (ValueType newHeight) const noexcept { return { pos.x, pos.y, w, jmax (ValueType(), newHeight) }; }
249
251 [[nodiscard]] Rectangle withSize (ValueType newWidth, ValueType newHeight) const noexcept { return { pos.x, pos.y, jmax (ValueType(), newWidth), jmax (ValueType(), newHeight) }; }
252
254 [[nodiscard]] Rectangle withSizeKeepingCentre (ValueType newWidth, ValueType newHeight) const noexcept { return { pos.x + (w - newWidth) / (ValueType) 2,
255 pos.y + (h - newHeight) / (ValueType) 2, newWidth, newHeight }; }
256
261 void setLeft (ValueType newLeft) noexcept { w = jmax (ValueType(), pos.x + w - newLeft); pos.x = newLeft; }
262
267 [[nodiscard]] Rectangle withLeft (ValueType newLeft) const noexcept { return { newLeft, pos.y, jmax (ValueType(), pos.x + w - newLeft), h }; }
268
273 void setTop (ValueType newTop) noexcept { h = jmax (ValueType(), pos.y + h - newTop); pos.y = newTop; }
274
279 [[nodiscard]] Rectangle withTop (ValueType newTop) const noexcept { return { pos.x, newTop, w, jmax (ValueType(), pos.y + h - newTop) }; }
280
285 void setRight (ValueType newRight) noexcept { pos.x = jmin (pos.x, newRight); w = newRight - pos.x; }
286
291 [[nodiscard]] Rectangle withRight (ValueType newRight) const noexcept { return { jmin (pos.x, newRight), pos.y, jmax (ValueType(), newRight - pos.x), h }; }
292
297 void setBottom (ValueType newBottom) noexcept { pos.y = jmin (pos.y, newBottom); h = newBottom - pos.y; }
298
303 [[nodiscard]] Rectangle withBottom (ValueType newBottom) const noexcept { return { pos.x, jmin (pos.y, newBottom), w, jmax (ValueType(), newBottom - pos.y) }; }
304
306 [[nodiscard]] Rectangle withTrimmedLeft (ValueType amountToRemove) const noexcept { return withLeft (pos.x + amountToRemove); }
307
309 [[nodiscard]] Rectangle withTrimmedRight (ValueType amountToRemove) const noexcept { return withWidth (w - amountToRemove); }
310
312 [[nodiscard]] Rectangle withTrimmedTop (ValueType amountToRemove) const noexcept { return withTop (pos.y + amountToRemove); }
313
315 [[nodiscard]] Rectangle withTrimmedBottom (ValueType amountToRemove) const noexcept { return withHeight (h - amountToRemove); }
316
317 //==============================================================================
319 void translate (ValueType deltaX,
320 ValueType deltaY) noexcept
321 {
322 pos.x += deltaX;
323 pos.y += deltaY;
324 }
325
327 [[nodiscard]] Rectangle translated (ValueType deltaX,
328 ValueType deltaY) const noexcept
329 {
330 return { pos.x + deltaX, pos.y + deltaY, w, h };
331 }
332
335 {
336 return { pos.x + deltaPosition.x, pos.y + deltaPosition.y, w, h };
337 }
338
341 {
342 pos += deltaPosition;
343 return *this;
344 }
345
348 {
349 return { pos.x - deltaPosition.x, pos.y - deltaPosition.y, w, h };
350 }
351
354 {
355 pos -= deltaPosition;
356 return *this;
357 }
358
364 template <typename FloatType>
365 Rectangle operator* (FloatType scaleFactor) const noexcept
366 {
367 Rectangle r (*this);
368 r *= scaleFactor;
369 return r;
370 }
371
377 template <typename FloatType>
378 Rectangle operator*= (FloatType scaleFactor) noexcept
379 {
380 Rectangle<FloatType> ((FloatType) pos.x * scaleFactor,
381 (FloatType) pos.y * scaleFactor,
382 (FloatType) w * scaleFactor,
383 (FloatType) h * scaleFactor).copyWithRounding (*this);
384 return *this;
385 }
386
392 template <typename FloatType>
394 {
395 Rectangle<FloatType> ((FloatType) pos.x * scaleFactor.x,
396 (FloatType) pos.y * scaleFactor.y,
397 (FloatType) w * scaleFactor.x,
398 (FloatType) h * scaleFactor.y).copyWithRounding (*this);
399 return *this;
400 }
401
403 template <typename FloatType>
404 Rectangle operator/ (FloatType scaleFactor) const noexcept
405 {
406 Rectangle r (*this);
407 r /= scaleFactor;
408 return r;
409 }
410
412 template <typename FloatType>
413 Rectangle operator/= (FloatType scaleFactor) noexcept
414 {
415 Rectangle<FloatType> ((FloatType) pos.x / scaleFactor,
416 (FloatType) pos.y / scaleFactor,
417 (FloatType) w / scaleFactor,
418 (FloatType) h / scaleFactor).copyWithRounding (*this);
419 return *this;
420 }
421
423 template <typename FloatType>
425 {
426 Rectangle<FloatType> ((FloatType) pos.x / scaleFactor.x,
427 (FloatType) pos.y / scaleFactor.y,
428 (FloatType) w / scaleFactor.x,
429 (FloatType) h / scaleFactor.y).copyWithRounding (*this);
430 return *this;
431 }
432
438 void expand (ValueType deltaX,
439 ValueType deltaY) noexcept
440 {
441 auto nw = jmax (ValueType(), w + deltaX * 2);
442 auto nh = jmax (ValueType(), h + deltaY * 2);
443 setBounds (pos.x - deltaX, pos.y - deltaY, nw, nh);
444 }
445
451 [[nodiscard]] Rectangle expanded (ValueType deltaX,
452 ValueType deltaY) const noexcept
453 {
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 };
457 }
458
464 [[nodiscard]] Rectangle expanded (ValueType delta) const noexcept
465 {
466 return expanded (delta, delta);
467 }
468
474 void reduce (ValueType deltaX,
475 ValueType deltaY) noexcept
476 {
477 expand (-deltaX, -deltaY);
478 }
479
485 [[nodiscard]] Rectangle reduced (ValueType deltaX,
486 ValueType deltaY) const noexcept
487 {
488 return expanded (-deltaX, -deltaY);
489 }
490
496 [[nodiscard]] Rectangle reduced (ValueType delta) const noexcept
497 {
498 return reduced (delta, delta);
499 }
500
511 {
512 const Rectangle r (pos.x, pos.y, w, jmin (amountToRemove, h));
513 pos.y += r.h; h -= r.h;
514 return r;
515 }
516
527 {
528 const Rectangle r (pos.x, pos.y, jmin (amountToRemove, w), h);
529 pos.x += r.w; w -= r.w;
530 return r;
531 }
532
543 {
545 const Rectangle r (pos.x + w - amountToRemove, pos.y, amountToRemove, h);
546 w -= amountToRemove;
547 return r;
548 }
549
560 {
562 const Rectangle r (pos.x, pos.y + h - amountToRemove, w, amountToRemove);
563 h -= amountToRemove;
564 return r;
565 }
566
567 //==============================================================================
570 {
571 return { jlimit (pos.x, pos.x + w, point.x),
572 jlimit (pos.y, pos.y + h, point.y) };
573 }
574
580 template <typename FloatType>
581 Point<ValueType> getRelativePoint (FloatType relativeX, FloatType relativeY) const noexcept
582 {
583 return { pos.x + static_cast<ValueType> ((FloatType) w * relativeX),
584 pos.y + static_cast<ValueType> ((FloatType) h * relativeY) };
585 }
586
588 template <typename FloatType>
589 ValueType proportionOfWidth (FloatType proportion) const noexcept
590 {
591 return static_cast<ValueType> ((FloatType) w * proportion);
592 }
593
595 template <typename FloatType>
596 ValueType proportionOfHeight (FloatType proportion) const noexcept
597 {
598 return static_cast<ValueType> ((FloatType) h * proportion);
599 }
600
605 template <typename FloatType>
607 {
608 return { pos.x + static_cast<ValueType> (w * proportionalRect.pos.x),
609 pos.y + static_cast<ValueType> (h * proportionalRect.pos.y),
612 }
613
614 //==============================================================================
616 bool operator== (const Rectangle& other) const noexcept
617 {
618 const auto tie = [] (const Rectangle& r) { return std::tie (r.pos, r.w, r.h); };
619 return tie (*this) == tie (other);
620 }
621
623 bool operator!= (const Rectangle& other) const noexcept { return ! operator== (other); }
624
626 bool contains (ValueType xCoord, ValueType yCoord) const noexcept
627 {
628 return xCoord >= pos.x && yCoord >= pos.y && xCoord < pos.x + w && yCoord < pos.y + h;
629 }
630
632 bool contains (Point<ValueType> point) const noexcept
633 {
634 return point.x >= pos.x && point.y >= pos.y && point.x < pos.x + w && point.y < pos.y + h;
635 }
636
638 bool contains (Rectangle other) const noexcept
639 {
640 return pos.x <= other.pos.x && pos.y <= other.pos.y
641 && pos.x + w >= other.pos.x + other.w && pos.y + h >= other.pos.y + other.h;
642 }
643
645 bool intersects (Rectangle other) const noexcept
646 {
647 return pos.x + w > other.pos.x
648 && pos.y + h > other.pos.y
649 && pos.x < other.pos.x + other.w
650 && pos.y < other.pos.y + other.h
651 && w > ValueType() && h > ValueType()
652 && other.w > ValueType() && other.h > ValueType();
653 }
654
656 bool intersects (const Line<ValueType>& line) const noexcept
657 {
658 return contains (line.getStart()) || contains (line.getEnd())
659 || line.intersects (Line<ValueType> (getTopLeft(), getTopRight()))
660 || line.intersects (Line<ValueType> (getTopRight(), getBottomRight()))
661 || line.intersects (Line<ValueType> (getBottomRight(), getBottomLeft()))
662 || line.intersects (Line<ValueType> (getBottomLeft(), getTopLeft()));
663 }
664
669 {
670 auto nx = jmax (pos.x, other.pos.x);
671 auto ny = jmax (pos.y, other.pos.y);
672 auto nw = jmin (pos.x + w, other.pos.x + other.w) - nx;
673
674 if (nw >= ValueType())
675 {
676 auto nh = jmin (pos.y + h, other.pos.y + other.h) - ny;
677
678 if (nh >= ValueType())
679 return { nx, ny, nw, nh };
680 }
681
682 return {};
683 }
684
689 bool intersectRectangle (ValueType& otherX, ValueType& otherY, ValueType& otherW, ValueType& otherH) const noexcept
690 {
691 auto maxX = jmax (otherX, pos.x);
692 otherW = jmin (otherX + otherW, pos.x + w) - maxX;
693
694 if (otherW > ValueType())
695 {
696 auto maxY = jmax (otherY, pos.y);
697 otherH = jmin (otherY + otherH, pos.y + h) - maxY;
698
699 if (otherH > ValueType())
700 {
701 otherX = maxX; otherY = maxY;
702 return true;
703 }
704 }
705
706 return false;
707 }
708
717
724 {
725 if (other.isEmpty()) return *this;
726 if (isEmpty()) return other;
727
728 auto newX = jmin (pos.x, other.pos.x);
729 auto newY = jmin (pos.y, other.pos.y);
730
731 return { newX, newY,
732 jmax (pos.x + w, other.pos.x + other.w) - newX,
733 jmax (pos.y + h, other.pos.y + other.h) - newY };
734 }
735
743 {
744 if (exactlyEqual (pos.x, other.pos.x) && exactlyEqual (getRight(), other.getRight())
745 && (other.getBottom() >= pos.y && other.pos.y <= getBottom()))
746 {
747 auto newY = jmin (pos.y, other.pos.y);
748 h = jmax (getBottom(), other.getBottom()) - newY;
749 pos.y = newY;
750 return true;
751 }
752
753 if (exactlyEqual (pos.y, other.pos.y) && exactlyEqual (getBottom(), other.getBottom())
754 && (other.getRight() >= pos.x && other.pos.x <= getRight()))
755 {
756 auto newX = jmin (pos.x, other.pos.x);
757 w = jmax (getRight(), other.getRight()) - newX;
758 pos.x = newX;
759 return true;
760 }
761
762 return false;
763 }
764
772 {
773 int inside = 0;
774 auto otherR = other.getRight();
775 if (pos.x >= other.pos.x && pos.x < otherR) inside = 1;
776 auto otherB = other.getBottom();
777 if (pos.y >= other.pos.y && pos.y < otherB) inside |= 2;
778 auto r = pos.x + w;
779 if (r >= other.pos.x && r < otherR) inside |= 4;
780 auto b = pos.y + h;
781 if (b >= other.pos.y && b < otherB) inside |= 8;
782
783 switch (inside)
784 {
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;
789 default: break;
790 }
791
792 return false;
793 }
794
803 {
804 auto newPos = areaToFitWithin.withSize (areaToFitWithin.getWidth() - w,
805 areaToFitWithin.getHeight() - h)
806 .getConstrainedPoint (pos);
807
808 return { newPos.x, newPos.y,
809 jmin (w, areaToFitWithin.getWidth()),
810 jmin (h, areaToFitWithin.getHeight()) };
811 }
812
818 [[nodiscard]] Rectangle transformedBy (const AffineTransform& transform) const noexcept
819 {
821
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);
826
827 transform.transformPoints (x1, y1, x2, y2);
828 transform.transformPoints (x3, y3, x4, y4);
829
830 auto rx1 = jmin (x1, x2, x3, x4);
831 auto rx2 = jmax (x1, x2, x3, x4);
832 auto ry1 = jmin (y1, y2, y3, y4);
833 auto ry2 = jmax (y1, y2, y3, y4);
834
835 Rectangle r;
836 Rectangle<FloatType> (rx1, ry1, rx2 - rx1, ry2 - ry1).copyWithRounding (r);
837 return r;
838 }
839
845 {
846 return Rectangle<int>::leftTopRightBottom (detail::floorAsInt (pos.x),
847 detail::floorAsInt (pos.y),
848 detail::ceilAsInt (pos.x + w),
849 detail::ceilAsInt (pos.y + h));
850 }
851
858 {
859 return { roundToInt (pos.x), roundToInt (pos.y),
860 roundToInt (w), roundToInt (h) };
861 }
862
873
878 {
879 return { static_cast<float> (pos.x), static_cast<float> (pos.y),
880 static_cast<float> (w), static_cast<float> (h) };
881 }
882
887 {
888 return { static_cast<double> (pos.x), static_cast<double> (pos.y),
889 static_cast<double> (w), static_cast<double> (h) };
890 }
891
896 template <typename TargetType>
898 {
900 copyWithRounding (r);
901 return r;
902 }
903
906 {
907 if (numPoints <= 0)
908 return {};
909
910 auto minX = points[0].x;
911 auto maxX = minX;
912 auto minY = points[0].y;
913 auto maxY = minY;
914
915 for (int i = 1; i < numPoints; ++i)
916 {
917 minX = jmin (minX, points[i].x);
918 maxX = jmax (maxX, points[i].x);
919 minY = jmin (minY, points[i].y);
920 maxY = jmax (maxY, points[i].y);
921 }
922
923 return { minX, minY, maxX - minX, maxY - minY };
924 }
925
926 //==============================================================================
931 static bool intersectRectangles (ValueType& x1, ValueType& y1, ValueType& w1, ValueType& h1,
932 ValueType x2, ValueType y2, ValueType w2, ValueType h2) noexcept
933 {
934 auto x = jmax (x1, x2);
935 w1 = jmin (x1 + w1, x2 + w2) - x;
936
937 if (w1 > ValueType())
938 {
939 auto y = jmax (y1, y2);
940 h1 = jmin (y1 + h1, y2 + h2) - y;
941
942 if (h1 > ValueType())
943 {
944 x1 = x; y1 = y;
945 return true;
946 }
947 }
948
949 return false;
950 }
951
952 //==============================================================================
963 {
964 String s;
965 s.preallocateBytes (32);
966 s << pos.x << ' ' << pos.y << ' ' << w << ' ' << h;
967 return s;
968 }
969
981 {
983 toks.addTokens (stringVersion.text.findEndOfWhitespace(), ",; \t\r\n", "");
984
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]) };
989 }
990
991 #ifndef DOXYGEN
992 [[deprecated ("This has been renamed to transformedBy in order to match the method names used in the Point class.")]]
993 Rectangle transformed (const AffineTransform& t) const noexcept { return transformedBy (t); }
994 #endif
995
996private:
997 template <typename OtherType> friend class Rectangle;
998
999 Point<ValueType> pos;
1000 ValueType w {}, h {};
1001
1002 void copyWithRounding (Rectangle<int>& result) const noexcept { result = getSmallestIntegerContainer(); }
1003 void copyWithRounding (Rectangle<float>& result) const noexcept { result = toFloat(); }
1004 void copyWithRounding (Rectangle<double>& result) const noexcept { result = toDouble(); }
1005};
1006
1007} // namespace juce
T ceil(T... args)
Represents a 2D affine-transformation matrix.
Represents a line.
Definition juce_Line.h:47
A pair of (x, y) coordinates.
Definition juce_Point.h:42
A general-purpose range object, that simply represents any linear range with a start and end point.
Definition juce_Range.h:40
static Range withStartAndLength(const ValueType startValue, const ValueType length) noexcept
Returns a range with a given start and length.
Definition juce_Range.h:66
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.
The JUCE String class!
Definition juce_String.h:53
void preallocateBytes(size_t numBytesNeeded)
Increases the string's internally allocated storage.
T floor(T... args)
typedef int
typedef float
T max(T... args)
T min(T... args)
JUCE Namespace.
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...
Definition juce_Memory.h:88
int roundToInt(const FloatType value) noexcept
Fast floating-point-to-integer conversion.
T tie(T... args)
y1