vector2.inl - Engine C API Reference

vector2.inl
  1. #include "math.h"
  2. namespace stingray_plugin_foundation {
  3. // Operators
  4. __forceinline Vector2 operator / (const Vector2 &lhs, const Vector2 &rhs) {
  5. return vector2(lhs.x/rhs.x, lhs.y/rhs.y);
  6. }
  7. __forceinline Vector2 operator * (const Vector2 &lhs, const Vector2 &rhs) {
  8. return vector2(lhs.x*rhs.x, lhs.y*rhs.y);
  9. }
  10. __forceinline Vector2 operator + (const Vector2 &lhs, const Vector2 &rhs) {
  11. return vector2(lhs.x+rhs.x, lhs.y+rhs.y);
  12. }
  13. __forceinline Vector2 operator - (const Vector2 &lhs, const Vector2 &rhs) {
  14. return vector2(lhs.x-rhs.x, lhs.y-rhs.y);
  15. }
  16. __forceinline Vector2 operator - (const Vector2 &v) {
  17. return vector2(-v.x, -v.y);
  18. }
  19. __forceinline Vector2 operator + (const Vector2 &v) {
  20. return v;
  21. }
  22. __forceinline void operator /= (Vector2 &lhs, const Vector2 &rhs) {
  23. lhs.x/=rhs.x; lhs.y/=rhs.y;
  24. }
  25. __forceinline void operator *= (Vector2 &lhs, const Vector2 &rhs) {
  26. lhs.x*=rhs.x; lhs.y*=rhs.y;
  27. }
  28. __forceinline void operator += (Vector2 &lhs, const Vector2 &rhs) {
  29. lhs.x+=rhs.x; lhs.y+=rhs.y;
  30. }
  31. __forceinline void operator -= (Vector2 &lhs, const Vector2 &rhs) {
  32. lhs.x-=rhs.x; lhs.y-=rhs.y;
  33. }
  34. __forceinline Vector2 operator/(const Vector2 &lhs, float rhs) {
  35. return vector2(lhs.x/rhs, lhs.y/rhs);
  36. }
  37. __forceinline Vector2 operator*(const Vector2 &lhs, float rhs) {
  38. return vector2(lhs.x*rhs, lhs.y*rhs);
  39. }
  40. __forceinline Vector2 operator*(float lhs, const Vector2 &rhs) {
  41. return vector2(rhs.x*lhs, rhs.y*lhs);
  42. }
  43. __forceinline Vector2 operator+(const Vector2 &lhs, float rhs) {
  44. return vector2(lhs.x+rhs, lhs.y+rhs);
  45. }
  46. __forceinline Vector2 operator-(const Vector2 &lhs, float rhs) {
  47. return vector2(lhs.x-rhs, lhs.y-rhs);
  48. }
  49. __forceinline void operator/=(Vector2 &lhs, float rhs) {
  50. lhs.x/=rhs; lhs.y/=rhs;
  51. }
  52. __forceinline void operator*=(Vector2 &lhs, float rhs) {
  53. lhs.x*=rhs; lhs.y*=rhs;
  54. }
  55. __forceinline void operator+=(Vector2 &lhs, float rhs) {
  56. lhs.x+=rhs; lhs.y+=rhs;
  57. }
  58. __forceinline void operator-=(Vector2 &lhs, float rhs) {
  59. lhs.x-=rhs; lhs.y-=rhs;
  60. }
  61. __forceinline bool operator==(const Vector2 &lhs, const Vector2 &rhs)
  62. {
  63. return lhs.x == rhs.x && lhs.y == rhs.y;
  64. }
  65. __forceinline bool operator!=(const Vector2 &lhs, const Vector2 &rhs)
  66. {
  67. return !(lhs == rhs);
  68. }
  69. // Methods
  70. __forceinline Vector2 vector2(float x, float y) {
  71. Vector2 v;
  72. v.x = x;
  73. v.y = y;
  74. return v;
  75. }
  76. __forceinline float & element(Vector2 &v, int i)
  77. {
  78. return *(&v.x + i);
  79. }
  80. __forceinline const float & element(const Vector2 &v, int i)
  81. {
  82. return *(&v.x + i);
  83. }
  84. __forceinline bool is_zero(const Vector2 &v)
  85. {
  86. return v.x == 0.0f && v.y == 0.0f;
  87. }
  88. __forceinline bool is_zero(const Vector2 &v, float eps)
  89. {
  90. return length_squared(v) <= eps*eps;
  91. }
  92. __forceinline void zero(Vector2 &v) {
  93. v.x=0.f; v.y=0.f;
  94. }
  95. __forceinline float length(const Vector2 &v) {
  96. return math::square_root(v.x*v.x + v.y*v.y);
  97. }
  98. __forceinline float length_squared(const Vector2 &v) {
  99. return v.x*v.x + v.y*v.y;
  100. }
  101. __forceinline Vector2 normalize(const Vector2 &v) {
  102. float l=length(v);
  103. if (l<0.0001)
  104. return vector2(0.f, 0.f);
  105. return v / l;
  106. }
  107. __forceinline float dot(const Vector2 &v0, const Vector2 &v1) {
  108. return v0.x * v1.x + v0.y * v1.y;
  109. }
  110. __forceinline float distance(const Vector2 &v0, const Vector2 &v1) {
  111. Vector2 tmp = vector2(v0.x-v1.x,v0.y-v1.y);
  112. return length(tmp);
  113. }
  114. __forceinline float distance_squared(const Vector2 &v0, const Vector2 &v1) {
  115. return (v0.x-v1.x)*(v0.x-v1.x) + (v0.y-v1.y)*(v0.y-v1.y);
  116. }
  117. __forceinline Vector2 lerp(const Vector2 &a, const Vector2 &b, float p)
  118. {
  119. return a * (1-p) + b * p;
  120. }
  121. __forceinline Vector2 min(const Vector2 &v0, const Vector2 &v1)
  122. {
  123. return vector2(math::min(v0.x,v1.x), math::min(v0.y,v1.y));
  124. }
  125. __forceinline Vector2 max(const Vector2 &v0, const Vector2 &v1)
  126. {
  127. return vector2(math::max(v0.x,v1.x), math::max(v0.y,v1.y));
  128. }
  129. }