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 2.5KB

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