The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

858 lines
37KB

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