vec2.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <cmath>
  4. #define VEC2_EPSILON (float)0.001
  5. class Vec2 {
  6. public:
  7. float x;
  8. float y;
  9. Vec2()
  10. : x(0)
  11. , y(0) {
  12. }
  13. Vec2(float x_, float y_)
  14. : x(x_)
  15. , y(y_) {
  16. }
  17. Vec2 operator+(const Vec2& rhs) const {
  18. return Vec2(x + rhs.x, y + rhs.y);
  19. }
  20. Vec2 operator+(float s) const {
  21. return Vec2(x + s, y + s);
  22. }
  23. void operator+=(const Vec2& rhs) {
  24. x += rhs.x;
  25. y += rhs.y;
  26. }
  27. Vec2 operator-(const Vec2& rhs) const {
  28. return Vec2(x - rhs.x, y - rhs.y);
  29. }
  30. Vec2 operator-(float s) const {
  31. return Vec2(x - s, y - s);
  32. }
  33. void operator-=(const Vec2& rhs) {
  34. x -= rhs.x;
  35. y -= rhs.y;
  36. }
  37. Vec2 operator*(float s) const {
  38. return Vec2(x * s, y * s);
  39. }
  40. void operator*=(float s) {
  41. x *= s;
  42. y *= s;
  43. }
  44. Vec2 operator/(float s) const {
  45. return Vec2(x / s, y / s);
  46. }
  47. bool operator==(const Vec2& rhs) const {
  48. return x == rhs.x && y == rhs.y;
  49. }
  50. // Magnitude / length of vector
  51. float mag() const {
  52. return sqrtf(x * x + y * y);
  53. }
  54. // Magnitude squared
  55. float mag2() const {
  56. return x * x + y * y;
  57. }
  58. // Dot product: this.x * v.x + this.y * v.y
  59. float dot(const Vec2& v) const {
  60. return x * v.x + y * v.y;
  61. }
  62. // Cross product
  63. float cross(const Vec2& v) const {
  64. return x * v.y - y * v.x;
  65. }
  66. void normalize(void) {
  67. float len = mag();
  68. if(len > VEC2_EPSILON) {
  69. float inverse_len = 1.0f / len;
  70. x *= inverse_len;
  71. y *= inverse_len;
  72. }
  73. }
  74. // Distance squared between this and next
  75. float dist2(const Vec2& v) const {
  76. float dx = x - v.x;
  77. float dy = y - v.y;
  78. return dx * dx + dy * dy;
  79. }
  80. // Distance between tihs and next
  81. float dist(const Vec2& v) const {
  82. return sqrtf(dist2(v));
  83. }
  84. // void rotate(float radians) {
  85. // float c = std::cos(radians);
  86. // float s = std::sin(radians);
  87. // float xp = x * c - y * s;
  88. // float yp = x * s + y * c;
  89. // x = xp;
  90. // y = yp;
  91. // }
  92. };
  93. inline Vec2 operator*(float s, const Vec2& v) {
  94. return Vec2(s * v.x, s * v.y);
  95. }
  96. // Vec2 Vec2_unit(const Vec2& v);
  97. // // Returns true if point b lies between points a and c
  98. // bool Vec2_colinear(const Vec2& a, const Vec2& b, const Vec2& c);
  99. // // Returns the closest point to the line segment ab and p
  100. Vec2 Vec2_closest(const Vec2& a, const Vec2& b, const Vec2& p);
  101. // // Returns the closest point to the infinite ray ab and p
  102. // Vec2 Vec2_closest_ray(const Vec2& a, const Vec2& b, const Vec2& p);
  103. // // Returns if A, B, C are listed in counterclockwise order
  104. // bool Vec2_ccw(const Vec2& A, const Vec2& B, const Vec2& C);
  105. // // Returns if line AB intersects with line CD
  106. // bool Vec2_intersect(const Vec2& A, const Vec2& B, const Vec2& C, const Vec2& D);
  107. // // Returns intersection point of two lines AB and CD.
  108. // // The intersection point may not lie on either segment.
  109. // Vec2 Vec2_intersection(const Vec2& A, const Vec2& B, const Vec2& C, const Vec2& D);
  110. // // Returns distance of ray origin to intersection point on line segment. -1 if no intersection
  111. // float Vec2_ray_line_segment_intersect(
  112. // const Vec2& origin,
  113. // const Vec2& dir,
  114. // const Vec2& l1,
  115. // const Vec2& l2);
  116. // bool Vec2_project(const Vec2& A, const Vec2& B, const Vec2& P, Vec2& dst);
  117. // bool Vec2_point_circle_collide(const Vec2& point, const Vec2& circle, float radius);
  118. // bool Vec2_line_circle_collide(
  119. // const Vec2 a,
  120. // const Vec2 b,
  121. // const Vec2 circle,
  122. // float radius,
  123. // Vec2* nearest);