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.

juce_Rectangle.h 42KB

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