Rect.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StdLibExtras.h>
  7. #include <AK/String.h>
  8. #include <AK/Vector.h>
  9. #include <LibGfx/Line.h>
  10. #include <LibGfx/Rect.h>
  11. #include <LibIPC/Decoder.h>
  12. #include <LibIPC/Encoder.h>
  13. namespace Gfx {
  14. template<typename T>
  15. Rect<T>::RelativeLocation::RelativeLocation(Rect<T> const& base_rect, Rect<T> const& other_rect)
  16. {
  17. if (base_rect.is_empty() || other_rect.is_empty())
  18. return;
  19. auto parts = base_rect.shatter(other_rect);
  20. for (auto& part : parts) {
  21. if (part.x() < other_rect.x()) {
  22. if (part.y() < other_rect.y())
  23. m_top_left = true;
  24. if ((part.y() >= other_rect.y() && part.y() < other_rect.bottom()) || (part.y() <= other_rect.bottom() && part.bottom() > other_rect.y()))
  25. m_left = true;
  26. if (part.y() >= other_rect.bottom() || part.bottom() > other_rect.y())
  27. m_bottom_left = true;
  28. }
  29. if (part.x() >= other_rect.x() || part.right() > other_rect.x()) {
  30. if (part.y() < other_rect.y())
  31. m_top = true;
  32. if (part.y() >= other_rect.bottom() || part.bottom() > other_rect.bottom())
  33. m_bottom = true;
  34. }
  35. if (part.x() >= other_rect.right() || part.right() > other_rect.right()) {
  36. if (part.y() < other_rect.y())
  37. m_top_right = true;
  38. if ((part.y() >= other_rect.y() && part.y() < other_rect.bottom()) || (part.y() <= other_rect.bottom() && part.bottom() > other_rect.y()))
  39. m_right = true;
  40. if (part.y() >= other_rect.bottom() || part.bottom() > other_rect.y())
  41. m_bottom_right = true;
  42. }
  43. }
  44. }
  45. template<typename T>
  46. Vector<Point<T>, 2> Rect<T>::intersected(Line<T> const& line) const
  47. {
  48. if (is_empty())
  49. return {};
  50. Vector<Point<T>, 2> points;
  51. if (auto point = line.intersected({ top_left(), top_right() }); point.has_value())
  52. points.append({ point.value().x(), y() });
  53. if (auto point = line.intersected({ bottom_left(), bottom_right() }); point.has_value()) {
  54. points.append({ point.value().x(), bottom() });
  55. if (points.size() == 2)
  56. return points;
  57. }
  58. if (height() > 2) {
  59. if (auto point = line.intersected({ { x(), y() + 1 }, { x(), bottom() - 1 } }); point.has_value()) {
  60. points.append({ x(), point.value().y() });
  61. if (points.size() == 2)
  62. return points;
  63. }
  64. if (auto point = line.intersected({ { right(), y() + 1 }, { right(), bottom() - 1 } }); point.has_value())
  65. points.append({ right(), point.value().y() });
  66. }
  67. return points;
  68. }
  69. template<typename T>
  70. float Rect<T>::center_point_distance_to(Rect<T> const& other) const
  71. {
  72. return Line { center(), other.center() }.length();
  73. }
  74. template<typename T>
  75. Vector<Point<T>, 2> Rect<T>::closest_outside_center_points(Rect<T> const& other) const
  76. {
  77. if (intersects(other))
  78. return {};
  79. Line centers_line { center(), other.center() };
  80. auto points_this = intersected(centers_line);
  81. VERIFY(points_this.size() == 1);
  82. auto points_other = other.intersected(centers_line);
  83. VERIFY(points_other.size() == 1);
  84. return { points_this[0], points_other[0] };
  85. }
  86. template<typename T>
  87. float Rect<T>::outside_center_point_distance_to(Rect<T> const& other) const
  88. {
  89. auto points = closest_outside_center_points(other);
  90. if (points.is_empty())
  91. return 0.0;
  92. return Line { points[0], points[0] }.length();
  93. }
  94. template<typename T>
  95. Rect<T> Rect<T>::constrained_to(Rect<T> const& constrain_rect) const
  96. {
  97. if (constrain_rect.contains(*this))
  98. return *this;
  99. T move_x = 0, move_y = 0;
  100. if (right() > constrain_rect.right())
  101. move_x = constrain_rect.right() - right();
  102. if (bottom() > constrain_rect.bottom())
  103. move_y = constrain_rect.bottom() - bottom();
  104. if (x() < constrain_rect.x())
  105. move_x = x() - constrain_rect.x();
  106. if (y() < constrain_rect.y())
  107. move_y = y() - constrain_rect.y();
  108. auto rect = *this;
  109. if (move_x != 0 || move_y != 0)
  110. rect.translate_by(move_x, move_y);
  111. return rect;
  112. }
  113. template<typename T>
  114. Rect<T> Rect<T>::aligned_within(Size<T> const& rect_size, Point<T> const& align_at, TextAlignment alignment) const
  115. {
  116. if (rect_size.is_empty())
  117. return {};
  118. if (!size().contains(rect_size))
  119. return {};
  120. if (!contains(align_at))
  121. return {};
  122. Rect<T> rect;
  123. switch (alignment) {
  124. case TextAlignment::TopCenter:
  125. rect = { { align_at.x() - rect_size.width() / 2, align_at.y() }, rect_size };
  126. break;
  127. case TextAlignment::TopLeft:
  128. rect = { align_at, rect_size };
  129. break;
  130. case TextAlignment::TopRight:
  131. rect = { { align_at.x() - rect_size.width(), align_at.y() }, rect_size };
  132. break;
  133. case TextAlignment::CenterLeft:
  134. rect = { { align_at.x(), align_at.y() - rect_size.height() / 2 }, rect_size };
  135. break;
  136. case TextAlignment::Center:
  137. rect = { { align_at.x() - rect_size.width() / 2, align_at.y() - rect_size.height() / 2 }, rect_size };
  138. break;
  139. case TextAlignment::CenterRight:
  140. rect = { { align_at.x() - rect_size.width() / 2, align_at.y() }, rect_size };
  141. break;
  142. case TextAlignment::BottomCenter:
  143. rect = { { align_at.x() - rect_size.width() / 2, align_at.y() - rect_size.width() }, rect_size };
  144. break;
  145. case TextAlignment::BottomLeft:
  146. rect = { { align_at.x(), align_at.y() - rect_size.width() }, rect_size };
  147. break;
  148. case TextAlignment::BottomRight:
  149. rect = { { align_at.x() - rect_size.width(), align_at.y() - rect_size.width() }, rect_size };
  150. break;
  151. }
  152. return rect.constrained_to(*this);
  153. }
  154. template<typename T>
  155. Point<T> Rect<T>::closest_to(Point<T> const& point) const
  156. {
  157. if (is_empty())
  158. return {};
  159. Optional<Point<T>> closest_point;
  160. float closest_distance = 0.0;
  161. auto check_distance = [&](Line<T> const& line) {
  162. auto point_on_line = line.closest_to(point);
  163. auto distance = Line { point_on_line, point }.length();
  164. if (!closest_point.has_value() || distance < closest_distance) {
  165. closest_point = point_on_line;
  166. closest_distance = distance;
  167. }
  168. };
  169. check_distance({ top_left(), top_right() });
  170. check_distance({ bottom_left(), bottom_right() });
  171. if (height() > 2) {
  172. check_distance({ { x(), y() + 1 }, { x(), bottom() - 1 } });
  173. check_distance({ { right(), y() + 1 }, { right(), bottom() - 1 } });
  174. }
  175. VERIFY(closest_point.has_value());
  176. VERIFY(side(closest_point.value()) != Side::None);
  177. return closest_point.value();
  178. }
  179. template<typename T>
  180. void Rect<T>::intersect(Rect<T> const& other)
  181. {
  182. T l = max(left(), other.left());
  183. T r = min(right(), other.right());
  184. T t = max(top(), other.top());
  185. T b = min(bottom(), other.bottom());
  186. if (l > r || t > b) {
  187. m_location = {};
  188. m_size = {};
  189. return;
  190. }
  191. m_location.set_x(l);
  192. m_location.set_y(t);
  193. m_size.set_width((r - l) + 1);
  194. m_size.set_height((b - t) + 1);
  195. }
  196. template<typename T>
  197. Rect<T> Rect<T>::united(Rect<T> const& other) const
  198. {
  199. if (is_null())
  200. return other;
  201. if (other.is_null())
  202. return *this;
  203. Rect<T> rect;
  204. rect.set_left(min(left(), other.left()));
  205. rect.set_top(min(top(), other.top()));
  206. rect.set_right(max(right(), other.right()));
  207. rect.set_bottom(max(bottom(), other.bottom()));
  208. return rect;
  209. }
  210. template<typename T>
  211. Vector<Rect<T>, 4> Rect<T>::shatter(Rect<T> const& hammer) const
  212. {
  213. Vector<Rect<T>, 4> pieces;
  214. if (!intersects(hammer)) {
  215. pieces.unchecked_append(*this);
  216. return pieces;
  217. }
  218. Rect<T> top_shard {
  219. x(),
  220. y(),
  221. width(),
  222. hammer.y() - y()
  223. };
  224. Rect<T> bottom_shard {
  225. x(),
  226. hammer.y() + hammer.height(),
  227. width(),
  228. (y() + height()) - (hammer.y() + hammer.height())
  229. };
  230. Rect<T> left_shard {
  231. x(),
  232. max(hammer.y(), y()),
  233. hammer.x() - x(),
  234. min((hammer.y() + hammer.height()), (y() + height())) - max(hammer.y(), y())
  235. };
  236. Rect<T> right_shard {
  237. hammer.x() + hammer.width(),
  238. max(hammer.y(), y()),
  239. right() - hammer.right(),
  240. min((hammer.y() + hammer.height()), (y() + height())) - max(hammer.y(), y())
  241. };
  242. if (!top_shard.is_empty())
  243. pieces.unchecked_append(top_shard);
  244. if (!bottom_shard.is_empty())
  245. pieces.unchecked_append(bottom_shard);
  246. if (!left_shard.is_empty())
  247. pieces.unchecked_append(left_shard);
  248. if (!right_shard.is_empty())
  249. pieces.unchecked_append(right_shard);
  250. return pieces;
  251. }
  252. template<typename T>
  253. void Rect<T>::align_within(Rect<T> const& other, TextAlignment alignment)
  254. {
  255. switch (alignment) {
  256. case TextAlignment::Center:
  257. center_within(other);
  258. return;
  259. case TextAlignment::TopCenter:
  260. set_x(other.x() + other.width() / 2);
  261. return;
  262. case TextAlignment::TopLeft:
  263. set_location(other.location());
  264. return;
  265. case TextAlignment::TopRight:
  266. set_x(other.x() + other.width() - width());
  267. set_y(other.y());
  268. return;
  269. case TextAlignment::CenterLeft:
  270. set_x(other.x());
  271. center_vertically_within(other);
  272. return;
  273. case TextAlignment::CenterRight:
  274. set_x(other.x() + other.width() - width());
  275. center_vertically_within(other);
  276. return;
  277. case TextAlignment::BottomCenter:
  278. set_x(other.x() + other.width() / 2);
  279. set_y(other.y() + other.height() - height());
  280. return;
  281. case TextAlignment::BottomLeft:
  282. set_x(other.x());
  283. set_y(other.y() + other.height() - height());
  284. return;
  285. case TextAlignment::BottomRight:
  286. set_x(other.x() + other.width() - width());
  287. set_y(other.y() + other.height() - height());
  288. return;
  289. }
  290. }
  291. template<typename T>
  292. void Rect<T>::set_size_around(Size<T> const& new_size, Point<T> const& fixed_point)
  293. {
  294. const T new_x = fixed_point.x() - (T)(new_size.width() * ((float)(fixed_point.x() - x()) / width()));
  295. const T new_y = fixed_point.y() - (T)(new_size.height() * ((float)(fixed_point.y() - y()) / height()));
  296. set_location({ new_x, new_y });
  297. set_size(new_size);
  298. }
  299. template<>
  300. String IntRect::to_string() const
  301. {
  302. return String::formatted("[{},{} {}x{}]", x(), y(), width(), height());
  303. }
  304. template<>
  305. String FloatRect::to_string() const
  306. {
  307. return String::formatted("[{},{} {}x{}]", x(), y(), width(), height());
  308. }
  309. }
  310. namespace IPC {
  311. bool encode(Encoder& encoder, Gfx::IntRect const& rect)
  312. {
  313. encoder << rect.location() << rect.size();
  314. return true;
  315. }
  316. ErrorOr<void> decode(Decoder& decoder, Gfx::IntRect& rect)
  317. {
  318. Gfx::IntPoint point;
  319. Gfx::IntSize size;
  320. TRY(decoder.decode(point));
  321. TRY(decoder.decode(size));
  322. rect = { point, size };
  323. return {};
  324. }
  325. }
  326. template class Gfx::Rect<int>;
  327. template class Gfx::Rect<float>;