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.

827 lines
36KB

  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. /** Scales this rectangle by the given amount, centred around the origin. */
  228. template <typename FloatType>
  229. Rectangle operator* (FloatType scaleFactor) const noexcept
  230. {
  231. Rectangle r (*this);
  232. r *= scaleFactor;
  233. return r;
  234. }
  235. /** Scales this rectangle by the given amount, centred around the origin. */
  236. template <typename FloatType>
  237. Rectangle operator*= (FloatType scaleFactor) noexcept
  238. {
  239. Rectangle<FloatType> (pos.x * scaleFactor,
  240. pos.y * scaleFactor,
  241. w * scaleFactor,
  242. h * scaleFactor).copyWithRounding (*this);
  243. return *this;
  244. }
  245. /** Scales this rectangle by the given amount, centred around the origin. */
  246. template <typename FloatType>
  247. Rectangle operator/ (FloatType scaleFactor) const noexcept
  248. {
  249. return operator* (((FloatType) 1) / scaleFactor);
  250. }
  251. /** Scales this rectangle by the given amount, centred around the origin. */
  252. template <typename FloatType>
  253. Rectangle operator/= (FloatType scaleFactor) noexcept
  254. {
  255. return operator*= (((FloatType) 1) / scaleFactor);
  256. }
  257. /** Expands the rectangle by a given amount.
  258. Effectively, its new size is (x - deltaX, y - deltaY, w + deltaX * 2, h + deltaY * 2).
  259. @see expanded, reduce, reduced
  260. */
  261. void expand (const ValueType deltaX,
  262. const ValueType deltaY) noexcept
  263. {
  264. const ValueType nw = jmax (ValueType(), w + deltaX * 2);
  265. const ValueType nh = jmax (ValueType(), h + deltaY * 2);
  266. setBounds (pos.x - deltaX, pos.y - deltaY, nw, nh);
  267. }
  268. /** Returns a rectangle that is larger than this one by a given amount.
  269. Effectively, the rectangle returned is (x - deltaX, y - deltaY, w + deltaX * 2, h + deltaY * 2).
  270. @see expand, reduce, reduced
  271. */
  272. Rectangle expanded (const ValueType deltaX,
  273. const ValueType deltaY) const noexcept
  274. {
  275. const ValueType nw = jmax (ValueType(), w + deltaX * 2);
  276. const ValueType nh = jmax (ValueType(), h + deltaY * 2);
  277. return Rectangle (pos.x - deltaX, pos.y - deltaY, nw, nh);
  278. }
  279. /** Returns a rectangle that is larger than this one by a given amount.
  280. Effectively, the rectangle returned is (x - delta, y - delta, w + delta * 2, h + delta * 2).
  281. @see expand, reduce, reduced
  282. */
  283. Rectangle expanded (const ValueType delta) const noexcept
  284. {
  285. return expanded (delta, delta);
  286. }
  287. /** Shrinks the rectangle by a given amount.
  288. Effectively, its new size is (x + deltaX, y + deltaY, w - deltaX * 2, h - deltaY * 2).
  289. @see reduced, expand, expanded
  290. */
  291. void reduce (const ValueType deltaX,
  292. const ValueType deltaY) noexcept
  293. {
  294. expand (-deltaX, -deltaY);
  295. }
  296. /** Returns a rectangle that is smaller than this one by a given amount.
  297. Effectively, the rectangle returned is (x + deltaX, y + deltaY, w - deltaX * 2, h - deltaY * 2).
  298. @see reduce, expand, expanded
  299. */
  300. Rectangle reduced (const ValueType deltaX,
  301. const ValueType deltaY) const noexcept
  302. {
  303. return expanded (-deltaX, -deltaY);
  304. }
  305. /** Returns a rectangle that is smaller than this one by a given amount.
  306. Effectively, the rectangle returned is (x + delta, y + delta, w - delta * 2, h - delta * 2).
  307. @see reduce, expand, expanded
  308. */
  309. Rectangle reduced (const ValueType delta) const noexcept
  310. {
  311. return reduced (delta, delta);
  312. }
  313. /** Removes a strip from the top of this rectangle, reducing this rectangle
  314. by the specified amount and returning the section that was removed.
  315. E.g. if this rectangle is (100, 100, 300, 300) and amountToRemove is 50, this will
  316. return (100, 100, 300, 50) and leave this rectangle as (100, 150, 300, 250).
  317. If amountToRemove is greater than the height of this rectangle, it'll be clipped to
  318. that value.
  319. */
  320. Rectangle removeFromTop (const ValueType amountToRemove) noexcept
  321. {
  322. const Rectangle r (pos.x, pos.y, w, jmin (amountToRemove, h));
  323. pos.y += r.h; h -= r.h;
  324. return r;
  325. }
  326. /** Removes a strip from the left-hand edge of this rectangle, reducing this rectangle
  327. by the specified amount and returning the section that was removed.
  328. E.g. if this rectangle is (100, 100, 300, 300) and amountToRemove is 50, this will
  329. return (100, 100, 50, 300) and leave this rectangle as (150, 100, 250, 300).
  330. If amountToRemove is greater than the width of this rectangle, it'll be clipped to
  331. that value.
  332. */
  333. Rectangle removeFromLeft (const ValueType amountToRemove) noexcept
  334. {
  335. const Rectangle r (pos.x, pos.y, jmin (amountToRemove, w), h);
  336. pos.x += r.w; w -= r.w;
  337. return r;
  338. }
  339. /** Removes a strip from the right-hand edge of this rectangle, reducing this rectangle
  340. by the specified amount and returning the section that was removed.
  341. E.g. if this rectangle is (100, 100, 300, 300) and amountToRemove is 50, this will
  342. return (250, 100, 50, 300) and leave this rectangle as (100, 100, 250, 300).
  343. If amountToRemove is greater than the width of this rectangle, it'll be clipped to
  344. that value.
  345. */
  346. Rectangle removeFromRight (ValueType amountToRemove) noexcept
  347. {
  348. amountToRemove = jmin (amountToRemove, w);
  349. const Rectangle r (pos.x + w - amountToRemove, pos.y, amountToRemove, h);
  350. w -= amountToRemove;
  351. return r;
  352. }
  353. /** Removes a strip from the bottom 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 (100, 250, 300, 50) and leave this rectangle as (100, 100, 300, 250).
  357. If amountToRemove is greater than the height of this rectangle, it'll be clipped to
  358. that value.
  359. */
  360. Rectangle removeFromBottom (ValueType amountToRemove) noexcept
  361. {
  362. amountToRemove = jmin (amountToRemove, h);
  363. const Rectangle r (pos.x, pos.y + h - amountToRemove, w, amountToRemove);
  364. h -= amountToRemove;
  365. return r;
  366. }
  367. //==============================================================================
  368. /** Returns true if the two rectangles are identical. */
  369. bool operator== (const Rectangle& other) const noexcept { return pos == other.pos && w == other.w && h == other.h; }
  370. /** Returns true if the two rectangles are not identical. */
  371. bool operator!= (const Rectangle& other) const noexcept { return pos != other.pos || w != other.w || h != other.h; }
  372. /** Returns true if this coordinate is inside the rectangle. */
  373. bool contains (const ValueType xCoord, const ValueType yCoord) const noexcept
  374. {
  375. return xCoord >= pos.x && yCoord >= pos.y && xCoord < pos.x + w && yCoord < pos.y + h;
  376. }
  377. /** Returns true if this coordinate is inside the rectangle. */
  378. bool contains (const Point<ValueType> point) const noexcept
  379. {
  380. return point.x >= pos.x && point.y >= pos.y && point.x < pos.x + w && point.y < pos.y + h;
  381. }
  382. /** Returns true if this other rectangle is completely inside this one. */
  383. bool contains (const Rectangle& other) const noexcept
  384. {
  385. return pos.x <= other.pos.x && pos.y <= other.pos.y
  386. && pos.x + w >= other.pos.x + other.w && pos.y + h >= other.pos.y + other.h;
  387. }
  388. /** Returns the nearest point to the specified point that lies within this rectangle. */
  389. Point<ValueType> getConstrainedPoint (const Point<ValueType> point) const noexcept
  390. {
  391. return Point<ValueType> (jlimit (pos.x, pos.x + w, point.x),
  392. jlimit (pos.y, pos.y + h, point.y));
  393. }
  394. /** Returns a point within this rectangle, specified as proportional coordinates.
  395. The relative X and Y values should be between 0 and 1, where 0 is the left or
  396. top of this rectangle, and 1 is the right or bottom. (Out-of-bounds values
  397. will return a point outside the rectangle).
  398. */
  399. Point<ValueType> getRelativePoint (double relativeX, double relativeY) const noexcept
  400. {
  401. return Point<ValueType> (pos.x + static_cast <ValueType> (w * relativeX),
  402. pos.y + static_cast <ValueType> (h * relativeY));
  403. }
  404. /** Returns true if any part of another rectangle overlaps this one. */
  405. bool intersects (const Rectangle& other) const noexcept
  406. {
  407. return pos.x + w > other.pos.x
  408. && pos.y + h > other.pos.y
  409. && pos.x < other.pos.x + other.w
  410. && pos.y < other.pos.y + other.h
  411. && w > ValueType() && h > ValueType()
  412. && other.w > ValueType() && other.h > ValueType();
  413. }
  414. /** Returns the region that is the overlap between this and another rectangle.
  415. If the two rectangles don't overlap, the rectangle returned will be empty.
  416. */
  417. Rectangle getIntersection (const Rectangle& other) const noexcept
  418. {
  419. const ValueType nx = jmax (pos.x, other.pos.x);
  420. const ValueType ny = jmax (pos.y, other.pos.y);
  421. const ValueType nw = jmin (pos.x + w, other.pos.x + other.w) - nx;
  422. const ValueType nh = jmin (pos.y + h, other.pos.y + other.h) - ny;
  423. if (nw >= ValueType() && nh >= ValueType())
  424. return Rectangle (nx, ny, nw, nh);
  425. return Rectangle();
  426. }
  427. /** Clips a rectangle so that it lies only within this one.
  428. This is a non-static version of intersectRectangles().
  429. Returns false if the two regions didn't overlap.
  430. */
  431. bool intersectRectangle (ValueType& otherX, ValueType& otherY, ValueType& otherW, ValueType& otherH) const noexcept
  432. {
  433. const ValueType maxX (jmax (otherX, pos.x));
  434. otherW = jmin (otherX + otherW, pos.x + w) - maxX;
  435. if (otherW > ValueType())
  436. {
  437. const ValueType maxY (jmax (otherY, pos.y));
  438. otherH = jmin (otherY + otherH, pos.y + h) - maxY;
  439. if (otherH > ValueType())
  440. {
  441. otherX = maxX; otherY = maxY;
  442. return true;
  443. }
  444. }
  445. return false;
  446. }
  447. /** Returns the smallest rectangle that contains both this one and the one passed-in.
  448. If either this or the other rectangle are empty, they will not be counted as
  449. part of the resulting region.
  450. */
  451. Rectangle getUnion (const Rectangle& other) const noexcept
  452. {
  453. if (other.isEmpty()) return *this;
  454. if (isEmpty()) return other;
  455. const ValueType newX = jmin (pos.x, other.pos.x);
  456. const ValueType newY = jmin (pos.y, other.pos.y);
  457. return Rectangle (newX, newY,
  458. jmax (pos.x + w, other.pos.x + other.w) - newX,
  459. jmax (pos.y + h, other.pos.y + other.h) - newY);
  460. }
  461. /** If this rectangle merged with another one results in a simple rectangle, this
  462. will set this rectangle to the result, and return true.
  463. Returns false and does nothing to this rectangle if the two rectangles don't overlap,
  464. or if they form a complex region.
  465. */
  466. bool enlargeIfAdjacent (const Rectangle& other) noexcept
  467. {
  468. if (pos.x == other.pos.x && getRight() == other.getRight()
  469. && (other.getBottom() >= pos.y && other.pos.y <= getBottom()))
  470. {
  471. const ValueType newY = jmin (pos.y, other.pos.y);
  472. h = jmax (getBottom(), other.getBottom()) - newY;
  473. pos.y = newY;
  474. return true;
  475. }
  476. if (pos.y == other.pos.y && getBottom() == other.getBottom()
  477. && (other.getRight() >= pos.x && other.pos.x <= getRight()))
  478. {
  479. const ValueType newX = jmin (pos.x, other.pos.x);
  480. w = jmax (getRight(), other.getRight()) - newX;
  481. pos.x = newX;
  482. return true;
  483. }
  484. return false;
  485. }
  486. /** If after removing another rectangle from this one the result is a simple rectangle,
  487. this will set this object's bounds to be the result, and return true.
  488. Returns false and does nothing to this rectangle if the two rectangles don't overlap,
  489. or if removing the other one would form a complex region.
  490. */
  491. bool reduceIfPartlyContainedIn (const Rectangle& other) noexcept
  492. {
  493. int inside = 0;
  494. const ValueType otherR (other.getRight());
  495. if (pos.x >= other.pos.x && pos.x < otherR) inside = 1;
  496. const ValueType otherB (other.getBottom());
  497. if (pos.y >= other.pos.y && pos.y < otherB) inside |= 2;
  498. const ValueType r (pos.x + w);
  499. if (r >= other.pos.x && r < otherR) inside |= 4;
  500. const ValueType b (pos.y + h);
  501. if (b >= other.pos.y && b < otherB) inside |= 8;
  502. switch (inside)
  503. {
  504. case 1 + 2 + 8: w = r - otherR; pos.x = otherR; return true;
  505. case 1 + 2 + 4: h = b - otherB; pos.y = otherB; return true;
  506. case 2 + 4 + 8: w = other.pos.x - pos.x; return true;
  507. case 1 + 4 + 8: h = other.pos.y - pos.y; return true;
  508. }
  509. return false;
  510. }
  511. /** Tries to fit this rectangle within a target area, returning the result.
  512. If this rectangle is not completely inside the target area, then it'll be
  513. shifted (without changing its size) so that it lies within the target. If it
  514. is larger than the target rectangle in either dimension, then that dimension
  515. will be reduced to fit within the target.
  516. */
  517. Rectangle constrainedWithin (const Rectangle& areaToFitWithin) const noexcept
  518. {
  519. const ValueType newW (jmin (w, areaToFitWithin.getWidth()));
  520. const ValueType newH (jmin (h, areaToFitWithin.getHeight()));
  521. return Rectangle (jlimit (areaToFitWithin.getX(), areaToFitWithin.getRight() - newW, pos.x),
  522. jlimit (areaToFitWithin.getY(), areaToFitWithin.getBottom() - newH, pos.y),
  523. newW, newH);
  524. }
  525. /** Returns the smallest rectangle that can contain the shape created by applying
  526. a transform to this rectangle.
  527. This should only be used on floating point rectangles.
  528. */
  529. Rectangle transformedBy (const AffineTransform& transform) const noexcept
  530. {
  531. typedef typename TypeHelpers::SmallestFloatType<ValueType>::type FloatType;
  532. FloatType x1 = static_cast<FloatType> (pos.x), y1 = static_cast<FloatType> (pos.y);
  533. FloatType x2 = static_cast<FloatType> (pos.x + w), y2 = static_cast<FloatType> (pos.y);
  534. FloatType x3 = static_cast<FloatType> (pos.x), y3 = static_cast<FloatType> (pos.y + h);
  535. FloatType x4 = static_cast<FloatType> (x2), y4 = static_cast<FloatType> (y3);
  536. transform.transformPoints (x1, y1, x2, y2);
  537. transform.transformPoints (x3, y3, x4, y4);
  538. const FloatType rx1 = jmin (x1, x2, x3, x4);
  539. const FloatType rx2 = jmax (x1, x2, x3, x4);
  540. const FloatType ry1 = jmin (y1, y2, y3, y4);
  541. const FloatType ry2 = jmax (y1, y2, y3, y4);
  542. Rectangle r;
  543. Rectangle<FloatType> (rx1, ry1, rx2 - rx1, ry2 - ry1).copyWithRounding (r);
  544. return r;
  545. }
  546. /** Returns the smallest integer-aligned rectangle that completely contains this one.
  547. This is only relevent for floating-point rectangles, of course.
  548. @see toFloat()
  549. */
  550. Rectangle<int> getSmallestIntegerContainer() const noexcept
  551. {
  552. return getSmallestIntegerContainerWithType<int>();
  553. }
  554. /** Returns the smallest integer-aligned rectangle that completely contains this one.
  555. This is only relevent for floating-point rectangles, of course.
  556. @see toFloat()
  557. */
  558. template <typename IntType>
  559. Rectangle<IntType> getSmallestIntegerContainerWithType() const noexcept
  560. {
  561. const IntType x1 = static_cast <IntType> (std::floor (static_cast<float> (pos.x)));
  562. const IntType y1 = static_cast <IntType> (std::floor (static_cast<float> (pos.y)));
  563. const IntType x2 = static_cast <IntType> (std::ceil (static_cast<float> (pos.x + w)));
  564. const IntType y2 = static_cast <IntType> (std::ceil (static_cast<float> (pos.y + h)));
  565. return Rectangle<IntType> (x1, y1, x2 - x1, y2 - y1);
  566. }
  567. /** Casts this rectangle to a Rectangle<float>.
  568. Obviously this is mainly useful for rectangles that use integer types.
  569. @see getSmallestIntegerContainer
  570. */
  571. Rectangle<float> toFloat() const noexcept
  572. {
  573. return Rectangle<float> (static_cast<float> (pos.x), static_cast<float> (pos.y),
  574. static_cast<float> (w), static_cast<float> (h));
  575. }
  576. /** Casts this rectangle to a Rectangle<double>.
  577. Obviously this is mainly useful for rectangles that use integer types.
  578. @see getSmallestIntegerContainer
  579. */
  580. Rectangle<double> toDouble() const noexcept
  581. {
  582. return Rectangle<double> (static_cast<double> (pos.x), static_cast<double> (pos.y),
  583. static_cast<double> (w), static_cast<double> (h));
  584. }
  585. /** Returns the smallest Rectangle that can contain a set of points. */
  586. static Rectangle findAreaContainingPoints (const Point<ValueType>* const points, const int numPoints) noexcept
  587. {
  588. if (numPoints == 0)
  589. return Rectangle();
  590. ValueType minX (points[0].x);
  591. ValueType maxX (minX);
  592. ValueType minY (points[0].y);
  593. ValueType maxY (minY);
  594. for (int i = 1; i < numPoints; ++i)
  595. {
  596. minX = jmin (minX, points[i].x);
  597. maxX = jmax (maxX, points[i].x);
  598. minY = jmin (minY, points[i].y);
  599. maxY = jmax (maxY, points[i].y);
  600. }
  601. return Rectangle (minX, minY, maxX - minX, maxY - minY);
  602. }
  603. //==============================================================================
  604. /** Static utility to intersect two sets of rectangular coordinates.
  605. Returns false if the two regions didn't overlap.
  606. @see intersectRectangle
  607. */
  608. static bool intersectRectangles (ValueType& x1, ValueType& y1, ValueType& w1, ValueType& h1,
  609. const ValueType x2, const ValueType y2, const ValueType w2, const ValueType h2) noexcept
  610. {
  611. const ValueType x (jmax (x1, x2));
  612. w1 = jmin (x1 + w1, x2 + w2) - x;
  613. if (w1 > ValueType())
  614. {
  615. const ValueType y (jmax (y1, y2));
  616. h1 = jmin (y1 + h1, y2 + h2) - y;
  617. if (h1 > ValueType())
  618. {
  619. x1 = x; y1 = y;
  620. return true;
  621. }
  622. }
  623. return false;
  624. }
  625. //==============================================================================
  626. /** Creates a string describing this rectangle.
  627. The string will be of the form "x y width height", e.g. "100 100 400 200".
  628. Coupled with the fromString() method, this is very handy for things like
  629. storing rectangles (particularly component positions) in XML attributes.
  630. @see fromString
  631. */
  632. String toString() const
  633. {
  634. String s;
  635. s.preallocateBytes (32);
  636. s << pos.x << ' ' << pos.y << ' ' << w << ' ' << h;
  637. return s;
  638. }
  639. /** Parses a string containing a rectangle's details.
  640. The string should contain 4 integer tokens, in the form "x y width height". They
  641. can be comma or whitespace separated.
  642. This method is intended to go with the toString() method, to form an easy way
  643. of saving/loading rectangles as strings.
  644. @see toString
  645. */
  646. static Rectangle fromString (const String& stringVersion)
  647. {
  648. StringArray toks;
  649. toks.addTokens (stringVersion.trim(), ",; \t\r\n", String::empty);
  650. return Rectangle (parseIntAfterSpace (toks[0]),
  651. parseIntAfterSpace (toks[1]),
  652. parseIntAfterSpace (toks[2]),
  653. parseIntAfterSpace (toks[3]));
  654. }
  655. // This has been renamed by transformedBy, in order to match the method names used in the Point class.
  656. JUCE_DEPRECATED_WITH_BODY (Rectangle transformed (const AffineTransform& t) const noexcept, { return transformedBy (t); })
  657. private:
  658. template <typename OtherType> friend class Rectangle;
  659. template <typename OtherType> friend class RectangleList;
  660. Point<ValueType> pos;
  661. ValueType w, h;
  662. static int parseIntAfterSpace (const String& s) noexcept
  663. { return s.getCharPointer().findEndOfWhitespace().getIntValue32(); }
  664. void copyWithRounding (Rectangle<int>& result) const noexcept { result = getSmallestIntegerContainer(); }
  665. void copyWithRounding (Rectangle<float>& result) const noexcept { result = toFloat(); }
  666. void copyWithRounding (Rectangle<double>& result) const noexcept { result = toDouble(); }
  667. };
  668. #endif // JUCE_RECTANGLE_H_INCLUDED