Point.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #pragma once
  2. #include <AK/LogStream.h>
  3. #include <AK/String.h>
  4. #include <LibDraw/Orientation.h>
  5. class Rect;
  6. class Point {
  7. public:
  8. Point() {}
  9. Point(int x, int y)
  10. : m_x(x)
  11. , m_y(y)
  12. {
  13. }
  14. int x() const { return m_x; }
  15. int y() const { return m_y; }
  16. void set_x(int x) { m_x = x; }
  17. void set_y(int y) { m_y = y; }
  18. void move_by(int dx, int dy)
  19. {
  20. m_x += dx;
  21. m_y += dy;
  22. }
  23. void move_by(const Point& delta)
  24. {
  25. move_by(delta.x(), delta.y());
  26. }
  27. Point translated(const Point& delta) const
  28. {
  29. Point point = *this;
  30. point.move_by(delta);
  31. return point;
  32. }
  33. Point translated(int dx, int dy) const
  34. {
  35. Point point = *this;
  36. point.move_by(dx, dy);
  37. return point;
  38. }
  39. void constrain(const Rect&);
  40. bool operator==(const Point& other) const
  41. {
  42. return m_x == other.m_x
  43. && m_y == other.m_y;
  44. }
  45. bool operator!=(const Point& other) const
  46. {
  47. return !(*this == other);
  48. }
  49. Point operator-() const { return { -m_x, -m_y }; }
  50. Point operator-(const Point& other) const { return { m_x - other.m_x, m_y - other.m_y }; }
  51. Point& operator-=(const Point& other)
  52. {
  53. m_x -= other.m_x;
  54. m_y -= other.m_y;
  55. return *this;
  56. }
  57. Point& operator+=(const Point& other)
  58. {
  59. m_x += other.m_x;
  60. m_y += other.m_y;
  61. return *this;
  62. }
  63. Point operator+(const Point& other) const { return { m_x + other.m_x, m_y + other.m_y }; }
  64. String to_string() const { return String::format("[%d,%d]", x(), y()); }
  65. bool is_null() const { return !m_x && !m_y; }
  66. int primary_offset_for_orientation(Orientation orientation) const
  67. {
  68. return orientation == Orientation::Vertical ? y() : x();
  69. }
  70. void set_primary_offset_for_orientation(Orientation orientation, int value)
  71. {
  72. if (orientation == Orientation::Vertical)
  73. set_y(value);
  74. else
  75. set_x(value);
  76. }
  77. int secondary_offset_for_orientation(Orientation orientation) const
  78. {
  79. return orientation == Orientation::Vertical ? x() : y();
  80. }
  81. void set_secondary_offset_for_orientation(Orientation orientation, int value)
  82. {
  83. if (orientation == Orientation::Vertical)
  84. set_x(value);
  85. else
  86. set_y(value);
  87. }
  88. int dx_relative_to(const Point& other) const
  89. {
  90. return x() - other.x();
  91. }
  92. int dy_relative_to(const Point& other) const
  93. {
  94. return y() - other.y();
  95. }
  96. // Returns pixels moved from other in either direction
  97. int pixels_moved(const Point& other) const
  98. {
  99. return max(abs(dx_relative_to(other)), abs(dy_relative_to(other)));
  100. }
  101. private:
  102. int m_x { 0 };
  103. int m_y { 0 };
  104. };
  105. inline const LogStream& operator<<(const LogStream& stream, const Point& value)
  106. {
  107. return stream << value.to_string();
  108. }