FloatPoint.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #pragma once
  2. #include <AK/String.h>
  3. #include <AK/LogStream.h>
  4. #include <LibDraw/Orientation.h>
  5. class FloatRect;
  6. class FloatPoint {
  7. public:
  8. FloatPoint() {}
  9. FloatPoint(float x, float y)
  10. : m_x(x)
  11. , m_y(y)
  12. {
  13. }
  14. float x() const { return m_x; }
  15. float y() const { return m_y; }
  16. void set_x(float x) { m_x = x; }
  17. void set_y(float y) { m_y = y; }
  18. void move_by(float dx, float dy)
  19. {
  20. m_x += dx;
  21. m_y += dy;
  22. }
  23. void move_by(const FloatPoint& delta)
  24. {
  25. move_by(delta.x(), delta.y());
  26. }
  27. FloatPoint translated(const FloatPoint& delta) const
  28. {
  29. FloatPoint point = *this;
  30. point.move_by(delta);
  31. return point;
  32. }
  33. FloatPoint translated(float dx, float dy) const
  34. {
  35. FloatPoint point = *this;
  36. point.move_by(dx, dy);
  37. return point;
  38. }
  39. void constrain(const FloatRect&);
  40. bool operator==(const FloatPoint& other) const
  41. {
  42. return m_x == other.m_x
  43. && m_y == other.m_y;
  44. }
  45. bool operator!=(const FloatPoint& other) const
  46. {
  47. return !(*this == other);
  48. }
  49. FloatPoint operator-() const { return { -m_x, -m_y }; }
  50. FloatPoint operator-(const FloatPoint& other) const { return { m_x - other.m_x, m_y - other.m_y }; }
  51. FloatPoint& operator-=(const FloatPoint& other)
  52. {
  53. m_x -= other.m_x;
  54. m_y -= other.m_y;
  55. return *this;
  56. }
  57. FloatPoint& operator+=(const FloatPoint& other)
  58. {
  59. m_x += other.m_x;
  60. m_y += other.m_y;
  61. return *this;
  62. }
  63. FloatPoint operator+(const FloatPoint& other) const { return { m_x + other.m_x, m_y + other.m_y }; }
  64. String to_string() const { return String::format("[%g,%g]", x(), y()); }
  65. bool is_null() const { return !m_x && !m_y; }
  66. float 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, float value)
  71. {
  72. if (orientation == Orientation::Vertical)
  73. set_y(value);
  74. else
  75. set_x(value);
  76. }
  77. float 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, float value)
  82. {
  83. if (orientation == Orientation::Vertical)
  84. set_x(value);
  85. else
  86. set_y(value);
  87. }
  88. private:
  89. float m_x { 0 };
  90. float m_y { 0 };
  91. };
  92. inline const LogStream& operator<<(const LogStream& stream, const FloatPoint& value)
  93. {
  94. return stream << value.to_string();
  95. }