A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Vector2.h 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #pragma once
  2. #include <utility>
  3. #include <ostream>
  4. #include <cmath>
  5. #include <msgpack.hpp>
  6. namespace SwanCommon {
  7. template<typename T>
  8. struct Vector2 {
  9. T x;
  10. T y;
  11. MSGPACK_DEFINE(x, y)
  12. constexpr Vector2(T x = 0, T y = 0): x(x), y(y) {}
  13. constexpr Vector2(std::pair<T, T> p): x(p.first), y(p.second) {}
  14. constexpr Vector2<T> &set(T x, T y) {
  15. this->x = x;
  16. this->y = y;
  17. return *this;
  18. }
  19. constexpr T length() const {
  20. return (T)std::sqrt((double)squareLength());
  21. }
  22. constexpr T squareLength() const {
  23. return this->x * this->x + this->y * this->y;
  24. }
  25. constexpr Vector2<T> sign() const {
  26. return Vector2<T>(x > 0 ? 1 : -1, y > 0 ? 1 : -1);
  27. }
  28. constexpr Vector2<T> scale(T sx, T sy) const {
  29. return Vector2<T>(x * sx, y * sy);
  30. }
  31. constexpr Vector2<T> scale(T s) const {
  32. return scale(s, s);
  33. }
  34. constexpr Vector2<T> norm() const {
  35. return *this / length();
  36. }
  37. constexpr T dot(const Vector2<T> &vec) const {
  38. return x * vec.x + y * vec.y;
  39. }
  40. constexpr operator std::pair<T, T>() const {
  41. return std::pair<T, T>(x, y);
  42. }
  43. constexpr operator Vector2<float>() const {
  44. return Vector2<float>(x, y);
  45. }
  46. constexpr bool operator==(const Vector2<T> &vec) const {
  47. return x == vec.x && y == vec.y;
  48. }
  49. constexpr bool operator!=(const Vector2<T> &vec) const {
  50. return !(*this == vec);
  51. }
  52. constexpr Vector2<T> operator-() const {
  53. return Vector2<T>(-x, -y);
  54. }
  55. constexpr Vector2<T> operator+(const Vector2<T> &vec) const {
  56. return Vector2<T>(x + vec.x, y + vec.y);
  57. }
  58. constexpr Vector2<T> &operator+=(const Vector2<T> &vec) {
  59. x += vec.x;
  60. y += vec.y;
  61. return *this;
  62. }
  63. constexpr Vector2<T> operator-(const Vector2<T> &vec) const {
  64. return Vector2<T>(x - vec.x, y - vec.y);
  65. }
  66. constexpr Vector2<T> &operator-=(const Vector2<T> &vec) {
  67. x -= vec.x;
  68. y -= vec.y;
  69. return *this;
  70. }
  71. constexpr Vector2<T> operator*(const Vector2<T> &vec) const {
  72. return Vector2<T>(x * vec.x, y * vec.y);
  73. }
  74. constexpr Vector2<T> &operator*=(const Vector2<T> &vec) {
  75. x *= vec.x;
  76. y *= vec.y;
  77. return *this;
  78. }
  79. constexpr Vector2<T> operator*(T num) const {
  80. return Vector2<T>(x * num, y * num);
  81. }
  82. constexpr Vector2<T> operator*=(T num) {
  83. x *= num;
  84. y *= num;
  85. return *this;
  86. }
  87. constexpr Vector2<T> operator/(const Vector2<T> &vec) const {
  88. return Vector2<T>(x / vec.x, y / vec.y);
  89. }
  90. constexpr Vector2<T> &operator/=(const Vector2<T> &vec) {
  91. x /= vec.x;
  92. y /= vec.y;
  93. return *this;
  94. }
  95. constexpr Vector2<T> operator/(T num) const {
  96. return Vector2<T>(x / num, y / num);
  97. }
  98. constexpr Vector2<T> operator/=(T num) {
  99. x /= num;
  100. y /= num;
  101. return *this;
  102. }
  103. static const Vector2<T> ZERO;
  104. template<typename U>
  105. friend std::ostream &operator<<(std::ostream &os, const Vector2<U> &vec);
  106. };
  107. template<typename T>
  108. const Vector2<T> Vector2<T>::ZERO = Vector2<T>(0, 0);
  109. template<typename T>
  110. std::ostream &operator<<(std::ostream &os, const Vector2<T> &vec) {
  111. os << '(' << vec.x << ", " << vec.y << ')';
  112. return os;
  113. }
  114. using Vec2 = Vector2<float>;
  115. using Vec2i = Vector2<int>;
  116. }