Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

882 lines
38KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_RECTANGLE_H_INCLUDED
  18. #define JUCE_RECTANGLE_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. Manages a rectangle and allows geometric operations to be performed on it.
  22. @see RectangleList, Path, Line, Point
  23. */
  24. template <typename ValueType>
  25. class Rectangle
  26. {
  27. public:
  28. //==============================================================================
  29. /** Creates a rectangle of zero size.
  30. The default coordinates will be (0, 0, 0, 0).
  31. */
  32. Rectangle() noexcept
  33. : w(), h()
  34. {
  35. }
  36. /** Creates a copy of another rectangle. */
  37. Rectangle (const Rectangle& other) noexcept
  38. : pos (other.pos), w (other.w), h (other.h)
  39. {
  40. }
  41. /** Creates a rectangle with a given position and size. */
  42. Rectangle (const ValueType initialX, const ValueType initialY,
  43. const ValueType width, const ValueType height) noexcept
  44. : pos (initialX, initialY),
  45. w (width), h (height)
  46. {
  47. }
  48. /** Creates a rectangle with a given size, and a position of (0, 0). */
  49. Rectangle (const ValueType width, const ValueType height) noexcept
  50. : w (width), h (height)
  51. {
  52. }
  53. /** Creates a Rectangle from the positions of two opposite corners. */
  54. Rectangle (const Point<ValueType> corner1, const Point<ValueType> corner2) noexcept
  55. : pos (jmin (corner1.x, corner2.x),
  56. jmin (corner1.y, corner2.y)),
  57. w (corner1.x - corner2.x),
  58. h (corner1.y - corner2.y)
  59. {
  60. if (w < ValueType()) w = -w;
  61. if (h < ValueType()) h = -h;
  62. }
  63. /** Creates a Rectangle from a set of left, right, top, bottom coordinates.
  64. The right and bottom values must be larger than the left and top ones, or the resulting
  65. rectangle will have a negative size.
  66. */
  67. static Rectangle leftTopRightBottom (const ValueType left, const ValueType top,
  68. const ValueType right, const ValueType bottom) noexcept
  69. {
  70. return Rectangle (left, top, right - left, bottom - top);
  71. }
  72. Rectangle& operator= (const Rectangle& other) noexcept
  73. {
  74. pos = other.pos;
  75. w = other.w; h = other.h;
  76. return *this;
  77. }
  78. /** Destructor. */
  79. ~Rectangle() noexcept {}
  80. //==============================================================================
  81. /** Returns true if the rectangle's width or height are zero or less */
  82. bool isEmpty() const noexcept { return w <= ValueType() || h <= ValueType(); }
  83. /** Returns the x coordinate of the rectangle's left-hand-side. */
  84. inline ValueType getX() const noexcept { return pos.x; }
  85. /** Returns the y coordinate of the rectangle's top edge. */
  86. inline ValueType getY() const noexcept { return pos.y; }
  87. /** Returns the width of the rectangle. */
  88. inline ValueType getWidth() const noexcept { return w; }
  89. /** Returns the height of the rectangle. */
  90. inline ValueType getHeight() const noexcept { return h; }
  91. /** Returns the x coordinate of the rectangle's right-hand-side. */
  92. inline ValueType getRight() const noexcept { return pos.x + w; }
  93. /** Returns the y coordinate of the rectangle's bottom edge. */
  94. inline ValueType getBottom() const noexcept { return pos.y + h; }
  95. /** Returns the x coordinate of the rectangle's centre. */
  96. ValueType getCentreX() const noexcept { return pos.x + w / (ValueType) 2; }
  97. /** Returns the y coordinate of the rectangle's centre. */
  98. ValueType getCentreY() const noexcept { return pos.y + h / (ValueType) 2; }
  99. /** Returns the centre point of the rectangle. */
  100. Point<ValueType> getCentre() const noexcept { return Point<ValueType> (pos.x + w / (ValueType) 2,
  101. pos.y + h / (ValueType) 2); }
  102. /** Returns the aspect ratio of the rectangle's width / height.
  103. If widthOverHeight is true, it returns width / height; if widthOverHeight is false,
  104. it returns height / width. */
  105. ValueType getAspectRatio (const bool widthOverHeight = true) const noexcept { return widthOverHeight ? w / h : h / w; }
  106. //==============================================================================
  107. /** Returns the rectangle's top-left position as a Point. */
  108. inline Point<ValueType> getPosition() const noexcept { return pos; }
  109. /** Changes the position of the rectangle's top-left corner (leaving its size unchanged). */
  110. inline void setPosition (const Point<ValueType> newPos) noexcept { pos = newPos; }
  111. /** Changes the position of the rectangle's top-left corner (leaving its size unchanged). */
  112. inline void setPosition (const ValueType newX, const ValueType newY) noexcept { pos.setXY (newX, newY); }
  113. /** Returns the rectangle's top-left position as a Point. */
  114. Point<ValueType> getTopLeft() const noexcept { return pos; }
  115. /** Returns the rectangle's top-right position as a Point. */
  116. Point<ValueType> getTopRight() const noexcept { return Point<ValueType> (pos.x + w, pos.y); }
  117. /** Returns the rectangle's bottom-left position as a Point. */
  118. Point<ValueType> getBottomLeft() const noexcept { return Point<ValueType> (pos.x, pos.y + h); }
  119. /** Returns the rectangle's bottom-right position as a Point. */
  120. Point<ValueType> getBottomRight() const noexcept { return Point<ValueType> (pos.x + w, pos.y + h); }
  121. /** Changes the rectangle's size, leaving the position of its top-left corner unchanged. */
  122. void setSize (const ValueType newWidth, const ValueType newHeight) noexcept { w = newWidth; h = newHeight; }
  123. /** Changes all the rectangle's coordinates. */
  124. void setBounds (const ValueType newX, const ValueType newY,
  125. const ValueType newWidth, const ValueType newHeight) noexcept { pos.x = newX; pos.y = newY; w = newWidth; h = newHeight; }
  126. /** Changes the rectangle's X coordinate */
  127. inline void setX (const ValueType newX) noexcept { pos.x = newX; }
  128. /** Changes the rectangle's Y coordinate */
  129. inline void setY (const ValueType newY) noexcept { pos.y = newY; }
  130. /** Changes the rectangle's width */
  131. inline void setWidth (const ValueType newWidth) noexcept { w = newWidth; }
  132. /** Changes the rectangle's height */
  133. inline void setHeight (const ValueType newHeight) noexcept { h = newHeight; }
  134. /** Returns a rectangle which has the same size and y-position as this one, but with a different x-position. */
  135. Rectangle withX (const ValueType newX) const noexcept { return Rectangle (newX, pos.y, w, h); }
  136. /** Returns a rectangle which has the same size and x-position as this one, but with a different y-position. */
  137. Rectangle withY (const ValueType newY) const noexcept { return Rectangle (pos.x, newY, w, h); }
  138. /** Returns a rectangle with the same size as this one, but a new position. */
  139. Rectangle withPosition (const ValueType newX, const ValueType newY) const noexcept { return Rectangle (newX, newY, w, h); }
  140. /** Returns a rectangle with the same size as this one, but a new position. */
  141. Rectangle withPosition (const Point<ValueType> newPos) const noexcept { return Rectangle (newPos.x, newPos.y, w, h); }
  142. /** Returns a rectangle whose size is the same as this one, but whose top-left position is (0, 0). */
  143. Rectangle withZeroOrigin() const noexcept { return Rectangle (w, h); }
  144. /** Returns a rectangle which has the same position and height as this one, but with a different width. */
  145. Rectangle withWidth (const ValueType newWidth) const noexcept { return Rectangle (pos.x, pos.y, newWidth, h); }
  146. /** Returns a rectangle which has the same position and width as this one, but with a different height. */
  147. Rectangle withHeight (const ValueType newHeight) const noexcept { return Rectangle (pos.x, pos.y, w, newHeight); }
  148. /** Returns a rectangle with the same position as this one, but a new size. */
  149. Rectangle withSize (const ValueType newWidth, const ValueType newHeight) const noexcept { return Rectangle (pos.x, pos.y, newWidth, newHeight); }
  150. /** Moves the x position, adjusting the width so that the right-hand edge remains in the same place.
  151. If the x is moved to be on the right of the current right-hand edge, the width will be set to zero.
  152. @see withLeft
  153. */
  154. void setLeft (const ValueType newLeft) noexcept { w = jmax (ValueType(), pos.x + w - newLeft); pos.x = newLeft; }
  155. /** Returns a new rectangle with a different x position, but the same right-hand edge as this one.
  156. If the new x is beyond the right of the current right-hand edge, the width will be set to zero.
  157. @see setLeft
  158. */
  159. Rectangle withLeft (const ValueType newLeft) const noexcept { return Rectangle (newLeft, pos.y, jmax (ValueType(), pos.x + w - newLeft), h); }
  160. /** Moves the y position, adjusting the height so that the bottom edge remains in the same place.
  161. If the y is moved to be below the current bottom edge, the height will be set to zero.
  162. @see withTop
  163. */
  164. void setTop (const ValueType newTop) noexcept { h = jmax (ValueType(), pos.y + h - newTop); pos.y = newTop; }
  165. /** Returns a new rectangle with a different y position, but the same bottom edge as this one.
  166. If the new y is beyond the bottom of the current rectangle, the height will be set to zero.
  167. @see setTop
  168. */
  169. Rectangle withTop (const ValueType newTop) const noexcept { return Rectangle (pos.x, newTop, w, jmax (ValueType(), pos.y + h - newTop)); }
  170. /** Adjusts the width so that the right-hand edge of the rectangle has this new value.
  171. If the new right is below the current X value, the X will be pushed down to match it.
  172. @see getRight, withRight
  173. */
  174. void setRight (const ValueType newRight) noexcept { pos.x = jmin (pos.x, newRight); w = newRight - pos.x; }
  175. /** Returns a new rectangle with a different right-hand edge position, but the same left-hand edge as this one.
  176. If the new right edge is below the current left-hand edge, the width will be set to zero.
  177. @see setRight
  178. */
  179. Rectangle withRight (const ValueType newRight) const noexcept { return Rectangle (jmin (pos.x, newRight), pos.y, jmax (ValueType(), newRight - pos.x), h); }
  180. /** Adjusts the height so that the bottom edge of the rectangle has this new value.
  181. If the new bottom is lower than the current Y value, the Y will be pushed down to match it.
  182. @see getBottom, withBottom
  183. */
  184. void setBottom (const ValueType newBottom) noexcept { pos.y = jmin (pos.y, newBottom); h = newBottom - pos.y; }
  185. /** Returns a new rectangle with a different bottom edge position, but the same top edge as this one.
  186. If the new y is beyond the bottom of the current rectangle, the height will be set to zero.
  187. @see setBottom
  188. */
  189. Rectangle withBottom (const ValueType newBottom) const noexcept { return Rectangle (pos.x, jmin (pos.y, newBottom), w, jmax (ValueType(), newBottom - pos.y)); }
  190. //==============================================================================
  191. /** Moves the rectangle's position by adding amount to its x and y coordinates. */
  192. void translate (const ValueType deltaX,
  193. const ValueType deltaY) noexcept
  194. {
  195. pos.x += deltaX;
  196. pos.y += deltaY;
  197. }
  198. /** Returns a rectangle which is the same as this one moved by a given amount. */
  199. Rectangle translated (const ValueType deltaX,
  200. const ValueType deltaY) const noexcept
  201. {
  202. return Rectangle (pos.x + deltaX, pos.y + deltaY, w, h);
  203. }
  204. /** Returns a rectangle which is the same as this one moved by a given amount. */
  205. Rectangle operator+ (const Point<ValueType> deltaPosition) const noexcept
  206. {
  207. return Rectangle (pos.x + deltaPosition.x, pos.y + deltaPosition.y, w, h);
  208. }
  209. /** Moves this rectangle by a given amount. */
  210. Rectangle& operator+= (const Point<ValueType> deltaPosition) noexcept
  211. {
  212. pos += deltaPosition;
  213. return *this;
  214. }
  215. /** Returns a rectangle which is the same as this one moved by a given amount. */
  216. Rectangle operator- (const Point<ValueType> deltaPosition) const noexcept
  217. {
  218. return Rectangle (pos.x - deltaPosition.x, pos.y - deltaPosition.y, w, h);
  219. }
  220. /** Moves this rectangle by a given amount. */
  221. Rectangle& operator-= (const Point<ValueType> deltaPosition) noexcept
  222. {
  223. pos -= deltaPosition;
  224. return *this;
  225. }
  226. /** Returns a rectangle that has been scaled by the given amount, centred around the origin.
  227. Note that if the rectangle has int coordinates and it's scaled by a
  228. floating-point amount, then the result will be converted back to integer
  229. coordinates using getSmallestIntegerContainer().
  230. */
  231. template <typename FloatType>
  232. Rectangle operator* (FloatType scaleFactor) const noexcept
  233. {
  234. Rectangle r (*this);
  235. r *= scaleFactor;
  236. return r;
  237. }
  238. /** Scales this rectangle by the given amount, centred around the origin.
  239. Note that if the rectangle has int coordinates and it's scaled by a
  240. floating-point amount, then the result will be converted back to integer
  241. coordinates using getSmallestIntegerContainer().
  242. */
  243. template <typename FloatType>
  244. Rectangle operator*= (FloatType scaleFactor) noexcept
  245. {
  246. Rectangle<FloatType> (pos.x * scaleFactor,
  247. pos.y * scaleFactor,
  248. w * scaleFactor,
  249. h * scaleFactor).copyWithRounding (*this);
  250. return *this;
  251. }
  252. /** Scales this rectangle by the given X and Y factors, centred around the origin.
  253. Note that if the rectangle has int coordinates and it's scaled by a
  254. floating-point amount, then the result will be converted back to integer
  255. coordinates using getSmallestIntegerContainer().
  256. */
  257. template <typename FloatType>
  258. Rectangle operator*= (Point<FloatType> scaleFactor) noexcept
  259. {
  260. Rectangle<FloatType> (pos.x * scaleFactor.x,
  261. pos.y * scaleFactor.y,
  262. w * scaleFactor.x,
  263. h * scaleFactor.y).copyWithRounding (*this);
  264. return *this;
  265. }
  266. /** Scales this rectangle by the given amount, centred around the origin. */
  267. template <typename FloatType>
  268. Rectangle operator/ (FloatType scaleFactor) const noexcept
  269. {
  270. Rectangle r (*this);
  271. r /= scaleFactor;
  272. return r;
  273. }
  274. /** Scales this rectangle by the given amount, centred around the origin. */
  275. template <typename FloatType>
  276. Rectangle operator/= (FloatType scaleFactor) noexcept
  277. {
  278. Rectangle<FloatType> (pos.x / scaleFactor,
  279. pos.y / scaleFactor,
  280. w / scaleFactor,
  281. h / scaleFactor).copyWithRounding (*this);
  282. return *this;
  283. }
  284. /** Scales this rectangle by the given X and Y factors, centred around the origin. */
  285. template <typename FloatType>
  286. Rectangle operator/= (Point<FloatType> scaleFactor) noexcept
  287. {
  288. Rectangle<FloatType> (pos.x / scaleFactor.x,
  289. pos.y / scaleFactor.y,
  290. w / scaleFactor.x,
  291. h / scaleFactor.y).copyWithRounding (*this);
  292. return *this;
  293. }
  294. /** Expands the rectangle by a given amount.
  295. Effectively, its new size is (x - deltaX, y - deltaY, w + deltaX * 2, h + deltaY * 2).
  296. @see expanded, reduce, reduced
  297. */
  298. void expand (const ValueType deltaX,
  299. const ValueType deltaY) noexcept
  300. {
  301. const ValueType nw = jmax (ValueType(), w + deltaX * 2);
  302. const ValueType nh = jmax (ValueType(), h + deltaY * 2);
  303. setBounds (pos.x - deltaX, pos.y - deltaY, nw, nh);
  304. }
  305. /** Returns a rectangle that is larger than this one by a given amount.
  306. Effectively, the rectangle returned is (x - deltaX, y - deltaY, w + deltaX * 2, h + deltaY * 2).
  307. @see expand, reduce, reduced
  308. */
  309. Rectangle expanded (const ValueType deltaX,
  310. const ValueType deltaY) const noexcept
  311. {
  312. const ValueType nw = jmax (ValueType(), w + deltaX * 2);
  313. const ValueType nh = jmax (ValueType(), h + deltaY * 2);
  314. return Rectangle (pos.x - deltaX, pos.y - deltaY, nw, nh);
  315. }
  316. /** Returns a rectangle that is larger than this one by a given amount.
  317. Effectively, the rectangle returned is (x - delta, y - delta, w + delta * 2, h + delta * 2).
  318. @see expand, reduce, reduced
  319. */
  320. Rectangle expanded (const ValueType delta) const noexcept
  321. {
  322. return expanded (delta, delta);
  323. }
  324. /** Shrinks the rectangle by a given amount.
  325. Effectively, its new size is (x + deltaX, y + deltaY, w - deltaX * 2, h - deltaY * 2).
  326. @see reduced, expand, expanded
  327. */
  328. void reduce (const ValueType deltaX,
  329. const ValueType deltaY) noexcept
  330. {
  331. expand (-deltaX, -deltaY);
  332. }
  333. /** Returns a rectangle that is smaller than this one by a given amount.
  334. Effectively, the rectangle returned is (x + deltaX, y + deltaY, w - deltaX * 2, h - deltaY * 2).
  335. @see reduce, expand, expanded
  336. */
  337. Rectangle reduced (const ValueType deltaX,
  338. const ValueType deltaY) const noexcept
  339. {
  340. return expanded (-deltaX, -deltaY);
  341. }
  342. /** Returns a rectangle that is smaller than this one by a given amount.
  343. Effectively, the rectangle returned is (x + delta, y + delta, w - delta * 2, h - delta * 2).
  344. @see reduce, expand, expanded
  345. */
  346. Rectangle reduced (const ValueType delta) const noexcept
  347. {
  348. return reduced (delta, delta);
  349. }
  350. /** Removes a strip from the top of this rectangle, reducing this rectangle
  351. by the specified amount and returning the section that was removed.
  352. E.g. if this rectangle is (100, 100, 300, 300) and amountToRemove is 50, this will
  353. return (100, 100, 300, 50) and leave this rectangle as (100, 150, 300, 250).
  354. If amountToRemove is greater than the height of this rectangle, it'll be clipped to
  355. that value.
  356. */
  357. Rectangle removeFromTop (const ValueType amountToRemove) noexcept
  358. {
  359. const Rectangle r (pos.x, pos.y, w, jmin (amountToRemove, h));
  360. pos.y += r.h; h -= r.h;
  361. return r;
  362. }
  363. /** Removes a strip from the left-hand edge of this rectangle, reducing this rectangle
  364. by the specified amount and returning the section that was removed.
  365. E.g. if this rectangle is (100, 100, 300, 300) and amountToRemove is 50, this will
  366. return (100, 100, 50, 300) and leave this rectangle as (150, 100, 250, 300).
  367. If amountToRemove is greater than the width of this rectangle, it'll be clipped to
  368. that value.
  369. */
  370. Rectangle removeFromLeft (const ValueType amountToRemove) noexcept
  371. {
  372. const Rectangle r (pos.x, pos.y, jmin (amountToRemove, w), h);
  373. pos.x += r.w; w -= r.w;
  374. return r;
  375. }
  376. /** Removes a strip from the right-hand edge of this rectangle, reducing this rectangle
  377. by the specified amount and returning the section that was removed.
  378. E.g. if this rectangle is (100, 100, 300, 300) and amountToRemove is 50, this will
  379. return (250, 100, 50, 300) and leave this rectangle as (100, 100, 250, 300).
  380. If amountToRemove is greater than the width of this rectangle, it'll be clipped to
  381. that value.
  382. */
  383. Rectangle removeFromRight (ValueType amountToRemove) noexcept
  384. {
  385. amountToRemove = jmin (amountToRemove, w);
  386. const Rectangle r (pos.x + w - amountToRemove, pos.y, amountToRemove, h);
  387. w -= amountToRemove;
  388. return r;
  389. }
  390. /** Removes a strip from the bottom of this rectangle, reducing this rectangle
  391. by the specified amount and returning the section that was removed.
  392. E.g. if this rectangle is (100, 100, 300, 300) and amountToRemove is 50, this will
  393. return (100, 250, 300, 50) and leave this rectangle as (100, 100, 300, 250).
  394. If amountToRemove is greater than the height of this rectangle, it'll be clipped to
  395. that value.
  396. */
  397. Rectangle removeFromBottom (ValueType amountToRemove) noexcept
  398. {
  399. amountToRemove = jmin (amountToRemove, h);
  400. const Rectangle r (pos.x, pos.y + h - amountToRemove, w, amountToRemove);
  401. h -= amountToRemove;
  402. return r;
  403. }
  404. //==============================================================================
  405. /** Returns true if the two rectangles are identical. */
  406. bool operator== (const Rectangle& other) const noexcept { return pos == other.pos && w == other.w && h == other.h; }
  407. /** Returns true if the two rectangles are not identical. */
  408. bool operator!= (const Rectangle& other) const noexcept { return pos != other.pos || w != other.w || h != other.h; }
  409. /** Returns true if this coordinate is inside the rectangle. */
  410. bool contains (const ValueType xCoord, const ValueType yCoord) const noexcept
  411. {
  412. return xCoord >= pos.x && yCoord >= pos.y && xCoord < pos.x + w && yCoord < pos.y + h;
  413. }
  414. /** Returns true if this coordinate is inside the rectangle. */
  415. bool contains (const Point<ValueType> point) const noexcept
  416. {
  417. return point.x >= pos.x && point.y >= pos.y && point.x < pos.x + w && point.y < pos.y + h;
  418. }
  419. /** Returns true if this other rectangle is completely inside this one. */
  420. bool contains (const Rectangle& other) const noexcept
  421. {
  422. return pos.x <= other.pos.x && pos.y <= other.pos.y
  423. && pos.x + w >= other.pos.x + other.w && pos.y + h >= other.pos.y + other.h;
  424. }
  425. /** Returns the nearest point to the specified point that lies within this rectangle. */
  426. Point<ValueType> getConstrainedPoint (const Point<ValueType> point) const noexcept
  427. {
  428. return Point<ValueType> (jlimit (pos.x, pos.x + w, point.x),
  429. jlimit (pos.y, pos.y + h, point.y));
  430. }
  431. /** Returns a point within this rectangle, specified as proportional coordinates.
  432. The relative X and Y values should be between 0 and 1, where 0 is the left or
  433. top of this rectangle, and 1 is the right or bottom. (Out-of-bounds values
  434. will return a point outside the rectangle).
  435. */
  436. Point<ValueType> getRelativePoint (double relativeX, double relativeY) const noexcept
  437. {
  438. return Point<ValueType> (pos.x + static_cast <ValueType> (w * relativeX),
  439. pos.y + static_cast <ValueType> (h * relativeY));
  440. }
  441. /** Returns true if any part of another rectangle overlaps this one. */
  442. bool intersects (const Rectangle& other) const noexcept
  443. {
  444. return pos.x + w > other.pos.x
  445. && pos.y + h > other.pos.y
  446. && pos.x < other.pos.x + other.w
  447. && pos.y < other.pos.y + other.h
  448. && w > ValueType() && h > ValueType()
  449. && other.w > ValueType() && other.h > ValueType();
  450. }
  451. /** Returns the region that is the overlap between this and another rectangle.
  452. If the two rectangles don't overlap, the rectangle returned will be empty.
  453. */
  454. Rectangle getIntersection (const Rectangle& other) const noexcept
  455. {
  456. const ValueType nx = jmax (pos.x, other.pos.x);
  457. const ValueType ny = jmax (pos.y, other.pos.y);
  458. const ValueType nw = jmin (pos.x + w, other.pos.x + other.w) - nx;
  459. const ValueType nh = jmin (pos.y + h, other.pos.y + other.h) - ny;
  460. if (nw >= ValueType() && nh >= ValueType())
  461. return Rectangle (nx, ny, nw, nh);
  462. return Rectangle();
  463. }
  464. /** Clips a set of rectangle coordinates so that they lie only within this one.
  465. This is a non-static version of intersectRectangles().
  466. Returns false if the two rectangles didn't overlap.
  467. */
  468. bool intersectRectangle (ValueType& otherX, ValueType& otherY, ValueType& otherW, ValueType& otherH) const noexcept
  469. {
  470. const ValueType maxX (jmax (otherX, pos.x));
  471. otherW = jmin (otherX + otherW, pos.x + w) - maxX;
  472. if (otherW > ValueType())
  473. {
  474. const ValueType maxY (jmax (otherY, pos.y));
  475. otherH = jmin (otherY + otherH, pos.y + h) - maxY;
  476. if (otherH > ValueType())
  477. {
  478. otherX = maxX; otherY = maxY;
  479. return true;
  480. }
  481. }
  482. return false;
  483. }
  484. /** Clips a rectangle so that it lies only within this one.
  485. Returns false if the two rectangles didn't overlap.
  486. */
  487. bool intersectRectangle (Rectangle<ValueType>& rectangleToClip) const noexcept
  488. {
  489. return intersectRectangle (rectangleToClip.pos.x, rectangleToClip.pos.y,
  490. rectangleToClip.w, rectangleToClip.h);
  491. }
  492. /** Returns the smallest rectangle that contains both this one and the one passed-in.
  493. If either this or the other rectangle are empty, they will not be counted as
  494. part of the resulting region.
  495. */
  496. Rectangle getUnion (const Rectangle& other) const noexcept
  497. {
  498. if (other.isEmpty()) return *this;
  499. if (isEmpty()) return other;
  500. const ValueType newX = jmin (pos.x, other.pos.x);
  501. const ValueType newY = jmin (pos.y, other.pos.y);
  502. return Rectangle (newX, newY,
  503. jmax (pos.x + w, other.pos.x + other.w) - newX,
  504. jmax (pos.y + h, other.pos.y + other.h) - newY);
  505. }
  506. /** If this rectangle merged with another one results in a simple rectangle, this
  507. will set this rectangle to the result, and return true.
  508. Returns false and does nothing to this rectangle if the two rectangles don't overlap,
  509. or if they form a complex region.
  510. */
  511. bool enlargeIfAdjacent (const Rectangle& other) noexcept
  512. {
  513. if (pos.x == other.pos.x && getRight() == other.getRight()
  514. && (other.getBottom() >= pos.y && other.pos.y <= getBottom()))
  515. {
  516. const ValueType newY = jmin (pos.y, other.pos.y);
  517. h = jmax (getBottom(), other.getBottom()) - newY;
  518. pos.y = newY;
  519. return true;
  520. }
  521. if (pos.y == other.pos.y && getBottom() == other.getBottom()
  522. && (other.getRight() >= pos.x && other.pos.x <= getRight()))
  523. {
  524. const ValueType newX = jmin (pos.x, other.pos.x);
  525. w = jmax (getRight(), other.getRight()) - newX;
  526. pos.x = newX;
  527. return true;
  528. }
  529. return false;
  530. }
  531. /** If after removing another rectangle from this one the result is a simple rectangle,
  532. this will set this object's bounds to be the result, and return true.
  533. Returns false and does nothing to this rectangle if the two rectangles don't overlap,
  534. or if removing the other one would form a complex region.
  535. */
  536. bool reduceIfPartlyContainedIn (const Rectangle& other) noexcept
  537. {
  538. int inside = 0;
  539. const ValueType otherR (other.getRight());
  540. if (pos.x >= other.pos.x && pos.x < otherR) inside = 1;
  541. const ValueType otherB (other.getBottom());
  542. if (pos.y >= other.pos.y && pos.y < otherB) inside |= 2;
  543. const ValueType r (pos.x + w);
  544. if (r >= other.pos.x && r < otherR) inside |= 4;
  545. const ValueType b (pos.y + h);
  546. if (b >= other.pos.y && b < otherB) inside |= 8;
  547. switch (inside)
  548. {
  549. case 1 + 2 + 8: w = r - otherR; pos.x = otherR; return true;
  550. case 1 + 2 + 4: h = b - otherB; pos.y = otherB; return true;
  551. case 2 + 4 + 8: w = other.pos.x - pos.x; return true;
  552. case 1 + 4 + 8: h = other.pos.y - pos.y; return true;
  553. }
  554. return false;
  555. }
  556. /** Tries to fit this rectangle within a target area, returning the result.
  557. If this rectangle is not completely inside the target area, then it'll be
  558. shifted (without changing its size) so that it lies within the target. If it
  559. is larger than the target rectangle in either dimension, then that dimension
  560. will be reduced to fit within the target.
  561. */
  562. Rectangle constrainedWithin (const Rectangle& areaToFitWithin) const noexcept
  563. {
  564. const ValueType newW (jmin (w, areaToFitWithin.getWidth()));
  565. const ValueType newH (jmin (h, areaToFitWithin.getHeight()));
  566. return Rectangle (jlimit (areaToFitWithin.getX(), areaToFitWithin.getRight() - newW, pos.x),
  567. jlimit (areaToFitWithin.getY(), areaToFitWithin.getBottom() - newH, pos.y),
  568. newW, newH);
  569. }
  570. /** Returns the smallest rectangle that can contain the shape created by applying
  571. a transform to this rectangle.
  572. This should only be used on floating point rectangles.
  573. */
  574. Rectangle transformedBy (const AffineTransform& transform) const noexcept
  575. {
  576. typedef typename TypeHelpers::SmallestFloatType<ValueType>::type FloatType;
  577. FloatType x1 = static_cast<FloatType> (pos.x), y1 = static_cast<FloatType> (pos.y);
  578. FloatType x2 = static_cast<FloatType> (pos.x + w), y2 = static_cast<FloatType> (pos.y);
  579. FloatType x3 = static_cast<FloatType> (pos.x), y3 = static_cast<FloatType> (pos.y + h);
  580. FloatType x4 = static_cast<FloatType> (x2), y4 = static_cast<FloatType> (y3);
  581. transform.transformPoints (x1, y1, x2, y2);
  582. transform.transformPoints (x3, y3, x4, y4);
  583. const FloatType rx1 = jmin (x1, x2, x3, x4);
  584. const FloatType rx2 = jmax (x1, x2, x3, x4);
  585. const FloatType ry1 = jmin (y1, y2, y3, y4);
  586. const FloatType ry2 = jmax (y1, y2, y3, y4);
  587. Rectangle r;
  588. Rectangle<FloatType> (rx1, ry1, rx2 - rx1, ry2 - ry1).copyWithRounding (r);
  589. return r;
  590. }
  591. /** Returns the smallest integer-aligned rectangle that completely contains this one.
  592. This is only relevent for floating-point rectangles, of course.
  593. @see toFloat()
  594. */
  595. Rectangle<int> getSmallestIntegerContainer() const noexcept
  596. {
  597. const int x1 = floorAsInt (pos.x);
  598. const int y1 = floorAsInt (pos.y);
  599. const int x2 = ceilAsInt (pos.x + w);
  600. const int y2 = ceilAsInt (pos.y + h);
  601. return Rectangle<int> (x1, y1, x2 - x1, y2 - y1);
  602. }
  603. /** Casts this rectangle to a Rectangle<float>.
  604. @see getSmallestIntegerContainer
  605. */
  606. Rectangle<float> toFloat() const noexcept
  607. {
  608. return Rectangle<float> (static_cast<float> (pos.x), static_cast<float> (pos.y),
  609. static_cast<float> (w), static_cast<float> (h));
  610. }
  611. /** Casts this rectangle to a Rectangle<double>.
  612. @see getSmallestIntegerContainer
  613. */
  614. Rectangle<double> toDouble() const noexcept
  615. {
  616. return Rectangle<double> (static_cast<double> (pos.x), static_cast<double> (pos.y),
  617. static_cast<double> (w), static_cast<double> (h));
  618. }
  619. /** Casts this rectangle to a Rectangle with the given type.
  620. If the target type is a conversion from float to int, then the conversion
  621. will be done using getSmallestIntegerContainer().
  622. */
  623. template <typename TargetType>
  624. Rectangle<TargetType> toType() const noexcept
  625. {
  626. Rectangle<TargetType> r;
  627. copyWithRounding (r);
  628. return r;
  629. }
  630. /** Returns the smallest Rectangle that can contain a set of points. */
  631. static Rectangle findAreaContainingPoints (const Point<ValueType>* const points, const int numPoints) noexcept
  632. {
  633. if (numPoints == 0)
  634. return Rectangle();
  635. ValueType minX (points[0].x);
  636. ValueType maxX (minX);
  637. ValueType minY (points[0].y);
  638. ValueType maxY (minY);
  639. for (int i = 1; i < numPoints; ++i)
  640. {
  641. minX = jmin (minX, points[i].x);
  642. maxX = jmax (maxX, points[i].x);
  643. minY = jmin (minY, points[i].y);
  644. maxY = jmax (maxY, points[i].y);
  645. }
  646. return Rectangle (minX, minY, maxX - minX, maxY - minY);
  647. }
  648. //==============================================================================
  649. /** Static utility to intersect two sets of rectangular coordinates.
  650. Returns false if the two regions didn't overlap.
  651. @see intersectRectangle
  652. */
  653. static bool intersectRectangles (ValueType& x1, ValueType& y1, ValueType& w1, ValueType& h1,
  654. const ValueType x2, const ValueType y2, const ValueType w2, const ValueType h2) noexcept
  655. {
  656. const ValueType x (jmax (x1, x2));
  657. w1 = jmin (x1 + w1, x2 + w2) - x;
  658. if (w1 > ValueType())
  659. {
  660. const ValueType y (jmax (y1, y2));
  661. h1 = jmin (y1 + h1, y2 + h2) - y;
  662. if (h1 > ValueType())
  663. {
  664. x1 = x; y1 = y;
  665. return true;
  666. }
  667. }
  668. return false;
  669. }
  670. //==============================================================================
  671. /** Creates a string describing this rectangle.
  672. The string will be of the form "x y width height", e.g. "100 100 400 200".
  673. Coupled with the fromString() method, this is very handy for things like
  674. storing rectangles (particularly component positions) in XML attributes.
  675. @see fromString
  676. */
  677. String toString() const
  678. {
  679. String s;
  680. s.preallocateBytes (32);
  681. s << pos.x << ' ' << pos.y << ' ' << w << ' ' << h;
  682. return s;
  683. }
  684. /** Parses a string containing a rectangle's details.
  685. The string should contain 4 integer tokens, in the form "x y width height". They
  686. can be comma or whitespace separated.
  687. This method is intended to go with the toString() method, to form an easy way
  688. of saving/loading rectangles as strings.
  689. @see toString
  690. */
  691. static Rectangle fromString (StringRef stringVersion)
  692. {
  693. StringArray toks;
  694. toks.addTokens (stringVersion.text.findEndOfWhitespace(), ",; \t\r\n", String::empty);
  695. return Rectangle (parseIntAfterSpace (toks[0]),
  696. parseIntAfterSpace (toks[1]),
  697. parseIntAfterSpace (toks[2]),
  698. parseIntAfterSpace (toks[3]));
  699. }
  700. #ifndef DOXYGEN
  701. // This has been renamed by transformedBy, in order to match the method names used in the Point class.
  702. JUCE_DEPRECATED_WITH_BODY (Rectangle transformed (const AffineTransform& t) const noexcept, { return transformedBy (t); })
  703. #endif
  704. private:
  705. template <typename OtherType> friend class Rectangle;
  706. Point<ValueType> pos;
  707. ValueType w, h;
  708. static int parseIntAfterSpace (StringRef s) noexcept
  709. { return s.text.findEndOfWhitespace().getIntValue32(); }
  710. void copyWithRounding (Rectangle<int>& result) const noexcept { result = getSmallestIntegerContainer(); }
  711. void copyWithRounding (Rectangle<float>& result) const noexcept { result = toFloat(); }
  712. void copyWithRounding (Rectangle<double>& result) const noexcept { result = toDouble(); }
  713. static int floorAsInt (int n) noexcept { return n; }
  714. static int floorAsInt (float n) noexcept { return (int) std::floor (n); }
  715. static int floorAsInt (double n) noexcept { return (int) std::floor (n); }
  716. static int ceilAsInt (int n) noexcept { return n; }
  717. static int ceilAsInt (float n) noexcept { return (int) std::ceil (n); }
  718. static int ceilAsInt (double n) noexcept { return (int) std::ceil (n); }
  719. };
  720. #endif // JUCE_RECTANGLE_H_INCLUDED