Point.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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/StdLibExtras.h>
  9. #include <LibGfx/AffineTransform.h>
  10. #include <LibGfx/Forward.h>
  11. #include <LibGfx/Orientation.h>
  12. #include <LibIPC/Forward.h>
  13. #include <math.h>
  14. namespace Gfx {
  15. template<typename T>
  16. class Point {
  17. public:
  18. Point() = default;
  19. Point(T x, T y)
  20. : m_x(x)
  21. , m_y(y)
  22. {
  23. }
  24. template<typename U>
  25. Point(U x, U y)
  26. : m_x(x)
  27. , m_y(y)
  28. {
  29. }
  30. template<typename U>
  31. explicit Point(Point<U> const& other)
  32. : m_x(other.x())
  33. , m_y(other.y())
  34. {
  35. }
  36. [[nodiscard]] ALWAYS_INLINE T x() const { return m_x; }
  37. [[nodiscard]] ALWAYS_INLINE T y() const { return m_y; }
  38. ALWAYS_INLINE void set_x(T x) { m_x = x; }
  39. ALWAYS_INLINE void set_y(T y) { m_y = y; }
  40. [[nodiscard]] ALWAYS_INLINE bool is_null() const { return !m_x && !m_y; }
  41. [[nodiscard]] ALWAYS_INLINE bool is_empty() 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(Point<T> const& delta) const
  76. {
  77. Point<T> point = *this;
  78. point.scale_by(delta);
  79. return point;
  80. }
  81. [[nodiscard]] Point<T> scaled(T sx, T sy) const
  82. {
  83. Point<T> point = *this;
  84. point.scale_by(sx, sy);
  85. return point;
  86. }
  87. [[nodiscard]] Point<T> transformed(AffineTransform const& transform) const
  88. {
  89. Point<T> point = *this;
  90. point.transform_by(transform);
  91. return point;
  92. }
  93. void constrain(Rect<T> const&);
  94. [[nodiscard]] Point<T> constrained(Rect<T> const& rect) const
  95. {
  96. Point<T> point = *this;
  97. point.constrain(rect);
  98. return point;
  99. }
  100. [[nodiscard]] Point<T> moved_left(T amount) const { return { x() - amount, y() }; }
  101. [[nodiscard]] Point<T> moved_right(T amount) const { return { x() + amount, y() }; }
  102. [[nodiscard]] Point<T> moved_up(T amount) const { return { x(), y() - amount }; }
  103. [[nodiscard]] Point<T> moved_down(T amount) const { return { x(), y() + amount }; }
  104. template<class U>
  105. [[nodiscard]] bool operator==(Point<U> const& other) const
  106. {
  107. return x() == other.x() && y() == other.y();
  108. }
  109. template<class U>
  110. [[nodiscard]] bool operator!=(Point<U> const& other) const
  111. {
  112. return !(*this == other);
  113. }
  114. [[nodiscard]] Point<T> operator+(Point<T> const& other) const { return { m_x + other.m_x, m_y + other.m_y }; }
  115. Point<T>& operator+=(Point<T> const& other)
  116. {
  117. m_x += other.m_x;
  118. m_y += other.m_y;
  119. return *this;
  120. }
  121. [[nodiscard]] Point<T> operator-() const { return { -m_x, -m_y }; }
  122. [[nodiscard]] Point<T> operator-(Point<T> const& other) const { return { m_x - other.m_x, m_y - other.m_y }; }
  123. Point<T>& operator-=(Point<T> const& other)
  124. {
  125. m_x -= other.m_x;
  126. m_y -= other.m_y;
  127. return *this;
  128. }
  129. [[nodiscard]] Point<T> operator*(T factor) const { return { m_x * factor, m_y * factor }; }
  130. Point<T>& operator*=(T factor)
  131. {
  132. m_x *= factor;
  133. m_y *= factor;
  134. return *this;
  135. }
  136. [[nodiscard]] Point<T> operator/(T factor) const { return { m_x / factor, m_y / factor }; }
  137. Point<T>& operator/=(T factor)
  138. {
  139. m_x /= factor;
  140. m_y /= factor;
  141. return *this;
  142. }
  143. [[nodiscard]] T primary_offset_for_orientation(Orientation orientation) const
  144. {
  145. return orientation == Orientation::Vertical ? y() : x();
  146. }
  147. void set_primary_offset_for_orientation(Orientation orientation, T value)
  148. {
  149. if (orientation == Orientation::Vertical) {
  150. set_y(value);
  151. } else {
  152. set_x(value);
  153. }
  154. }
  155. [[nodiscard]] T secondary_offset_for_orientation(Orientation orientation) const
  156. {
  157. return orientation == Orientation::Vertical ? x() : y();
  158. }
  159. void set_secondary_offset_for_orientation(Orientation orientation, T value)
  160. {
  161. if (orientation == Orientation::Vertical) {
  162. set_x(value);
  163. } else {
  164. set_y(value);
  165. }
  166. }
  167. [[nodiscard]] T dx_relative_to(Point<T> const& other) const
  168. {
  169. return x() - other.x();
  170. }
  171. [[nodiscard]] T dy_relative_to(Point<T> const& other) const
  172. {
  173. return y() - other.y();
  174. }
  175. // Returns pixels moved from other in either direction
  176. [[nodiscard]] T pixels_moved(Point<T> const& other) const
  177. {
  178. return max(AK::abs(dx_relative_to(other)), AK::abs(dy_relative_to(other)));
  179. }
  180. [[nodiscard]] float distance_from(Point<T> const& other) const
  181. {
  182. if (*this == other)
  183. return 0;
  184. return sqrtf(powf(m_x - other.m_x, 2.0f) + powf(m_y - other.m_y, 2.0f));
  185. }
  186. [[nodiscard]] Point absolute_relative_distance_to(Point const& other) const
  187. {
  188. return { AK::abs(dx_relative_to(other)), AK::abs(dy_relative_to(other)) };
  189. }
  190. template<typename U>
  191. [[nodiscard]] Point<U> to_type() const
  192. {
  193. return Point<U>(*this);
  194. }
  195. [[nodiscard]] String to_string() const;
  196. private:
  197. T m_x { 0 };
  198. T m_y { 0 };
  199. };
  200. using IntPoint = Point<int>;
  201. using FloatPoint = Point<float>;
  202. }
  203. namespace AK {
  204. template<typename T>
  205. struct Formatter<Gfx::Point<T>> : Formatter<StringView> {
  206. void format(FormatBuilder& builder, Gfx::Point<T> const& value)
  207. {
  208. Formatter<StringView>::format(builder, value.to_string());
  209. }
  210. };
  211. }
  212. namespace IPC {
  213. bool encode(Encoder&, Gfx::IntPoint const&);
  214. bool decode(Decoder&, Gfx::IntPoint&);
  215. }