Rect.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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::TopLeft:
  125. rect = { align_at, rect_size };
  126. break;
  127. case TextAlignment::CenterLeft:
  128. rect = { { align_at.x(), align_at.y() - rect_size.height() / 2 }, rect_size };
  129. break;
  130. case TextAlignment::Center:
  131. rect = { { align_at.x() - rect_size.width() / 2, align_at.y() - rect_size.height() / 2 }, rect_size };
  132. break;
  133. case TextAlignment::CenterRight:
  134. rect = { { align_at.x() - rect_size.width() / 2, align_at.y() }, rect_size };
  135. break;
  136. case TextAlignment::TopRight:
  137. rect = { { align_at.x() - rect_size.width(), align_at.y() }, rect_size };
  138. break;
  139. case TextAlignment::BottomLeft:
  140. rect = { { align_at.x(), align_at.y() - rect_size.width() }, rect_size };
  141. break;
  142. case TextAlignment::BottomRight:
  143. rect = { { align_at.x() - rect_size.width(), align_at.y() - rect_size.width() }, rect_size };
  144. break;
  145. }
  146. return rect.constrained_to(*this);
  147. }
  148. template<typename T>
  149. Point<T> Rect<T>::closest_to(Point<T> const& point) const
  150. {
  151. if (is_empty())
  152. return {};
  153. Optional<Point<T>> closest_point;
  154. float closest_distance = 0.0;
  155. auto check_distance = [&](const Line<T>& line) {
  156. auto point_on_line = line.closest_to(point);
  157. auto distance = Line { point_on_line, point }.length();
  158. if (!closest_point.has_value() || distance < closest_distance) {
  159. closest_point = point_on_line;
  160. closest_distance = distance;
  161. }
  162. };
  163. check_distance({ top_left(), top_right() });
  164. check_distance({ bottom_left(), bottom_right() });
  165. if (height() > 2) {
  166. check_distance({ { x(), y() + 1 }, { x(), bottom() - 1 } });
  167. check_distance({ { right(), y() + 1 }, { right(), bottom() - 1 } });
  168. }
  169. VERIFY(closest_point.has_value());
  170. VERIFY(side(closest_point.value()) != Side::None);
  171. return closest_point.value();
  172. }
  173. template<typename T>
  174. void Rect<T>::intersect(Rect<T> const& other)
  175. {
  176. T l = max(left(), other.left());
  177. T r = min(right(), other.right());
  178. T t = max(top(), other.top());
  179. T b = min(bottom(), other.bottom());
  180. if (l > r || t > b) {
  181. m_location = {};
  182. m_size = {};
  183. return;
  184. }
  185. m_location.set_x(l);
  186. m_location.set_y(t);
  187. m_size.set_width((r - l) + 1);
  188. m_size.set_height((b - t) + 1);
  189. }
  190. template<typename T>
  191. Rect<T> Rect<T>::united(Rect<T> const& other) const
  192. {
  193. if (is_null())
  194. return other;
  195. if (other.is_null())
  196. return *this;
  197. Rect<T> rect;
  198. rect.set_left(min(left(), other.left()));
  199. rect.set_top(min(top(), other.top()));
  200. rect.set_right(max(right(), other.right()));
  201. rect.set_bottom(max(bottom(), other.bottom()));
  202. return rect;
  203. }
  204. template<typename T>
  205. Vector<Rect<T>, 4> Rect<T>::shatter(Rect<T> const& hammer) const
  206. {
  207. Vector<Rect<T>, 4> pieces;
  208. if (!intersects(hammer)) {
  209. pieces.unchecked_append(*this);
  210. return pieces;
  211. }
  212. Rect<T> top_shard {
  213. x(),
  214. y(),
  215. width(),
  216. hammer.y() - y()
  217. };
  218. Rect<T> bottom_shard {
  219. x(),
  220. hammer.y() + hammer.height(),
  221. width(),
  222. (y() + height()) - (hammer.y() + hammer.height())
  223. };
  224. Rect<T> left_shard {
  225. x(),
  226. max(hammer.y(), y()),
  227. hammer.x() - x(),
  228. min((hammer.y() + hammer.height()), (y() + height())) - max(hammer.y(), y())
  229. };
  230. Rect<T> right_shard {
  231. hammer.x() + hammer.width(),
  232. max(hammer.y(), y()),
  233. right() - hammer.right(),
  234. min((hammer.y() + hammer.height()), (y() + height())) - max(hammer.y(), y())
  235. };
  236. if (!top_shard.is_empty())
  237. pieces.unchecked_append(top_shard);
  238. if (!bottom_shard.is_empty())
  239. pieces.unchecked_append(bottom_shard);
  240. if (!left_shard.is_empty())
  241. pieces.unchecked_append(left_shard);
  242. if (!right_shard.is_empty())
  243. pieces.unchecked_append(right_shard);
  244. return pieces;
  245. }
  246. template<typename T>
  247. void Rect<T>::align_within(Rect<T> const& other, TextAlignment alignment)
  248. {
  249. switch (alignment) {
  250. case TextAlignment::Center:
  251. center_within(other);
  252. return;
  253. case TextAlignment::TopLeft:
  254. set_location(other.location());
  255. return;
  256. case TextAlignment::TopRight:
  257. set_x(other.x() + other.width() - width());
  258. set_y(other.y());
  259. return;
  260. case TextAlignment::CenterLeft:
  261. set_x(other.x());
  262. center_vertically_within(other);
  263. return;
  264. case TextAlignment::CenterRight:
  265. set_x(other.x() + other.width() - width());
  266. center_vertically_within(other);
  267. return;
  268. case TextAlignment::BottomLeft:
  269. set_x(other.x());
  270. set_y(other.y() + other.height() - height());
  271. return;
  272. case TextAlignment::BottomRight:
  273. set_x(other.x() + other.width() - width());
  274. set_y(other.y() + other.height() - height());
  275. return;
  276. }
  277. }
  278. template<typename T>
  279. void Rect<T>::set_size_around(Size<T> const& new_size, Point<T> const& fixed_point)
  280. {
  281. const T new_x = fixed_point.x() - (T)(new_size.width() * ((float)(fixed_point.x() - x()) / width()));
  282. const T new_y = fixed_point.y() - (T)(new_size.height() * ((float)(fixed_point.y() - y()) / height()));
  283. set_location({ new_x, new_y });
  284. set_size(new_size);
  285. }
  286. template<>
  287. String IntRect::to_string() const
  288. {
  289. return String::formatted("[{},{} {}x{}]", x(), y(), width(), height());
  290. }
  291. template<>
  292. String FloatRect::to_string() const
  293. {
  294. return String::formatted("[{},{} {}x{}]", x(), y(), width(), height());
  295. }
  296. }
  297. namespace IPC {
  298. bool encode(Encoder& encoder, Gfx::IntRect const& rect)
  299. {
  300. encoder << rect.location() << rect.size();
  301. return true;
  302. }
  303. bool decode(Decoder& decoder, Gfx::IntRect& rect)
  304. {
  305. Gfx::IntPoint point;
  306. Gfx::IntSize size;
  307. if (!decoder.decode(point))
  308. return false;
  309. if (!decoder.decode(size))
  310. return false;
  311. rect = { point, size };
  312. return true;
  313. }
  314. }
  315. template class Gfx::Rect<int>;
  316. template class Gfx::Rect<float>;