vector2.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #pragma once
  6. #include "assert.h"
  7. #include "types.h"
  8. #include "math_utils.h"
  9. #include "math_types.h"
  10. namespace crown
  11. {
  12. /// Negates @a a and returns the result.
  13. Vector2 operator-(const Vector2& a);
  14. /// Adds the vector @a a to @a b and returns the result.
  15. Vector2 operator+(Vector2 a, const Vector2& b);
  16. /// Subtracts the vector @a b from @a a and returns the result.
  17. Vector2 operator-(Vector2 a, const Vector2& b);
  18. /// Multiplies the vector @a a by the scalar @a k and returns the result.
  19. Vector2 operator*(Vector2 a, float k);
  20. /// @copydoc operator*(Vector2, float)
  21. Vector2 operator*(float k, Vector2 a);
  22. /// Divides the vector @a a by the scalar @a k and returns the result.
  23. Vector2 operator/(Vector2 a, float k);
  24. /// Returns true whether the vectors @a a and @a b are equal.
  25. bool operator==(const Vector2& a, const Vector2& b);
  26. /// Functions to manipulate Vector2.
  27. ///
  28. /// @ingroup Math
  29. namespace vector2
  30. {
  31. const Vector2 ZERO = Vector2(0, 0);
  32. /// Returns the dot product between the vectors @a a and @a b.
  33. float dot(const Vector2& a, const Vector2& b);
  34. /// Returns the lenght of @a a.
  35. float length(const Vector2& a);
  36. /// Returns the squared length of @a a.
  37. float squared_length(const Vector2& a);
  38. /// Sets the lenght of @a a to @a len.
  39. void set_length(Vector2& a, float len);
  40. /// Normalizes @a a and returns the result.
  41. Vector2 normalize(Vector2& a);
  42. /// Returns the distance between the points @a a and @a b.
  43. float distance(const Vector2& a, const Vector2& b);
  44. /// Returns the angle between the vectors @a a and @a b.
  45. float angle(const Vector2& a, const Vector2& b);
  46. /// Returns the pointer to the data of @a a.
  47. float* to_float_ptr(Vector2& a);
  48. /// @copydoc to_float_ptr(Vector2&)
  49. const float* to_float_ptr(const Vector2& a);
  50. } // namespace vector2
  51. inline Vector2 operator-(const Vector2& a)
  52. {
  53. return Vector2(-a.x, -a.y);
  54. }
  55. inline Vector2 operator+(Vector2 a, const Vector2& b)
  56. {
  57. a += b;
  58. return a;
  59. }
  60. inline Vector2 operator-(Vector2 a, const Vector2& b)
  61. {
  62. a -= b;
  63. return a;
  64. }
  65. inline Vector2 operator*(Vector2 a, float k)
  66. {
  67. a *= k;
  68. return a;
  69. }
  70. inline Vector2 operator*(float k, Vector2 a)
  71. {
  72. a *= k;
  73. return a;
  74. }
  75. inline Vector2 operator/(Vector2 a, float k)
  76. {
  77. a /= k;
  78. return a;
  79. }
  80. inline bool operator==(const Vector2& a, const Vector2& b)
  81. {
  82. return equals(a.x, b.x) && equals(a.y, b.y);
  83. }
  84. namespace vector2
  85. {
  86. inline float dot(const Vector2& a, const Vector2& b)
  87. {
  88. return a.x * b.x + a.y * b.y;
  89. }
  90. inline float length(const Vector2& a)
  91. {
  92. return sqrt(a.x * a.x + a.y * a.y);
  93. }
  94. inline float squared_length(const Vector2& a)
  95. {
  96. return a.x * a.x + a.y * a.y;
  97. }
  98. inline void set_length(Vector2& a, float len)
  99. {
  100. normalize(a);
  101. a.x *= len;
  102. a.y *= len;
  103. }
  104. inline Vector2 normalize(Vector2& a)
  105. {
  106. float inv_len = 1.0f / length(a);
  107. a.x *= inv_len;
  108. a.y *= inv_len;
  109. return a;
  110. }
  111. inline float distance(const Vector2& a, const Vector2& b)
  112. {
  113. return length(b - a);
  114. }
  115. inline float angle(const Vector2& a, const Vector2& b)
  116. {
  117. return acos(dot(a, b) / (length(a) * length(b)));
  118. }
  119. inline float* to_float_ptr(Vector2& a)
  120. {
  121. return &a.x;
  122. }
  123. inline const float* to_float_ptr(const Vector2& a)
  124. {
  125. return &a.x;
  126. }
  127. } // namespace vector2
  128. inline Vector2::Vector2()
  129. {
  130. // Do not initialize
  131. }
  132. inline Vector2::Vector2(float val) : x(val), y(val)
  133. {
  134. }
  135. inline Vector2::Vector2(float nx, float ny) : x(nx), y(ny)
  136. {
  137. }
  138. inline Vector2::Vector2(const float a[2]) : x(a[0]), y(a[1])
  139. {
  140. }
  141. inline const float& Vector2::operator[](uint32_t i) const
  142. {
  143. CE_ASSERT(i < 2, "Index out of bounds");
  144. return (&x)[i];
  145. }
  146. inline float& Vector2::operator[](uint32_t i)
  147. {
  148. CE_ASSERT(i < 2, "Index out of bounds");
  149. return (&x)[i];
  150. }
  151. inline Vector2& Vector2::operator+=(const Vector2& a)
  152. {
  153. x += a.x;
  154. y += a.y;
  155. return *this;
  156. }
  157. inline Vector2& Vector2::operator-=(const Vector2& a)
  158. {
  159. x -= a.x;
  160. y -= a.y;
  161. return *this;
  162. }
  163. inline Vector2& Vector2::operator*=(float k)
  164. {
  165. x *= k;
  166. y *= k;
  167. return *this;
  168. }
  169. inline Vector2& Vector2::operator/=(float k)
  170. {
  171. CE_ASSERT(k != (float)0.0, "Division by zero");
  172. float inv = (float)(1.0 / k);
  173. x *= inv;
  174. y *= inv;
  175. return *this;
  176. }
  177. } // namespace crown