Rect.h 34 KB

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