Vector.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #pragma once
  2. #include <cmath>
  3. #include "Helpers.h"
  4. struct Vector {
  5. float x;
  6. float y;
  7. Vector &operator=(const Vector &other) {
  8. if (this != &other) {
  9. x = other.x;
  10. y = other.y;
  11. }
  12. return *this;
  13. }
  14. Vector(const Vector &other) : x(other.x), y(other.y) {}
  15. Vector(float _x, float _y) : x(_x), y(_y) {}
  16. Vector() : x(0), y(0) {}
  17. Vector operator+(Vector const &other) {
  18. return Vector({x + other.x, y + other.y});
  19. }
  20. Vector operator+(float const &other) {
  21. return Vector({x + other, y + other});
  22. }
  23. Vector &operator+=(Vector const &other) {
  24. x += other.x;
  25. y += other.y;
  26. return *this;
  27. }
  28. Vector operator-(Vector const &other) {
  29. return Vector{x - other.x, y - other.y};
  30. }
  31. Vector &operator-=(Vector const &other) {
  32. x -= other.x;
  33. y -= other.y;
  34. return *this;
  35. }
  36. Vector operator-(Vector const &other) const {
  37. return Vector{x - other.x, y - other.y};
  38. }
  39. Vector operator*(Vector const &other) {
  40. return Vector{x * other.x, y * other.y};
  41. }
  42. Vector operator*(float other) {
  43. return Vector{x * other, y * other};
  44. }
  45. Vector &operator*=(Vector const &other) {
  46. x *= other.x;
  47. y *= other.y;
  48. return *this;
  49. }
  50. Vector &operator*=(float other) {
  51. x *= other;
  52. y *= other;
  53. return *this;
  54. }
  55. Vector operator/(Vector const &other) {
  56. return Vector{x / other.x, y / other.y};
  57. }
  58. Vector &operator/=(Vector const &other) {
  59. x /= other.x;
  60. y /= other.y;
  61. return *this;
  62. }
  63. Vector operator/(float other) {
  64. return Vector{x / other, y / other};
  65. }
  66. Vector &operator/=(float other) {
  67. x /= other;
  68. y /= other;
  69. return *this;
  70. }
  71. float magnitude() {
  72. return sqrtf(x * x + y * y);
  73. }
  74. float distance(Vector const &other) {
  75. Vector v = *this - other;
  76. return v.magnitude();
  77. }
  78. void normalize() {
  79. float m = magnitude();
  80. if (m == 0) {
  81. x = 0;
  82. y = 0;
  83. }else{
  84. x = x / m;
  85. y = y / m;
  86. }
  87. }
  88. Vector normalized() {
  89. float m = magnitude();
  90. if (m == 0) return {0, 0};
  91. return {x / m, y / m};
  92. }
  93. Vector inverse() {
  94. return {-x, -y};
  95. }
  96. float dot(Vector const &b) {
  97. return x * b.x + y * b.y;
  98. }
  99. void rotate(float deg) {
  100. float tx = x;
  101. float ty = y;
  102. x = (float) (cos(deg) * (double) tx - sin(deg) * (double) ty);
  103. y = (float) (sin(deg) * (double) tx + cos(deg) * (double) ty);
  104. }
  105. void rounded() {
  106. x = (float) round(x);
  107. y = (float) round(y);
  108. }
  109. Vector rotated(float deg) {
  110. return {
  111. (float) (cos(deg) * (double) x - sin(deg) * (double) y),
  112. (float) (sin(deg) * (double) x + cos(deg) * (double) y)
  113. };
  114. }
  115. float cross(Vector const &other) {
  116. return x * other.x - y * other.y;
  117. }
  118. Vector perpendicular() {
  119. return {-y, x};
  120. }
  121. Vector project(Vector const &lineA, Vector const &lineB, bool *success) {
  122. Vector AB = lineB - lineA;
  123. Vector AC = *this - lineA;
  124. float k = AC.dot(AB) / AB.dot(AB);
  125. if (k < 0 || k > 1) {
  126. *success = false;
  127. return {};
  128. }
  129. *success = true;
  130. return {
  131. k * AB.x + lineA.x,
  132. k * AB.y + lineA.y
  133. };
  134. }
  135. static Vector Lerp(Vector const &start, Vector const &end, float time) {
  136. return {
  137. lerp(start.x, end.x, time),
  138. lerp(start.y, end.y, time)
  139. };
  140. }
  141. static Vector Quadratic(Vector const &start, Vector const &control, Vector const &end, float time) {
  142. Vector a = Vector::Lerp(start, control, time);
  143. Vector b = Vector::Lerp(control, end, time);
  144. return Vector::Lerp(a, b, time);
  145. }
  146. };