Rect.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/Format.h>
  10. #include <LibGfx/AffineTransform.h>
  11. #include <LibGfx/Line.h>
  12. #include <LibGfx/Orientation.h>
  13. #include <LibGfx/Point.h>
  14. #include <LibGfx/Size.h>
  15. #include <LibGfx/TextAlignment.h>
  16. #include <math.h>
  17. namespace Gfx {
  18. template<typename T>
  19. T abst(T value)
  20. {
  21. return value < 0 ? -value : value;
  22. }
  23. template<typename T>
  24. class Rect {
  25. public:
  26. Rect() = default;
  27. Rect(T x, T y, T width, T height)
  28. : m_location(x, y)
  29. , m_size(width, height)
  30. {
  31. }
  32. template<typename U>
  33. Rect(U x, U y, U width, U height)
  34. : m_location(x, y)
  35. , m_size(width, height)
  36. {
  37. }
  38. Rect(Point<T> const& location, Size<T> const& size)
  39. : m_location(location)
  40. , m_size(size)
  41. {
  42. }
  43. template<typename U>
  44. Rect(Point<U> const& location, Size<U> const& size)
  45. : m_location(location)
  46. , m_size(size)
  47. {
  48. }
  49. template<typename U>
  50. explicit Rect(Rect<U> const& other)
  51. : m_location(other.location())
  52. , m_size(other.size())
  53. {
  54. }
  55. [[nodiscard]] ALWAYS_INLINE T x() const { return location().x(); }
  56. [[nodiscard]] ALWAYS_INLINE T y() const { return location().y(); }
  57. [[nodiscard]] ALWAYS_INLINE T width() const { return m_size.width(); }
  58. [[nodiscard]] ALWAYS_INLINE T height() const { return m_size.height(); }
  59. ALWAYS_INLINE void set_x(T x) { m_location.set_x(x); }
  60. ALWAYS_INLINE void set_y(T y) { m_location.set_y(y); }
  61. ALWAYS_INLINE void set_width(T width) { m_size.set_width(width); }
  62. ALWAYS_INLINE void set_height(T height) { m_size.set_height(height); }
  63. [[nodiscard]] ALWAYS_INLINE Point<T> const& location() const { return m_location; }
  64. [[nodiscard]] ALWAYS_INLINE Size<T> const& size() const { return m_size; }
  65. [[nodiscard]] ALWAYS_INLINE bool is_empty() const { return width() <= 0 || height() <= 0; }
  66. ALWAYS_INLINE void translate_by(T dx, T dy) { m_location.translate_by(dx, dy); }
  67. ALWAYS_INLINE void translate_by(T dboth) { m_location.translate_by(dboth); }
  68. ALWAYS_INLINE void translate_by(Point<T> const& delta) { m_location.translate_by(delta); }
  69. ALWAYS_INLINE void scale_by(T dx, T dy)
  70. {
  71. m_location.scale_by(dx, dy);
  72. m_size.scale_by(dx, dy);
  73. }
  74. ALWAYS_INLINE void scale_by(T dboth) { scale_by(dboth, dboth); }
  75. ALWAYS_INLINE void scale_by(Point<T> const& delta) { scale_by(delta.x(), delta.y()); }
  76. void transform_by(AffineTransform const& transform) { *this = transform.map(*this); }
  77. [[nodiscard]] Point<T> center() const
  78. {
  79. return { x() + width() / 2, y() + height() / 2 };
  80. }
  81. ALWAYS_INLINE void set_location(Point<T> const& location)
  82. {
  83. m_location = location;
  84. }
  85. ALWAYS_INLINE void set_size(Size<T> const& size)
  86. {
  87. m_size = size;
  88. }
  89. void set_size(T width, T height)
  90. {
  91. m_size.set_width(width);
  92. m_size.set_height(height);
  93. }
  94. void inflate(T w, T h)
  95. {
  96. set_x(x() - w / 2);
  97. set_width(width() + w);
  98. set_y(y() - h / 2);
  99. set_height(height() + h);
  100. }
  101. void inflate(T top, T right, T bottom, T left)
  102. {
  103. set_x(x() - left);
  104. set_width(width() + left + right);
  105. set_y(y() - top);
  106. set_height(height() + top + bottom);
  107. }
  108. void inflate(Size<T> const& size)
  109. {
  110. set_x(x() - size.width() / 2);
  111. set_width(width() + size.width());
  112. set_y(y() - size.height() / 2);
  113. set_height(height() + size.height());
  114. }
  115. void shrink(T w, T h)
  116. {
  117. set_x(x() + w / 2);
  118. set_width(width() - w);
  119. set_y(y() + h / 2);
  120. set_height(height() - h);
  121. }
  122. void shrink(T top, T right, T bottom, T left)
  123. {
  124. set_x(x() + left);
  125. set_width(width() - (left + right));
  126. set_y(y() + top);
  127. set_height(height() - (top + bottom));
  128. }
  129. void shrink(Size<T> const& size)
  130. {
  131. set_x(x() + size.width() / 2);
  132. set_width(width() - size.width());
  133. set_y(y() + size.height() / 2);
  134. set_height(height() - size.height());
  135. }
  136. [[nodiscard]] Rect<T> translated(T dx, T dy) const
  137. {
  138. Rect<T> rect = *this;
  139. rect.translate_by(dx, dy);
  140. return rect;
  141. }
  142. [[nodiscard]] Rect<T> translated(T dboth) const
  143. {
  144. Rect<T> rect = *this;
  145. rect.translate_by(dboth);
  146. return rect;
  147. }
  148. [[nodiscard]] Rect<T> translated(Point<T> const& delta) const
  149. {
  150. Rect<T> rect = *this;
  151. rect.translate_by(delta);
  152. return rect;
  153. }
  154. [[nodiscard]] Rect<T> scaled(T sx, T sy) const
  155. {
  156. Rect<T> rect = *this;
  157. rect.scale_by(sx, sy);
  158. return rect;
  159. }
  160. [[nodiscard]] Rect<T> scaled(Point<T> const& s) const
  161. {
  162. Rect<T> rect = *this;
  163. rect.scale_by(s);
  164. return rect;
  165. }
  166. [[nodiscard]] Rect<T> transformed(AffineTransform const& transform) const
  167. {
  168. Rect<T> rect = *this;
  169. rect.transform_by(transform);
  170. return rect;
  171. }
  172. [[nodiscard]] Rect<T> shrunken(T w, T h) const
  173. {
  174. Rect<T> rect = *this;
  175. rect.shrink(w, h);
  176. return rect;
  177. }
  178. [[nodiscard]] Rect<T> shrunken(T top, T right, T bottom, T left) const
  179. {
  180. Rect<T> rect = *this;
  181. rect.shrink(top, right, bottom, left);
  182. return rect;
  183. }
  184. [[nodiscard]] Rect<T> shrunken(Size<T> const& size) const
  185. {
  186. Rect<T> rect = *this;
  187. rect.shrink(size);
  188. return rect;
  189. }
  190. [[nodiscard]] Rect<T> inflated(T w, T h) const
  191. {
  192. Rect<T> rect = *this;
  193. rect.inflate(w, h);
  194. return rect;
  195. }
  196. [[nodiscard]] Rect<T> inflated(T top, T right, T bottom, T left) const
  197. {
  198. Rect<T> rect = *this;
  199. rect.inflate(top, right, bottom, left);
  200. return rect;
  201. }
  202. [[nodiscard]] Rect<T> inflated(Size<T> const& size) const
  203. {
  204. Rect<T> rect = *this;
  205. rect.inflate(size);
  206. return rect;
  207. }
  208. Rect<T> take_from_right(T w)
  209. {
  210. if (w > width())
  211. w = width();
  212. Rect<T> rect = *this;
  213. set_width(width() - w);
  214. rect.set_x(x() + width());
  215. rect.set_width(w);
  216. return rect;
  217. }
  218. Rect<T> take_from_left(T w)
  219. {
  220. if (w > width())
  221. w = width();
  222. Rect<T> rect = *this;
  223. set_x(x() + w);
  224. set_width(width() - w);
  225. rect.set_width(w);
  226. return rect;
  227. }
  228. Rect<T> take_from_top(T h)
  229. {
  230. if (h > height())
  231. h = height();
  232. Rect<T> rect = *this;
  233. set_y(y() + h);
  234. set_height(height() - h);
  235. rect.set_height(h);
  236. return rect;
  237. }
  238. Rect<T> take_from_bottom(T h)
  239. {
  240. if (h > height())
  241. h = height();
  242. Rect<T> rect = *this;
  243. set_height(height() - h);
  244. rect.set_y(y() + height());
  245. rect.set_height(h);
  246. return rect;
  247. }
  248. [[nodiscard]] bool contains_vertically(T y) const
  249. {
  250. return y >= top() && y <= bottom();
  251. }
  252. [[nodiscard]] bool contains_horizontally(T x) const
  253. {
  254. return x >= left() && x <= right();
  255. }
  256. [[nodiscard]] bool contains(T x, T y) const
  257. {
  258. return x >= m_location.x() && x <= right() && y >= m_location.y() && y <= bottom();
  259. }
  260. [[nodiscard]] ALWAYS_INLINE bool contains(Point<T> const& point) const
  261. {
  262. return contains(point.x(), point.y());
  263. }
  264. [[nodiscard]] bool contains(Rect<T> const& other) const
  265. {
  266. return left() <= other.left()
  267. && right() >= other.right()
  268. && top() <= other.top()
  269. && bottom() >= other.bottom();
  270. }
  271. template<typename Container>
  272. [[nodiscard]] bool contains(Container const& others) const
  273. {
  274. bool have_any = false;
  275. for (auto const& other : others) {
  276. if (!contains(other))
  277. return false;
  278. have_any = true;
  279. }
  280. return have_any;
  281. }
  282. [[nodiscard]] ALWAYS_INLINE T primary_offset_for_orientation(Orientation orientation) const { return m_location.primary_offset_for_orientation(orientation); }
  283. ALWAYS_INLINE void set_primary_offset_for_orientation(Orientation orientation, T value) { m_location.set_primary_offset_for_orientation(orientation, value); }
  284. [[nodiscard]] ALWAYS_INLINE T secondary_offset_for_orientation(Orientation orientation) const { return m_location.secondary_offset_for_orientation(orientation); }
  285. ALWAYS_INLINE void set_secondary_offset_for_orientation(Orientation orientation, T value) { m_location.set_secondary_offset_for_orientation(orientation, value); }
  286. [[nodiscard]] ALWAYS_INLINE T primary_size_for_orientation(Orientation orientation) const { return m_size.primary_size_for_orientation(orientation); }
  287. [[nodiscard]] ALWAYS_INLINE T secondary_size_for_orientation(Orientation orientation) const { return m_size.secondary_size_for_orientation(orientation); }
  288. ALWAYS_INLINE void set_primary_size_for_orientation(Orientation orientation, T value) { m_size.set_primary_size_for_orientation(orientation, value); }
  289. ALWAYS_INLINE void set_secondary_size_for_orientation(Orientation orientation, T value) { m_size.set_secondary_size_for_orientation(orientation, value); }
  290. [[nodiscard]] T first_edge_for_orientation(Orientation orientation) const
  291. {
  292. if (orientation == Orientation::Vertical)
  293. return top();
  294. return left();
  295. }
  296. [[nodiscard]] T last_edge_for_orientation(Orientation orientation) const
  297. {
  298. if (orientation == Orientation::Vertical)
  299. return bottom();
  300. return right();
  301. }
  302. [[nodiscard]] ALWAYS_INLINE T left() const { return x(); }
  303. [[nodiscard]] ALWAYS_INLINE T right() const { return x() + width() - 1; }
  304. [[nodiscard]] ALWAYS_INLINE T top() const { return y(); }
  305. [[nodiscard]] ALWAYS_INLINE T bottom() const { return y() + height() - 1; }
  306. ALWAYS_INLINE void set_left(T left)
  307. {
  308. set_x(left);
  309. }
  310. ALWAYS_INLINE void set_top(T top)
  311. {
  312. set_y(top);
  313. }
  314. ALWAYS_INLINE void set_right(T right)
  315. {
  316. set_width(right - x() + 1);
  317. }
  318. ALWAYS_INLINE void set_bottom(T bottom)
  319. {
  320. set_height(bottom - y() + 1);
  321. }
  322. void set_right_without_resize(T new_right)
  323. {
  324. auto delta = new_right - right();
  325. translate_by(delta, 0);
  326. }
  327. void set_bottom_without_resize(T new_bottom)
  328. {
  329. auto delta = new_bottom - bottom();
  330. translate_by(0, delta);
  331. }
  332. [[nodiscard]] bool intersects_vertically(Rect<T> const& other) const
  333. {
  334. return top() <= other.bottom() && other.top() <= bottom();
  335. }
  336. [[nodiscard]] bool intersects_horizontally(Rect<T> const& other) const
  337. {
  338. return left() <= other.right() && other.left() <= right();
  339. }
  340. [[nodiscard]] bool intersects(Rect<T> const& other) const
  341. {
  342. return left() <= other.right()
  343. && other.left() <= right()
  344. && top() <= other.bottom()
  345. && other.top() <= bottom();
  346. }
  347. template<typename Container>
  348. [[nodiscard]] bool intersects(Container const& others) const
  349. {
  350. for (auto const& other : others) {
  351. if (intersects(other))
  352. return true;
  353. }
  354. return false;
  355. }
  356. template<typename Container, typename Function>
  357. IterationDecision for_each_intersected(Container const& others, Function f) const
  358. {
  359. if (is_empty())
  360. return IterationDecision::Continue;
  361. for (auto const& other : others) {
  362. auto intersected_rect = intersected(other);
  363. if (!intersected_rect.is_empty()) {
  364. IterationDecision decision = f(intersected_rect);
  365. if (decision != IterationDecision::Continue)
  366. return decision;
  367. }
  368. }
  369. return IterationDecision::Continue;
  370. }
  371. [[nodiscard]] Vector<Rect<T>, 4> shatter(Rect<T> const& hammer) const
  372. {
  373. Vector<Rect<T>, 4> pieces;
  374. if (!intersects(hammer)) {
  375. pieces.unchecked_append(*this);
  376. return pieces;
  377. }
  378. Rect<T> top_shard {
  379. x(),
  380. y(),
  381. width(),
  382. hammer.y() - y()
  383. };
  384. Rect<T> bottom_shard {
  385. x(),
  386. hammer.y() + hammer.height(),
  387. width(),
  388. (y() + height()) - (hammer.y() + hammer.height())
  389. };
  390. Rect<T> left_shard {
  391. x(),
  392. max(hammer.y(), y()),
  393. hammer.x() - x(),
  394. min((hammer.y() + hammer.height()), (y() + height())) - max(hammer.y(), y())
  395. };
  396. Rect<T> right_shard {
  397. hammer.x() + hammer.width(),
  398. max(hammer.y(), y()),
  399. right() - hammer.right(),
  400. min((hammer.y() + hammer.height()), (y() + height())) - max(hammer.y(), y())
  401. };
  402. if (!top_shard.is_empty())
  403. pieces.unchecked_append(top_shard);
  404. if (!bottom_shard.is_empty())
  405. pieces.unchecked_append(bottom_shard);
  406. if (!left_shard.is_empty())
  407. pieces.unchecked_append(left_shard);
  408. if (!right_shard.is_empty())
  409. pieces.unchecked_append(right_shard);
  410. return pieces;
  411. }
  412. template<class U>
  413. [[nodiscard]] bool operator==(Rect<U> const& other) const
  414. {
  415. return location() == other.location() && size() == other.size();
  416. }
  417. [[nodiscard]] Rect<T> operator*(T factor) const { return { m_location * factor, m_size * factor }; }
  418. Rect<T>& operator*=(T factor)
  419. {
  420. m_location *= factor;
  421. m_size *= factor;
  422. return *this;
  423. }
  424. void intersect(Rect<T> const& other)
  425. {
  426. T l = max(left(), other.left());
  427. T r = min(right(), other.right());
  428. T t = max(top(), other.top());
  429. T b = min(bottom(), other.bottom());
  430. if (l > r || t > b) {
  431. m_location = {};
  432. m_size = {};
  433. return;
  434. }
  435. m_location.set_x(l);
  436. m_location.set_y(t);
  437. m_size.set_width((r - l) + 1);
  438. m_size.set_height((b - t) + 1);
  439. }
  440. [[nodiscard]] static Rect<T> centered_on(Point<T> const& center, Size<T> const& size)
  441. {
  442. return { { center.x() - size.width() / 2, center.y() - size.height() / 2 }, size };
  443. }
  444. [[nodiscard]] static Rect<T> from_two_points(Point<T> const& a, Point<T> const& b)
  445. {
  446. return { min(a.x(), b.x()), min(a.y(), b.y()), abst(a.x() - b.x()), abst(a.y() - b.y()) };
  447. }
  448. [[nodiscard]] static Rect<T> intersection(Rect<T> const& a, Rect<T> const& b)
  449. {
  450. Rect<T> r = a;
  451. r.intersect(b);
  452. return r;
  453. }
  454. [[nodiscard]] ALWAYS_INLINE Rect<T> intersected(Rect<T> const& other) const
  455. {
  456. return intersection(*this, other);
  457. }
  458. [[nodiscard]] Vector<Point<T>, 2> intersected(Line<T> const& line) const
  459. {
  460. if (is_empty())
  461. return {};
  462. Vector<Point<T>, 2> points;
  463. if (auto point = line.intersected({ top_left(), top_right() }); point.has_value())
  464. points.append({ point.value().x(), y() });
  465. if (auto point = line.intersected({ bottom_left(), bottom_right() }); point.has_value()) {
  466. points.append({ point.value().x(), bottom() });
  467. if (points.size() == 2)
  468. return points;
  469. }
  470. if (height() > 2) {
  471. if (auto point = line.intersected({ { x(), y() + 1 }, { x(), bottom() - 1 } }); point.has_value()) {
  472. points.append({ x(), point.value().y() });
  473. if (points.size() == 2)
  474. return points;
  475. }
  476. if (auto point = line.intersected({ { right(), y() + 1 }, { right(), bottom() - 1 } }); point.has_value())
  477. points.append({ right(), point.value().y() });
  478. }
  479. return points;
  480. }
  481. [[nodiscard]] float center_point_distance_to(Rect<T> const& other) const
  482. {
  483. return Line { center(), other.center() }.length();
  484. }
  485. [[nodiscard]] Vector<Point<T>, 2> closest_outside_center_points(Rect<T> const& other) const
  486. {
  487. if (intersects(other))
  488. return {};
  489. Line centers_line { center(), other.center() };
  490. auto points_this = intersected(centers_line);
  491. VERIFY(points_this.size() == 1);
  492. auto points_other = other.intersected(centers_line);
  493. VERIFY(points_other.size() == 1);
  494. return { points_this[0], points_other[0] };
  495. }
  496. [[nodiscard]] float outside_center_point_distance_to(Rect<T> const& other) const
  497. {
  498. auto points = closest_outside_center_points(other);
  499. if (points.is_empty())
  500. return 0.0;
  501. return Line { points[0], points[0] }.length();
  502. }
  503. [[nodiscard]] Rect<T> constrained_to(Rect<T> const& constrain_rect) const
  504. {
  505. if (constrain_rect.contains(*this))
  506. return *this;
  507. T move_x = 0, move_y = 0;
  508. if (right() > constrain_rect.right())
  509. move_x = constrain_rect.right() - right();
  510. if (bottom() > constrain_rect.bottom())
  511. move_y = constrain_rect.bottom() - bottom();
  512. if (x() < constrain_rect.x())
  513. move_x = x() - constrain_rect.x();
  514. if (y() < constrain_rect.y())
  515. move_y = y() - constrain_rect.y();
  516. auto rect = *this;
  517. if (move_x != 0 || move_y != 0)
  518. rect.translate_by(move_x, move_y);
  519. return rect;
  520. }
  521. [[nodiscard]] Rect<T> aligned_within(Size<T> const& rect_size, Point<T> const& align_at, TextAlignment alignment = TextAlignment::Center) const
  522. {
  523. if (rect_size.is_empty())
  524. return {};
  525. if (!size().contains(rect_size))
  526. return {};
  527. if (!contains(align_at))
  528. return {};
  529. Rect<T> rect;
  530. switch (alignment) {
  531. case TextAlignment::TopCenter:
  532. rect = { { align_at.x() - rect_size.width() / 2, align_at.y() }, rect_size };
  533. break;
  534. case TextAlignment::TopLeft:
  535. rect = { align_at, rect_size };
  536. break;
  537. case TextAlignment::TopRight:
  538. rect = { { align_at.x() - rect_size.width(), align_at.y() }, rect_size };
  539. break;
  540. case TextAlignment::CenterLeft:
  541. rect = { { align_at.x(), align_at.y() - rect_size.height() / 2 }, rect_size };
  542. break;
  543. case TextAlignment::Center:
  544. rect = { { align_at.x() - rect_size.width() / 2, align_at.y() - rect_size.height() / 2 }, rect_size };
  545. break;
  546. case TextAlignment::CenterRight:
  547. rect = { { align_at.x() - rect_size.width() / 2, align_at.y() }, rect_size };
  548. break;
  549. case TextAlignment::BottomCenter:
  550. rect = { { align_at.x() - rect_size.width() / 2, align_at.y() - rect_size.width() }, rect_size };
  551. break;
  552. case TextAlignment::BottomLeft:
  553. rect = { { align_at.x(), align_at.y() - rect_size.width() }, rect_size };
  554. break;
  555. case TextAlignment::BottomRight:
  556. rect = { { align_at.x() - rect_size.width(), align_at.y() - rect_size.width() }, rect_size };
  557. break;
  558. }
  559. return rect.constrained_to(*this);
  560. }
  561. [[nodiscard]] Point<T> closest_to(Point<T> const& point) const
  562. {
  563. if (is_empty())
  564. return {};
  565. Optional<Point<T>> closest_point;
  566. float closest_distance = 0.0;
  567. auto check_distance = [&](Line<T> const& line) {
  568. auto point_on_line = line.closest_to(point);
  569. auto distance = Line { point_on_line, point }.length();
  570. if (!closest_point.has_value() || distance < closest_distance) {
  571. closest_point = point_on_line;
  572. closest_distance = distance;
  573. }
  574. };
  575. check_distance({ top_left(), top_right() });
  576. check_distance({ bottom_left(), bottom_right() });
  577. if (height() > 2) {
  578. check_distance({ { x(), y() + 1 }, { x(), bottom() - 1 } });
  579. check_distance({ { right(), y() + 1 }, { right(), bottom() - 1 } });
  580. }
  581. VERIFY(closest_point.has_value());
  582. VERIFY(side(closest_point.value()) != Side::None);
  583. return closest_point.value();
  584. }
  585. class RelativeLocation {
  586. friend class Rect<T>;
  587. RelativeLocation(Rect<T> const& base_rect, Rect<T> const& other_rect)
  588. {
  589. if (base_rect.is_empty() || other_rect.is_empty())
  590. return;
  591. auto parts = base_rect.shatter(other_rect);
  592. for (auto& part : parts) {
  593. if (part.x() < other_rect.x()) {
  594. if (part.y() < other_rect.y())
  595. m_top_left = true;
  596. if ((part.y() >= other_rect.y() && part.y() < other_rect.bottom()) || (part.y() <= other_rect.bottom() && part.bottom() > other_rect.y()))
  597. m_left = true;
  598. if (part.y() >= other_rect.bottom() || part.bottom() > other_rect.y())
  599. m_bottom_left = true;
  600. }
  601. if (part.x() >= other_rect.x() || part.right() > other_rect.x()) {
  602. if (part.y() < other_rect.y())
  603. m_top = true;
  604. if (part.y() >= other_rect.bottom() || part.bottom() > other_rect.bottom())
  605. m_bottom = true;
  606. }
  607. if (part.x() >= other_rect.right() || part.right() > other_rect.right()) {
  608. if (part.y() < other_rect.y())
  609. m_top_right = true;
  610. if ((part.y() >= other_rect.y() && part.y() < other_rect.bottom()) || (part.y() <= other_rect.bottom() && part.bottom() > other_rect.y()))
  611. m_right = true;
  612. if (part.y() >= other_rect.bottom() || part.bottom() > other_rect.y())
  613. m_bottom_right = true;
  614. }
  615. }
  616. }
  617. public:
  618. RelativeLocation() = default;
  619. bool top_left() const { return m_top_left; }
  620. bool top() const { return m_top; }
  621. bool top_right() const { return m_top_right; }
  622. bool left() const { return m_left; }
  623. bool right() const { return m_right; }
  624. bool bottom_left() const { return m_bottom_left; }
  625. bool bottom() const { return m_bottom; }
  626. bool bottom_right() const { return m_bottom_right; }
  627. bool anywhere_above() const { return m_top_left || m_top || m_top_right; }
  628. bool anywhere_below() const { return m_bottom_left || m_bottom || m_bottom_right; }
  629. bool anywhere_left() const { return m_top_left || m_left || m_bottom_left; }
  630. bool anywhere_right() const { return m_top_right || m_right || m_bottom_right; }
  631. private:
  632. bool m_top_left : 1 { false };
  633. bool m_top : 1 { false };
  634. bool m_top_right : 1 { false };
  635. bool m_left : 1 { false };
  636. bool m_right : 1 { false };
  637. bool m_bottom_left : 1 { false };
  638. bool m_bottom : 1 { false };
  639. bool m_bottom_right : 1 { false };
  640. };
  641. [[nodiscard]] RelativeLocation relative_location_to(Rect<T> const& other) const
  642. {
  643. return RelativeLocation(*this, other);
  644. }
  645. enum class Side {
  646. None = 0,
  647. Left,
  648. Top,
  649. Right,
  650. Bottom
  651. };
  652. [[nodiscard]] Side side(Point<T> const& point) const
  653. {
  654. if (is_empty())
  655. return Side::None;
  656. if (point.y() == y() || point.y() == bottom())
  657. return (point.x() >= x() && point.x() <= right()) ? (point.y() == y() ? Side::Top : Side::Bottom) : Side::None;
  658. if (point.x() == x() || point.x() == right())
  659. return (point.y() > y() && point.y() < bottom()) ? (point.x() == x() ? Side::Left : Side::Right) : Side::None;
  660. return Side::None;
  661. }
  662. [[nodiscard]] Rect<T> rect_on_side(Side side, Rect<T> const& other) const
  663. {
  664. switch (side) {
  665. case Side::None:
  666. break;
  667. case Side::Left:
  668. // Return the area in other that is to the left of this rect
  669. if (other.x() < x()) {
  670. if (other.right() >= x())
  671. return { other.location(), { x() - other.x(), other.height() } };
  672. else
  673. return other;
  674. }
  675. break;
  676. case Side::Top:
  677. // Return the area in other that is above this rect
  678. if (other.y() < y()) {
  679. if (other.bottom() >= y())
  680. return { other.location(), { other.width(), y() - other.y() } };
  681. else
  682. return other;
  683. }
  684. break;
  685. case Side::Right:
  686. // Return the area in other that is to the right of this rect
  687. if (other.right() >= x()) {
  688. if (other.x() <= right())
  689. return { { right() + 1, other.y() }, { other.width() - (right() - other.x()), other.height() } };
  690. else
  691. return other;
  692. }
  693. break;
  694. case Side::Bottom:
  695. // Return the area in other that is below this rect
  696. if (other.bottom() >= y()) {
  697. if (other.y() <= bottom())
  698. return { { other.x(), bottom() + 1 }, { other.width(), other.height() - (bottom() - other.y()) } };
  699. else
  700. return other;
  701. }
  702. break;
  703. }
  704. return {};
  705. }
  706. template<typename Container>
  707. static bool disperse(Container& rects)
  708. {
  709. auto has_intersecting = [&]() {
  710. for (auto& rect : rects) {
  711. for (auto& other_rect : rects) {
  712. if (&rect == &other_rect)
  713. continue;
  714. if (rect.intersects(other_rect))
  715. return true;
  716. }
  717. }
  718. return false;
  719. };
  720. if (!has_intersecting())
  721. return false;
  722. auto calc_delta = [&](Rect<T> const& rect) -> Point<T> {
  723. auto rect_center = rect.center();
  724. Point<T> center_sum;
  725. for (auto& other_rect : rects) {
  726. if (&other_rect == &rect)
  727. continue;
  728. if (rect.intersects(other_rect))
  729. center_sum += rect_center - other_rect.center();
  730. }
  731. double m = sqrt((double)center_sum.x() * (double)center_sum.x() + (double)center_sum.y() * (double)center_sum.y());
  732. if (m != 0.0)
  733. return { (double)center_sum.x() / m + 0.5, (double)center_sum.y() / m + 0.5 };
  734. return {};
  735. };
  736. Vector<Point<T>, 8> deltas;
  737. do {
  738. bool changes = false;
  739. deltas.clear_with_capacity();
  740. for (auto& rect : rects) {
  741. auto delta = calc_delta(rect);
  742. if (!delta.is_zero())
  743. changes = true;
  744. deltas.append(delta);
  745. }
  746. // TODO: If we have no changes we would loop infinitely!
  747. // Figure out some way to resolve this. Maybe randomly moving an intersecting rect?
  748. VERIFY(changes);
  749. size_t i = 0;
  750. for (auto& rect : rects)
  751. rect.translate_by(deltas[i++]);
  752. } while (has_intersecting());
  753. return true;
  754. }
  755. [[nodiscard]] bool is_adjacent(Rect<T> const& other) const
  756. {
  757. if (is_empty() || other.is_empty())
  758. return false;
  759. if (intersects(other))
  760. return false;
  761. if (other.x() + other.width() == x() || other.x() == x() + width())
  762. return max(top(), other.top()) <= min(bottom(), other.bottom());
  763. if (other.y() + other.height() == y() || other.y() == y() + height())
  764. return max(left(), other.left()) <= min(right(), other.right());
  765. return false;
  766. }
  767. [[nodiscard]] static Rect<T> centered_at(Point<T> const& point, Size<T> const& size)
  768. {
  769. return { { point.x() - size.width() / 2, point.y() - size.height() / 2 }, size };
  770. }
  771. [[nodiscard]] Rect<T> united(Rect<T> const& other) const
  772. {
  773. if (is_empty())
  774. return other;
  775. if (other.is_empty())
  776. return *this;
  777. Rect<T> rect;
  778. rect.set_left(min(left(), other.left()));
  779. rect.set_top(min(top(), other.top()));
  780. rect.set_right(max(right(), other.right()));
  781. rect.set_bottom(max(bottom(), other.bottom()));
  782. return rect;
  783. }
  784. [[nodiscard]] Point<T> top_left() const { return { left(), top() }; }
  785. [[nodiscard]] Point<T> top_right() const { return { right(), top() }; }
  786. [[nodiscard]] Point<T> bottom_left() const { return { left(), bottom() }; }
  787. [[nodiscard]] Point<T> bottom_right() const { return { right(), bottom() }; }
  788. void align_within(Rect<T> const& other, TextAlignment alignment)
  789. {
  790. switch (alignment) {
  791. case TextAlignment::Center:
  792. center_within(other);
  793. return;
  794. case TextAlignment::TopCenter:
  795. set_x(other.x() + other.width() / 2);
  796. return;
  797. case TextAlignment::TopLeft:
  798. set_location(other.location());
  799. return;
  800. case TextAlignment::TopRight:
  801. set_x(other.x() + other.width() - width());
  802. set_y(other.y());
  803. return;
  804. case TextAlignment::CenterLeft:
  805. set_x(other.x());
  806. center_vertically_within(other);
  807. return;
  808. case TextAlignment::CenterRight:
  809. set_x(other.x() + other.width() - width());
  810. center_vertically_within(other);
  811. return;
  812. case TextAlignment::BottomCenter:
  813. set_x(other.x() + other.width() / 2);
  814. set_y(other.y() + other.height() - height());
  815. return;
  816. case TextAlignment::BottomLeft:
  817. set_x(other.x());
  818. set_y(other.y() + other.height() - height());
  819. return;
  820. case TextAlignment::BottomRight:
  821. set_x(other.x() + other.width() - width());
  822. set_y(other.y() + other.height() - height());
  823. return;
  824. }
  825. }
  826. void center_within(Rect<T> const& other)
  827. {
  828. center_horizontally_within(other);
  829. center_vertically_within(other);
  830. }
  831. [[nodiscard]] Rect centered_within(Rect const& other) const
  832. {
  833. Rect rect { *this };
  834. rect.center_horizontally_within(other);
  835. rect.center_vertically_within(other);
  836. return rect;
  837. }
  838. void center_horizontally_within(Rect<T> const& other)
  839. {
  840. set_x(other.center().x() - width() / 2);
  841. }
  842. void center_vertically_within(Rect<T> const& other)
  843. {
  844. set_y(other.center().y() - height() / 2);
  845. }
  846. template<typename U>
  847. requires(!IsSame<T, U>)
  848. [[nodiscard]] ALWAYS_INLINE Rect<U> to_type() const
  849. {
  850. return Rect<U>(*this);
  851. }
  852. template<FloatingPoint U>
  853. [[nodiscard]] ALWAYS_INLINE Rect<U> to_rounded() const
  854. {
  855. // FIXME: We may get away with `rint[lf]?()` here.
  856. // This would even give us some more control of these internals,
  857. // while the break-tie algorithm does not really matter
  858. if constexpr (IsSame<T, float>) {
  859. return {
  860. static_cast<U>(roundf(x())),
  861. static_cast<U>(roundf(y())),
  862. static_cast<U>(roundf(width())),
  863. static_cast<U>(roundf(height())),
  864. };
  865. }
  866. if constexpr (IsSame<T, double>) {
  867. return {
  868. static_cast<U>(round(x())),
  869. static_cast<U>(round(y())),
  870. static_cast<U>(round(width())),
  871. static_cast<U>(round(height())),
  872. };
  873. }
  874. return {
  875. static_cast<U>(roundl(x())),
  876. static_cast<U>(roundl(y())),
  877. static_cast<U>(roundl(width())),
  878. static_cast<U>(roundl(height())),
  879. };
  880. }
  881. template<Integral I>
  882. ALWAYS_INLINE Rect<I> to_rounded() const
  883. {
  884. return {
  885. round_to<I>(x()),
  886. round_to<I>(y()),
  887. round_to<I>(width()),
  888. round_to<I>(height()),
  889. };
  890. }
  891. [[nodiscard]] DeprecatedString to_deprecated_string() const;
  892. private:
  893. Point<T> m_location;
  894. Size<T> m_size;
  895. };
  896. using IntRect = Rect<int>;
  897. using FloatRect = Rect<float>;
  898. [[nodiscard]] ALWAYS_INLINE IntRect enclosing_int_rect(FloatRect const& float_rect)
  899. {
  900. int x1 = floorf(float_rect.x());
  901. int y1 = floorf(float_rect.y());
  902. int x2 = ceilf(float_rect.x() + float_rect.width());
  903. int y2 = ceilf(float_rect.y() + float_rect.height());
  904. return Gfx::IntRect::from_two_points({ x1, y1 }, { x2, y2 });
  905. }
  906. }
  907. namespace AK {
  908. template<typename T>
  909. struct Formatter<Gfx::Rect<T>> : Formatter<FormatString> {
  910. ErrorOr<void> format(FormatBuilder& builder, Gfx::Rect<T> const& value)
  911. {
  912. return Formatter<FormatString>::format(builder, "[{},{} {}x{}]"sv, value.x(), value.y(), value.width(), value.height());
  913. }
  914. };
  915. }
  916. namespace IPC {
  917. template<>
  918. ErrorOr<void> encode(Encoder&, Gfx::IntRect const&);
  919. template<>
  920. ErrorOr<Gfx::IntRect> decode(Decoder&);
  921. }