A 2D tile-based sandbox game.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Vector2.h 2.7KB

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