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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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)squareLength());
  18. }
  19. constexpr T squareLength() {
  20. return this->x * this->x + this->y * this->y;
  21. }
  22. constexpr Vector2<T> sign() {
  23. return Vector2<T>(x > 0 ? 1 : -1, y > 0 ? 1 : -1);
  24. }
  25. constexpr Vector2<T> scale(T sx, T sy) {
  26. return Vector2<T>(x * sx, y * sy);
  27. }
  28. constexpr Vector2<T> scale(T s) {
  29. return scale(s, s);
  30. }
  31. constexpr operator std::pair<T, T>() const {
  32. return std::pair<T, T>(x, y);
  33. }
  34. constexpr operator Vector2<float>() const {
  35. return Vector2<float>(x, y);
  36. }
  37. constexpr bool operator==(const Vector2<T> &vec) const {
  38. return x == vec.x && y == vec.y;
  39. }
  40. constexpr bool operator!=(const Vector2<T> &vec) const {
  41. return !(*this == vec);
  42. }
  43. constexpr Vector2<T> operator-() const {
  44. return Vector2<T>(-x, -y);
  45. }
  46. constexpr Vector2<T> operator+(const Vector2<T> &vec) const {
  47. return Vector2<T>(x + vec.x, y + vec.y);
  48. }
  49. constexpr Vector2<T> &operator+=(const Vector2<T> &vec) {
  50. x += vec.x;
  51. y += vec.y;
  52. return *this;
  53. }
  54. constexpr Vector2<T> operator-(const Vector2<T> &vec) const {
  55. return Vector2<T>(x - vec.x, y - vec.y);
  56. }
  57. constexpr Vector2<T> &operator-=(const Vector2<T> &vec) {
  58. x -= vec.x;
  59. y -= vec.y;
  60. return *this;
  61. }
  62. constexpr Vector2<T> operator*(const Vector2<T> &vec) const {
  63. return Vector2<T>(x * vec.x, y * vec.y);
  64. }
  65. constexpr Vector2<T> &operator*=(const Vector2<T> &vec) {
  66. x *= vec.x;
  67. y *= vec.y;
  68. return *this;
  69. }
  70. constexpr Vector2<T> operator*(T num) const {
  71. return Vector2<T>(x * num, y * num);
  72. }
  73. constexpr Vector2<T> operator*=(T num) {
  74. x *= num;
  75. y *= num;
  76. return *this;
  77. }
  78. constexpr Vector2<T> operator/(const Vector2<T> &vec) const {
  79. return Vector2<T>(x / vec.x, y / vec.y);
  80. }
  81. constexpr Vector2<T> &operator/=(const Vector2<T> &vec) {
  82. x /= vec.x;
  83. y /= vec.y;
  84. return *this;
  85. }
  86. constexpr Vector2<T> operator/(T num) const {
  87. return Vector2<T>(x / num, y / num);
  88. }
  89. constexpr Vector2<T> operator/=(T num) {
  90. x /= num;
  91. y /= num;
  92. return *this;
  93. }
  94. static const Vector2<T> ZERO;
  95. template<typename U>
  96. friend std::ostream &operator<<(std::ostream &os, const Vector2<U> &vec);
  97. };
  98. template<typename T>
  99. const Vector2<T> Vector2<T>::ZERO = Vector2<T>(0, 0);
  100. template<typename T>
  101. std::ostream &operator<<(std::ostream &os, const Vector2<T> &vec) {
  102. os << '(' << vec.x << ", " << vec.y << ')';
  103. return os;
  104. }
  105. using Vec2 = Vector2<float>;
  106. using Vec2i = Vector2<int>;
  107. }