Vector2.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/String.h>
  8. #include <math.h>
  9. namespace Gfx {
  10. template<typename T>
  11. class Vector2 final {
  12. public:
  13. constexpr Vector2() = default;
  14. constexpr Vector2(T x, T y)
  15. : m_x(x)
  16. , m_y(y)
  17. {
  18. }
  19. constexpr T x() const { return m_x; }
  20. constexpr T y() const { return m_y; }
  21. constexpr void set_x(T value) { m_x = value; }
  22. constexpr void set_y(T value) { m_y = value; }
  23. constexpr Vector2& operator+=(const Vector2& other)
  24. {
  25. m_x += other.m_x;
  26. m_y += other.m_y;
  27. return *this;
  28. }
  29. constexpr Vector2& operator-=(const Vector2& other)
  30. {
  31. m_x -= other.m_x;
  32. m_y -= other.m_y;
  33. return *this;
  34. }
  35. constexpr Vector2 operator+(const Vector2& other) const
  36. {
  37. return Vector2(m_x + other.m_x, m_y + other.m_y);
  38. }
  39. constexpr Vector2 operator-(const Vector2& other) const
  40. {
  41. return Vector2(m_x - other.m_x, m_y - other.m_y);
  42. }
  43. constexpr Vector2 operator*(const Vector2& other) const
  44. {
  45. return Vector2(m_x * other.m_x, m_y * other.m_y);
  46. }
  47. constexpr Vector2 operator/(const Vector2& other) const
  48. {
  49. return Vector2(m_x / other.m_x, m_y / other.m_y);
  50. }
  51. constexpr Vector2 operator*(T f) const
  52. {
  53. return Vector2(m_x * f, m_y * f);
  54. }
  55. constexpr Vector2 operator/(T f) const
  56. {
  57. return Vector2(m_x / f, m_y / f);
  58. }
  59. constexpr T dot(const Vector2& other) const
  60. {
  61. return m_x * other.m_x + m_y * other.m_y;
  62. }
  63. constexpr Vector2 normalized() const
  64. {
  65. T inv_length = 1 / length();
  66. return *this * inv_length;
  67. }
  68. constexpr Vector2 clamped(T m, T x) const
  69. {
  70. Vector2 copy { *this };
  71. copy.clamp(m, x);
  72. return copy;
  73. }
  74. constexpr void clamp(T min_value, T max_value)
  75. {
  76. m_x = max(min_value, m_x);
  77. m_y = max(min_value, m_y);
  78. m_x = min(max_value, m_x);
  79. m_y = min(max_value, m_y);
  80. }
  81. constexpr void normalize()
  82. {
  83. T inv_length = 1 / length();
  84. m_x *= inv_length;
  85. m_y *= inv_length;
  86. }
  87. constexpr T length() const
  88. {
  89. return sqrt(m_x * m_x + m_y * m_y);
  90. }
  91. String to_string() const
  92. {
  93. return String::formatted("[{},{}]", x(), y());
  94. }
  95. private:
  96. T m_x;
  97. T m_y;
  98. };
  99. typedef Vector2<float> FloatVector2;
  100. typedef Vector2<double> DoubleVector2;
  101. }
  102. namespace AK {
  103. template<typename T>
  104. struct Formatter<Gfx::Vector2<T>> : Formatter<StringView> {
  105. void format(FormatBuilder& builder, const Gfx::Vector2<T>& value)
  106. {
  107. Formatter<StringView>::format(builder, value.to_string());
  108. }
  109. };
  110. }
  111. using Gfx::DoubleVector2;
  112. using Gfx::FloatVector2;
  113. using Gfx::Vector2;