Point.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Format.h>
  8. #include <AK/Math.h>
  9. #include <AK/StdLibExtras.h>
  10. #include <LibGfx/AffineTransform.h>
  11. #include <LibGfx/Forward.h>
  12. #include <LibGfx/Orientation.h>
  13. #include <LibIPC/Forward.h>
  14. #include <math.h>
  15. namespace Gfx {
  16. template<typename T>
  17. class Point {
  18. public:
  19. Point() = default;
  20. constexpr Point(T x, T y)
  21. : m_x(x)
  22. , m_y(y)
  23. {
  24. }
  25. template<typename U>
  26. constexpr Point(U x, U y)
  27. : m_x(x)
  28. , m_y(y)
  29. {
  30. }
  31. template<typename U>
  32. explicit Point(Point<U> const& other)
  33. : m_x(other.x())
  34. , m_y(other.y())
  35. {
  36. }
  37. [[nodiscard]] constexpr ALWAYS_INLINE T x() const { return m_x; }
  38. [[nodiscard]] constexpr ALWAYS_INLINE T y() const { return m_y; }
  39. ALWAYS_INLINE void set_x(T x) { m_x = x; }
  40. ALWAYS_INLINE void set_y(T y) { m_y = y; }
  41. [[nodiscard]] ALWAYS_INLINE bool is_zero() const { return m_x == 0 && m_y == 0; }
  42. void translate_by(T dx, T dy)
  43. {
  44. m_x += dx;
  45. m_y += dy;
  46. }
  47. ALWAYS_INLINE void translate_by(T dboth) { translate_by(dboth, dboth); }
  48. ALWAYS_INLINE void translate_by(Point<T> const& delta) { translate_by(delta.x(), delta.y()); }
  49. void scale_by(T dx, T dy)
  50. {
  51. m_x *= dx;
  52. m_y *= dy;
  53. }
  54. ALWAYS_INLINE void scale_by(T dboth) { scale_by(dboth, dboth); }
  55. ALWAYS_INLINE void scale_by(Point<T> const& delta) { scale_by(delta.x(), delta.y()); }
  56. void transform_by(AffineTransform const& transform) { *this = transform.map(*this); }
  57. [[nodiscard]] Point<T> translated(Point<T> const& delta) const
  58. {
  59. Point<T> point = *this;
  60. point.translate_by(delta);
  61. return point;
  62. }
  63. [[nodiscard]] Point<T> translated(T dx, T dy) const
  64. {
  65. Point<T> point = *this;
  66. point.translate_by(dx, dy);
  67. return point;
  68. }
  69. [[nodiscard]] Point<T> translated(T dboth) const
  70. {
  71. Point<T> point = *this;
  72. point.translate_by(dboth, dboth);
  73. return point;
  74. }
  75. [[nodiscard]] Point<T> scaled(T dboth) const
  76. {
  77. Point<T> point = *this;
  78. point.scale_by(dboth);
  79. return point;
  80. }
  81. [[nodiscard]] Point<T> scaled(Point<T> const& delta) const
  82. {
  83. Point<T> point = *this;
  84. point.scale_by(delta);
  85. return point;
  86. }
  87. [[nodiscard]] Point<T> scaled(T sx, T sy) const
  88. {
  89. Point<T> point = *this;
  90. point.scale_by(sx, sy);
  91. return point;
  92. }
  93. [[nodiscard]] Point<T> transformed(AffineTransform const& transform) const
  94. {
  95. Point<T> point = *this;
  96. point.transform_by(transform);
  97. return point;
  98. }
  99. void constrain(Rect<T> const&);
  100. [[nodiscard]] Point<T> constrained(Rect<T> const& rect) const
  101. {
  102. Point<T> point = *this;
  103. point.constrain(rect);
  104. return point;
  105. }
  106. [[nodiscard]] Point<T> moved_left(T amount) const { return { x() - amount, y() }; }
  107. [[nodiscard]] Point<T> moved_right(T amount) const { return { x() + amount, y() }; }
  108. [[nodiscard]] Point<T> moved_up(T amount) const { return { x(), y() - amount }; }
  109. [[nodiscard]] Point<T> moved_down(T amount) const { return { x(), y() + amount }; }
  110. template<class U>
  111. [[nodiscard]] bool operator==(Point<U> const& other) const
  112. {
  113. return x() == other.x() && y() == other.y();
  114. }
  115. [[nodiscard]] Point<T> operator+(Point<T> const& other) const { return { m_x + other.m_x, m_y + other.m_y }; }
  116. Point<T>& operator+=(Point<T> const& other)
  117. {
  118. m_x += other.m_x;
  119. m_y += other.m_y;
  120. return *this;
  121. }
  122. [[nodiscard]] Point<T> operator-() const { return { -m_x, -m_y }; }
  123. [[nodiscard]] Point<T> operator-(Point<T> const& other) const { return { m_x - other.m_x, m_y - other.m_y }; }
  124. Point<T>& operator-=(Point<T> const& other)
  125. {
  126. m_x -= other.m_x;
  127. m_y -= other.m_y;
  128. return *this;
  129. }
  130. [[nodiscard]] Point<T> operator*(T factor) const { return { m_x * factor, m_y * factor }; }
  131. Point<T>& operator*=(T factor)
  132. {
  133. m_x *= factor;
  134. m_y *= factor;
  135. return *this;
  136. }
  137. [[nodiscard]] Point<T> operator/(T factor) const { return { m_x / factor, m_y / factor }; }
  138. Point<T>& operator/=(T factor)
  139. {
  140. m_x /= factor;
  141. m_y /= factor;
  142. return *this;
  143. }
  144. [[nodiscard]] T primary_offset_for_orientation(Orientation orientation) const
  145. {
  146. return orientation == Orientation::Vertical ? y() : x();
  147. }
  148. void set_primary_offset_for_orientation(Orientation orientation, T value)
  149. {
  150. if (orientation == Orientation::Vertical) {
  151. set_y(value);
  152. } else {
  153. set_x(value);
  154. }
  155. }
  156. [[nodiscard]] T secondary_offset_for_orientation(Orientation orientation) const
  157. {
  158. return orientation == Orientation::Vertical ? x() : y();
  159. }
  160. void set_secondary_offset_for_orientation(Orientation orientation, T value)
  161. {
  162. if (orientation == Orientation::Vertical) {
  163. set_x(value);
  164. } else {
  165. set_y(value);
  166. }
  167. }
  168. [[nodiscard]] T dx_relative_to(Point<T> const& other) const
  169. {
  170. return x() - other.x();
  171. }
  172. [[nodiscard]] T dy_relative_to(Point<T> const& other) const
  173. {
  174. return y() - other.y();
  175. }
  176. // Returns pixels moved from other in either direction
  177. [[nodiscard]] T pixels_moved(Point<T> const& other) const
  178. {
  179. return max(AK::abs(dx_relative_to(other)), AK::abs(dy_relative_to(other)));
  180. }
  181. [[nodiscard]] float distance_from(Point<T> const& other) const
  182. {
  183. if (*this == other)
  184. return 0;
  185. return AK::hypot<float>(m_x - other.m_x, m_y - other.m_y);
  186. }
  187. [[nodiscard]] Point absolute_relative_distance_to(Point const& other) const
  188. {
  189. return { AK::abs(dx_relative_to(other)), AK::abs(dy_relative_to(other)) };
  190. }
  191. [[nodiscard]] Point end_point_for_aspect_ratio(Point const& previous_end_point, float aspect_ratio) const;
  192. template<typename U>
  193. requires(!IsSame<T, U>)
  194. [[nodiscard]] Point<U> to_type() const
  195. {
  196. return Point<U>(*this);
  197. }
  198. template<typename U>
  199. [[nodiscard]] Point<U> to_rounded() const
  200. {
  201. return Point<U>(roundf(x()), roundf(y()));
  202. }
  203. template<typename U>
  204. requires FloatingPoint<T>
  205. [[nodiscard]] Point<U> to_ceiled() const
  206. {
  207. return Point<U>(ceil(x()), ceil(y()));
  208. }
  209. template<typename U>
  210. requires FloatingPoint<T>
  211. [[nodiscard]] Point<U> to_floored() const
  212. {
  213. return Point<U>(AK::floor(x()), AK::floor(y()));
  214. }
  215. [[nodiscard]] ByteString to_byte_string() const;
  216. private:
  217. T m_x { 0 };
  218. T m_y { 0 };
  219. };
  220. using IntPoint = Point<int>;
  221. using FloatPoint = Point<float>;
  222. template<typename T>
  223. inline Point<T> linear_interpolate(Point<T> const& p1, Point<T> const& p2, float t)
  224. {
  225. return Point<T> { p1.x() + t * (p2.x() - p1.x()), p1.y() + t * (p2.y() - p1.y()) };
  226. }
  227. template<typename T>
  228. inline Point<T> quadratic_interpolate(Point<T> const& p1, Point<T> const& p2, Point<T> const& c1, float t)
  229. {
  230. return linear_interpolate(linear_interpolate(p1, c1, t), linear_interpolate(c1, p2, t), t);
  231. }
  232. template<typename T>
  233. inline Point<T> cubic_interpolate(Point<T> const& p1, Point<T> const& p2, Point<T> const& c1, Point<T> const& c2, float t)
  234. {
  235. return linear_interpolate(quadratic_interpolate(p1, c1, c2, t), quadratic_interpolate(c1, c2, p2, t), t);
  236. }
  237. }
  238. namespace AK {
  239. template<typename T>
  240. struct Formatter<Gfx::Point<T>> : Formatter<FormatString> {
  241. ErrorOr<void> format(FormatBuilder& builder, Gfx::Point<T> const& value)
  242. {
  243. return Formatter<FormatString>::format(builder, "[{},{}]"sv, value.x(), value.y());
  244. }
  245. };
  246. }
  247. namespace IPC {
  248. template<>
  249. ErrorOr<void> encode(Encoder&, Gfx::IntPoint const&);
  250. template<>
  251. ErrorOr<void> encode(Encoder&, Gfx::FloatPoint const&);
  252. template<>
  253. ErrorOr<Gfx::IntPoint> decode(Decoder&);
  254. template<>
  255. ErrorOr<Gfx::FloatPoint> decode(Decoder&);
  256. }
  257. template<typename T>
  258. struct AK::Traits<Gfx::Point<T>> : public AK::DefaultTraits<Gfx::Point<T>> {
  259. static constexpr bool is_trivial() { return false; }
  260. static unsigned hash(Gfx::Point<T> const& point)
  261. {
  262. return pair_int_hash(AK::Traits<T>::hash(point.x()), AK::Traits<T>::hash(point.y()));
  263. }
  264. };