LMMS
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 //==============================================================================
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
171
174
177
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
211 inline void setCentre (Point<ValueType> newCentre) noexcept { setCentre (newCentre.x, newCentre.y); }
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 JUCE_NODISCARD Rectangle withX (ValueType newX) const noexcept { return { newX, pos.y, w, h }; }
221
223 JUCE_NODISCARD Rectangle withY (ValueType newY) const noexcept { return { pos.x, newY, w, h }; }
224
226 JUCE_NODISCARD Rectangle withRightX (ValueType newRightX) const noexcept { return { newRightX - w, pos.y, w, h }; }
227
229 JUCE_NODISCARD Rectangle withBottomY (ValueType newBottomY) const noexcept { return { pos.x, newBottomY - h, w, h }; }
230
232 JUCE_NODISCARD Rectangle withPosition (ValueType newX, ValueType newY) const noexcept { return { newX, newY, w, h }; }
233
235 JUCE_NODISCARD Rectangle withPosition (Point<ValueType> newPos) const noexcept { return { newPos.x, newPos.y, w, h }; }
236
239
241 JUCE_NODISCARD Rectangle withCentre (Point<ValueType> newCentre) const noexcept { return { newCentre.x - w / (ValueType) 2,
242 newCentre.y - h / (ValueType) 2, w, h }; }
243
245 JUCE_NODISCARD Rectangle withWidth (ValueType newWidth) const noexcept { return { pos.x, pos.y, jmax (ValueType(), newWidth), h }; }
246
248 JUCE_NODISCARD Rectangle withHeight (ValueType newHeight) const noexcept { return { pos.x, pos.y, w, jmax (ValueType(), newHeight) }; }
249
251 JUCE_NODISCARD Rectangle withSize (ValueType newWidth, ValueType newHeight) const noexcept { return { pos.x, pos.y, jmax (ValueType(), newWidth), jmax (ValueType(), newHeight) }; }
252
254 JUCE_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 JUCE_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 JUCE_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 JUCE_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 JUCE_NODISCARD Rectangle withBottom (ValueType newBottom) const noexcept { return { pos.x, jmin (pos.y, newBottom), w, jmax (ValueType(), newBottom - pos.y) }; }
304
306 JUCE_NODISCARD Rectangle withTrimmedLeft (ValueType amountToRemove) const noexcept { return withLeft (pos.x + amountToRemove); }
307
309 JUCE_NODISCARD Rectangle withTrimmedRight (ValueType amountToRemove) const noexcept { return withWidth (w - amountToRemove); }
310
312 JUCE_NODISCARD Rectangle withTrimmedTop (ValueType amountToRemove) const noexcept { return withTop (pos.y + amountToRemove); }
313
315 JUCE_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 Rectangle translated (ValueType deltaX,
328 ValueType deltaY) const noexcept
329 {
330 return { pos.x + deltaX, pos.y + deltaY, w, h };
331 }
332
334 Rectangle operator+ (Point<ValueType> deltaPosition) const noexcept
335 {
336 return { pos.x + deltaPosition.x, pos.y + deltaPosition.y, w, h };
337 }
338
340 Rectangle& operator+= (Point<ValueType> deltaPosition) noexcept
341 {
342 pos += deltaPosition;
343 return *this;
344 }
345
347 Rectangle operator- (Point<ValueType> deltaPosition) const noexcept
348 {
349 return { pos.x - deltaPosition.x, pos.y - deltaPosition.y, w, h };
350 }
351
353 Rectangle& operator-= (Point<ValueType> deltaPosition) noexcept
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>
393 Rectangle operator*= (Point<FloatType> scaleFactor) noexcept
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>
424 Rectangle operator/= (Point<FloatType> scaleFactor) noexcept
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 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 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 Rectangle reduced (ValueType deltaX,
486 ValueType deltaY) const noexcept
487 {
488 return expanded (-deltaX, -deltaY);
489 }
490
496 Rectangle reduced (ValueType delta) const noexcept
497 {
498 return reduced (delta, delta);
499 }
500
510 Rectangle removeFromTop (ValueType amountToRemove) noexcept
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
526 Rectangle removeFromLeft (ValueType amountToRemove) noexcept
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
542 Rectangle removeFromRight (ValueType amountToRemove) noexcept
543 {
544 amountToRemove = jmin (amountToRemove, w);
545 const Rectangle r (pos.x + w - amountToRemove, pos.y, amountToRemove, h);
546 w -= amountToRemove;
547 return r;
548 }
549
559 Rectangle removeFromBottom (ValueType amountToRemove) noexcept
560 {
561 amountToRemove = jmin (amountToRemove, h);
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>
606 Rectangle getProportion (Rectangle<FloatType> proportionalRect) const noexcept
607 {
608 return { pos.x + static_cast<ValueType> (w * proportionalRect.pos.x),
609 pos.y + static_cast<ValueType> (h * proportionalRect.pos.y),
610 proportionOfWidth (proportionalRect.w),
611 proportionOfHeight (proportionalRect.h) };
612 }
613
614 //==============================================================================
616 bool operator== (const Rectangle& other) const noexcept { return pos == other.pos && w == other.w && h == other.h; }
617
619 bool operator!= (const Rectangle& other) const noexcept { return pos != other.pos || w != other.w || h != other.h; }
620
622 bool contains (ValueType xCoord, ValueType yCoord) const noexcept
623 {
624 return xCoord >= pos.x && yCoord >= pos.y && xCoord < pos.x + w && yCoord < pos.y + h;
625 }
626
628 bool contains (Point<ValueType> point) const noexcept
629 {
630 return point.x >= pos.x && point.y >= pos.y && point.x < pos.x + w && point.y < pos.y + h;
631 }
632
634 bool contains (Rectangle other) const noexcept
635 {
636 return pos.x <= other.pos.x && pos.y <= other.pos.y
637 && pos.x + w >= other.pos.x + other.w && pos.y + h >= other.pos.y + other.h;
638 }
639
641 bool intersects (Rectangle other) const noexcept
642 {
643 return pos.x + w > other.pos.x
644 && pos.y + h > other.pos.y
645 && pos.x < other.pos.x + other.w
646 && pos.y < other.pos.y + other.h
647 && w > ValueType() && h > ValueType()
648 && other.w > ValueType() && other.h > ValueType();
649 }
650
652 bool intersects (const Line<ValueType>& line) const noexcept
653 {
654 return contains (line.getStart()) || contains (line.getEnd())
655 || line.intersects (Line<ValueType> (getTopLeft(), getTopRight()))
656 || line.intersects (Line<ValueType> (getTopRight(), getBottomRight()))
657 || line.intersects (Line<ValueType> (getBottomRight(), getBottomLeft()))
658 || line.intersects (Line<ValueType> (getBottomLeft(), getTopLeft()));
659 }
660
664 Rectangle getIntersection (Rectangle other) const noexcept
665 {
666 auto nx = jmax (pos.x, other.pos.x);
667 auto ny = jmax (pos.y, other.pos.y);
668 auto nw = jmin (pos.x + w, other.pos.x + other.w) - nx;
669
670 if (nw >= ValueType())
671 {
672 auto nh = jmin (pos.y + h, other.pos.y + other.h) - ny;
673
674 if (nh >= ValueType())
675 return { nx, ny, nw, nh };
676 }
677
678 return {};
679 }
680
685 bool intersectRectangle (ValueType& otherX, ValueType& otherY, ValueType& otherW, ValueType& otherH) const noexcept
686 {
687 auto maxX = jmax (otherX, pos.x);
688 otherW = jmin (otherX + otherW, pos.x + w) - maxX;
689
690 if (otherW > ValueType())
691 {
692 auto maxY = jmax (otherY, pos.y);
693 otherH = jmin (otherY + otherH, pos.y + h) - maxY;
694
695 if (otherH > ValueType())
696 {
697 otherX = maxX; otherY = maxY;
698 return true;
699 }
700 }
701
702 return false;
703 }
704
708 bool intersectRectangle (Rectangle<ValueType>& rectangleToClip) const noexcept
709 {
710 return intersectRectangle (rectangleToClip.pos.x, rectangleToClip.pos.y,
711 rectangleToClip.w, rectangleToClip.h);
712 }
713
719 Rectangle getUnion (Rectangle other) const noexcept
720 {
721 if (other.isEmpty()) return *this;
722 if (isEmpty()) return other;
723
724 auto newX = jmin (pos.x, other.pos.x);
725 auto newY = jmin (pos.y, other.pos.y);
726
727 return { newX, newY,
728 jmax (pos.x + w, other.pos.x + other.w) - newX,
729 jmax (pos.y + h, other.pos.y + other.h) - newY };
730 }
731
738 bool enlargeIfAdjacent (Rectangle other) noexcept
739 {
740 if (pos.x == other.pos.x && getRight() == other.getRight()
741 && (other.getBottom() >= pos.y && other.pos.y <= getBottom()))
742 {
743 auto newY = jmin (pos.y, other.pos.y);
744 h = jmax (getBottom(), other.getBottom()) - newY;
745 pos.y = newY;
746 return true;
747 }
748
749 if (pos.y == other.pos.y && getBottom() == other.getBottom()
750 && (other.getRight() >= pos.x && other.pos.x <= getRight()))
751 {
752 auto newX = jmin (pos.x, other.pos.x);
753 w = jmax (getRight(), other.getRight()) - newX;
754 pos.x = newX;
755 return true;
756 }
757
758 return false;
759 }
760
768 {
769 int inside = 0;
770 auto otherR = other.getRight();
771 if (pos.x >= other.pos.x && pos.x < otherR) inside = 1;
772 auto otherB = other.getBottom();
773 if (pos.y >= other.pos.y && pos.y < otherB) inside |= 2;
774 auto r = pos.x + w;
775 if (r >= other.pos.x && r < otherR) inside |= 4;
776 auto b = pos.y + h;
777 if (b >= other.pos.y && b < otherB) inside |= 8;
778
779 switch (inside)
780 {
781 case 1 + 2 + 8: w = r - otherR; pos.x = otherR; return true;
782 case 1 + 2 + 4: h = b - otherB; pos.y = otherB; return true;
783 case 2 + 4 + 8: w = other.pos.x - pos.x; return true;
784 case 1 + 4 + 8: h = other.pos.y - pos.y; return true;
785 default: break;
786 }
787
788 return false;
789 }
790
798 Rectangle constrainedWithin (Rectangle areaToFitWithin) const noexcept
799 {
800 auto newPos = areaToFitWithin.withSize (areaToFitWithin.getWidth() - w,
801 areaToFitWithin.getHeight() - h)
802 .getConstrainedPoint (pos);
803
804 return { newPos.x, newPos.y,
805 jmin (w, areaToFitWithin.getWidth()),
806 jmin (h, areaToFitWithin.getHeight()) };
807 }
808
814 Rectangle transformedBy (const AffineTransform& transform) const noexcept
815 {
816 using FloatType = typename TypeHelpers::SmallestFloatType<ValueType>::type;
817
818 auto x1 = static_cast<FloatType> (pos.x), y1 = static_cast<FloatType> (pos.y);
819 auto x2 = static_cast<FloatType> (pos.x + w), y2 = static_cast<FloatType> (pos.y);
820 auto x3 = static_cast<FloatType> (pos.x), y3 = static_cast<FloatType> (pos.y + h);
821 auto x4 = static_cast<FloatType> (x2), y4 = static_cast<FloatType> (y3);
822
823 transform.transformPoints (x1, y1, x2, y2);
824 transform.transformPoints (x3, y3, x4, y4);
825
826 auto rx1 = jmin (x1, x2, x3, x4);
827 auto rx2 = jmax (x1, x2, x3, x4);
828 auto ry1 = jmin (y1, y2, y3, y4);
829 auto ry2 = jmax (y1, y2, y3, y4);
830
831 Rectangle r;
832 Rectangle<FloatType> (rx1, ry1, rx2 - rx1, ry2 - ry1).copyWithRounding (r);
833 return r;
834 }
835
847
854 {
855 return { roundToInt (pos.x), roundToInt (pos.y),
856 roundToInt (w), roundToInt (h) };
857 }
858
869
874 {
875 return { static_cast<float> (pos.x), static_cast<float> (pos.y),
876 static_cast<float> (w), static_cast<float> (h) };
877 }
878
883 {
884 return { static_cast<double> (pos.x), static_cast<double> (pos.y),
885 static_cast<double> (w), static_cast<double> (h) };
886 }
887
892 template <typename TargetType>
899
901 static Rectangle findAreaContainingPoints (const Point<ValueType>* points, int numPoints) noexcept
902 {
903 if (numPoints <= 0)
904 return {};
905
906 auto minX = points[0].x;
907 auto maxX = minX;
908 auto minY = points[0].y;
909 auto maxY = minY;
910
911 for (int i = 1; i < numPoints; ++i)
912 {
913 minX = jmin (minX, points[i].x);
914 maxX = jmax (maxX, points[i].x);
915 minY = jmin (minY, points[i].y);
916 maxY = jmax (maxY, points[i].y);
917 }
918
919 return { minX, minY, maxX - minX, maxY - minY };
920 }
921
922 //==============================================================================
927 static bool intersectRectangles (ValueType& x1, ValueType& y1, ValueType& w1, ValueType& h1,
928 ValueType x2, ValueType y2, ValueType w2, ValueType h2) noexcept
929 {
930 auto x = jmax (x1, x2);
931 w1 = jmin (x1 + w1, x2 + w2) - x;
932
933 if (w1 > ValueType())
934 {
935 auto y = jmax (y1, y2);
936 h1 = jmin (y1 + h1, y2 + h2) - y;
937
938 if (h1 > ValueType())
939 {
940 x1 = x; y1 = y;
941 return true;
942 }
943 }
944
945 return false;
946 }
947
948 //==============================================================================
959 {
960 String s;
961 s.preallocateBytes (32);
962 s << pos.x << ' ' << pos.y << ' ' << w << ' ' << h;
963 return s;
964 }
965
976 static Rectangle fromString (StringRef stringVersion)
977 {
978 StringArray toks;
979 toks.addTokens (stringVersion.text.findEndOfWhitespace(), ",; \t\r\n", "");
980
981 return { detail::parseAfterSpace<ValueType> (toks[0]),
985 }
986
987 #ifndef DOXYGEN
988 [[deprecated ("This has been renamed to transformedBy in order to match the method names used in the Point class.")]]
989 Rectangle transformed (const AffineTransform& t) const noexcept { return transformedBy (t); }
990 #endif
991
992private:
993 template <typename OtherType> friend class Rectangle;
994
996 ValueType w {}, h {};
997
999 void copyWithRounding (Rectangle<float>& result) const noexcept { result = toFloat(); }
1001};
1002
1003} // namespace juce
Type jmin(const Type a, const Type b)
Definition MathsFunctions.h:60
Type jmax(const Type a, const Type b)
Definition MathsFunctions.h:48
#define noexcept
Definition DistrhoDefines.h:72
Definition juce_AffineTransform.h:43
Definition juce_Line.h:47
Definition juce_Point.h:42
Definition juce_Range.h:40
static JUCE_NODISCARD Range withStartAndLength(const ValueType startValue, const ValueType length) noexcept
Definition juce_Range.h:66
Definition juce_Rectangle.h:67
Rectangle(ValueType initialX, ValueType initialY, ValueType width, ValueType height) noexcept
Definition juce_Rectangle.h:79
ValueType getRight() const noexcept
Definition juce_Rectangle.h:139
Range< ValueType > getHorizontalRange() const noexcept
Definition juce_Rectangle.h:182
JUCE_NODISCARD Rectangle withZeroOrigin() const noexcept
Definition juce_Rectangle.h:238
Range< ValueType > getVerticalRange() const noexcept
Definition juce_Rectangle.h:185
void setLeft(ValueType newLeft) noexcept
Definition juce_Rectangle.h:261
JUCE_NODISCARD Rectangle withTop(ValueType newTop) const noexcept
Definition juce_Rectangle.h:279
String toString() const
Definition juce_Rectangle.h:958
Point< ValueType > getCentre() const noexcept
Definition juce_Rectangle.h:151
Point< ValueType > getBottomRight() const noexcept
Definition juce_Rectangle.h:179
bool intersects(Rectangle other) const noexcept
Definition juce_Rectangle.h:641
Rectangle(Point< ValueType > corner1, Point< ValueType > corner2) noexcept
Definition juce_Rectangle.h:93
void setHeight(ValueType newHeight) noexcept
Definition juce_Rectangle.h:204
void copyWithRounding(Rectangle< double > &result) const noexcept
Definition juce_Rectangle.h:1000
Rectangle< float > toFloat() const noexcept
Definition juce_Rectangle.h:873
bool contains(ValueType xCoord, ValueType yCoord) const noexcept
Definition juce_Rectangle.h:622
Rectangle removeFromRight(ValueType amountToRemove) noexcept
Definition juce_Rectangle.h:542
void setVerticalRange(Range< ValueType > range) noexcept
Definition juce_Rectangle.h:217
Point< ValueType > getPosition() const noexcept
Definition juce_Rectangle.h:161
Point< ValueType > getBottomLeft() const noexcept
Definition juce_Rectangle.h:176
ValueType getCentreX() const noexcept
Definition juce_Rectangle.h:145
Rectangle< int > getSmallestIntegerContainer() const noexcept
Definition juce_Rectangle.h:840
Rectangle< double > toDouble() const noexcept
Definition juce_Rectangle.h:882
Rectangle< TargetType > toType() const noexcept
Definition juce_Rectangle.h:893
Rectangle()=default
JUCE_NODISCARD Rectangle withPosition(Point< ValueType > newPos) const noexcept
Definition juce_Rectangle.h:235
void setHorizontalRange(Range< ValueType > range) noexcept
Definition juce_Rectangle.h:214
Rectangle removeFromBottom(ValueType amountToRemove) noexcept
Definition juce_Rectangle.h:559
bool contains(Rectangle other) const noexcept
Definition juce_Rectangle.h:634
static Rectangle leftTopRightBottom(ValueType left, ValueType top, ValueType right, ValueType bottom) noexcept
Definition juce_Rectangle.h:107
Point< ValueType > pos
Definition juce_Rectangle.h:995
Rectangle< int > toNearestInt() const noexcept
Definition juce_Rectangle.h:853
Point< ValueType > getTopLeft() const noexcept
Definition juce_Rectangle.h:170
Rectangle getIntersection(Rectangle other) const noexcept
Definition juce_Rectangle.h:664
static bool intersectRectangles(ValueType &x1, ValueType &y1, ValueType &w1, ValueType &h1, ValueType x2, ValueType y2, ValueType w2, ValueType h2) noexcept
Definition juce_Rectangle.h:927
ValueType getHeight() const noexcept
Definition juce_Rectangle.h:136
JUCE_NODISCARD Rectangle withTrimmedBottom(ValueType amountToRemove) const noexcept
Definition juce_Rectangle.h:315
Rectangle(ValueType width, ValueType height) noexcept
Definition juce_Rectangle.h:87
void setRight(ValueType newRight) noexcept
Definition juce_Rectangle.h:285
JUCE_NODISCARD Rectangle withX(ValueType newX) const noexcept
Definition juce_Rectangle.h:220
void setBottom(ValueType newBottom) noexcept
Definition juce_Rectangle.h:297
ValueType getCentreY() const noexcept
Definition juce_Rectangle.h:148
Rectangle constrainedWithin(Rectangle areaToFitWithin) const noexcept
Definition juce_Rectangle.h:798
JUCE_NODISCARD Rectangle withCentre(Point< ValueType > newCentre) const noexcept
Definition juce_Rectangle.h:241
void setPosition(ValueType newX, ValueType newY) noexcept
Definition juce_Rectangle.h:167
Rectangle reduced(ValueType delta) const noexcept
Definition juce_Rectangle.h:496
void setWidth(ValueType newWidth) noexcept
Definition juce_Rectangle.h:201
Rectangle< int > toNearestIntEdges() const noexcept
Definition juce_Rectangle.h:864
JUCE_NODISCARD Rectangle withHeight(ValueType newHeight) const noexcept
Definition juce_Rectangle.h:248
ValueType getBottom() const noexcept
Definition juce_Rectangle.h:142
void setTop(ValueType newTop) noexcept
Definition juce_Rectangle.h:273
Rectangle translated(ValueType deltaX, ValueType deltaY) const noexcept
Definition juce_Rectangle.h:327
Rectangle transformed(const AffineTransform &t) const noexcept
Definition juce_Rectangle.h:989
JUCE_NODISCARD Rectangle withBottom(ValueType newBottom) const noexcept
Definition juce_Rectangle.h:303
JUCE_NODISCARD Rectangle withWidth(ValueType newWidth) const noexcept
Definition juce_Rectangle.h:245
Rectangle(const Rectangle &)=default
JUCE_NODISCARD Rectangle withTrimmedLeft(ValueType amountToRemove) const noexcept
Definition juce_Rectangle.h:306
ValueType getX() const noexcept
Definition juce_Rectangle.h:127
ValueType getAspectRatio(bool widthOverHeight=true) const noexcept
Definition juce_Rectangle.h:157
Point< ValueType > getTopRight() const noexcept
Definition juce_Rectangle.h:173
JUCE_NODISCARD Rectangle withLeft(ValueType newLeft) const noexcept
Definition juce_Rectangle.h:267
Rectangle removeFromTop(ValueType amountToRemove) noexcept
Definition juce_Rectangle.h:510
JUCE_NODISCARD Rectangle withRight(ValueType newRight) const noexcept
Definition juce_Rectangle.h:291
void expand(ValueType deltaX, ValueType deltaY) noexcept
Definition juce_Rectangle.h:438
static Rectangle fromString(StringRef stringVersion)
Definition juce_Rectangle.h:976
static Rectangle findAreaContainingPoints(const Point< ValueType > *points, int numPoints) noexcept
Definition juce_Rectangle.h:901
Rectangle getUnion(Rectangle other) const noexcept
Definition juce_Rectangle.h:719
ValueType getWidth() const noexcept
Definition juce_Rectangle.h:133
void setX(ValueType newX) noexcept
Definition juce_Rectangle.h:195
void setCentre(Point< ValueType > newCentre) noexcept
Definition juce_Rectangle.h:211
bool enlargeIfAdjacent(Rectangle other) noexcept
Definition juce_Rectangle.h:738
bool intersects(const Line< ValueType > &line) const noexcept
Definition juce_Rectangle.h:652
friend class Rectangle
Definition juce_Rectangle.h:993
bool isFinite() const noexcept
Definition juce_Rectangle.h:124
Rectangle reduced(ValueType deltaX, ValueType deltaY) const noexcept
Definition juce_Rectangle.h:485
Rectangle removeFromLeft(ValueType amountToRemove) noexcept
Definition juce_Rectangle.h:526
Point< ValueType > getConstrainedPoint(Point< ValueType > point) const noexcept
Definition juce_Rectangle.h:569
bool contains(Point< ValueType > point) const noexcept
Definition juce_Rectangle.h:628
JUCE_NODISCARD Rectangle withRightX(ValueType newRightX) const noexcept
Definition juce_Rectangle.h:226
void copyWithRounding(Rectangle< int > &result) const noexcept
Definition juce_Rectangle.h:998
ValueType h
Definition juce_Rectangle.h:996
JUCE_NODISCARD Rectangle withY(ValueType newY) const noexcept
Definition juce_Rectangle.h:223
void translate(ValueType deltaX, ValueType deltaY) noexcept
Definition juce_Rectangle.h:319
void setPosition(Point< ValueType > newPos) noexcept
Definition juce_Rectangle.h:164
Rectangle expanded(ValueType delta) const noexcept
Definition juce_Rectangle.h:464
void setY(ValueType newY) noexcept
Definition juce_Rectangle.h:198
Point< ValueType > getRelativePoint(FloatType relativeX, FloatType relativeY) const noexcept
Definition juce_Rectangle.h:581
void setSize(ValueType newWidth, ValueType newHeight) noexcept
Definition juce_Rectangle.h:188
Rectangle getProportion(Rectangle< FloatType > proportionalRect) const noexcept
Definition juce_Rectangle.h:606
~Rectangle()=default
bool reduceIfPartlyContainedIn(Rectangle other) noexcept
Definition juce_Rectangle.h:767
bool intersectRectangle(Rectangle< ValueType > &rectangleToClip) const noexcept
Definition juce_Rectangle.h:708
ValueType proportionOfWidth(FloatType proportion) const noexcept
Definition juce_Rectangle.h:589
bool isEmpty() const noexcept
Definition juce_Rectangle.h:121
ValueType w
Definition juce_Rectangle.h:996
JUCE_NODISCARD Rectangle withPosition(ValueType newX, ValueType newY) const noexcept
Definition juce_Rectangle.h:232
void copyWithRounding(Rectangle< float > &result) const noexcept
Definition juce_Rectangle.h:999
ValueType getY() const noexcept
Definition juce_Rectangle.h:130
void reduce(ValueType deltaX, ValueType deltaY) noexcept
Definition juce_Rectangle.h:474
Rectangle transformedBy(const AffineTransform &transform) const noexcept
Definition juce_Rectangle.h:814
ValueType proportionOfHeight(FloatType proportion) const noexcept
Definition juce_Rectangle.h:596
void setCentre(ValueType newCentreX, ValueType newCentreY) noexcept
Definition juce_Rectangle.h:207
JUCE_NODISCARD Rectangle withSize(ValueType newWidth, ValueType newHeight) const noexcept
Definition juce_Rectangle.h:251
bool intersectRectangle(ValueType &otherX, ValueType &otherY, ValueType &otherW, ValueType &otherH) const noexcept
Definition juce_Rectangle.h:685
JUCE_NODISCARD Rectangle withBottomY(ValueType newBottomY) const noexcept
Definition juce_Rectangle.h:229
JUCE_NODISCARD Rectangle withSizeKeepingCentre(ValueType newWidth, ValueType newHeight) const noexcept
Definition juce_Rectangle.h:254
Rectangle expanded(ValueType deltaX, ValueType deltaY) const noexcept
Definition juce_Rectangle.h:451
void setBounds(ValueType newX, ValueType newY, ValueType newWidth, ValueType newHeight) noexcept
Definition juce_Rectangle.h:191
JUCE_NODISCARD Rectangle withTrimmedRight(ValueType amountToRemove) const noexcept
Definition juce_Rectangle.h:309
JUCE_NODISCARD Rectangle withTrimmedTop(ValueType amountToRemove) const noexcept
Definition juce_Rectangle.h:312
Definition juce_StringArray.h:35
int addTokens(StringRef stringToTokenise, bool preserveQuotedStrings)
Definition juce_StringArray.cpp:329
Definition juce_String.h:53
Definition juce_StringRef.h:62
String::CharPointerType text
Definition juce_StringRef.h:130
UINT_D64 w
Definition inflate.c:942
struct huft * t
Definition inflate.c:943
int y
Definition inflate.c:1588
register unsigned i
Definition inflate.c:1575
unsigned s
Definition inflate.c:1555
unsigned x[BMAX+1]
Definition inflate.c:1586
struct @113205115357366127300225113341150224053346037032::@137033172036070230260373056156374243321245367362 left
struct @113205115357366127300225113341150224053346037032::@137033172036070230260373056156374243321245367362 right
static int int minY
Definition pugl.h:1627
static int minX
Definition pugl.h:1626
static int int height
Definition pugl.h:1594
static int int int int maxY
Definition pugl.h:1630
static int width
Definition pugl.h:1593
static int int int maxX
Definition pugl.h:1628
#define JUCE_NODISCARD
Definition juce_CompilerSupport.h:108
Definition juce_FloatVectorOperations.h:143
ValueType parseAfterSpace(StringRef s) noexcept
Definition juce_Rectangle.h:40
int floorAsInt(int n) noexcept
Definition juce_Rectangle.h:46
int ceilAsInt(int n) noexcept
Definition juce_Rectangle.h:50
auto getNumericValue(StringRef s, Tag< int >)
Definition juce_Rectangle.h:35
Definition carla_juce.cpp:31
constexpr Type jmin(Type a, Type b)
Definition juce_MathsFunctions.h:106
bool juce_isfinite(NumericType) noexcept
Definition juce_MathsFunctions.h:421
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Definition juce_MathsFunctions.h:262
int roundToInt(const FloatType value) noexcept
Definition juce_MathsFunctions.h:465
float type
Definition juce_MathsFunctions.h:657
Definition juce_Rectangle.h:33
void Rectangle(HDC ctx, int l, int t, int r, int b)
Definition swell-gdi-generic.cpp:279
int n
Definition crypt.c:458
int r
Definition crypt.c:458
uch h[RAND_HEAD_LEN]
Definition crypt.c:459
b
Definition crypt.c:628
int result
Definition process.c:1455
typedef int(UZ_EXP MsgFn)()
#define const
Definition zconf.h:137