vector2.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "assert.h"
  25. #include "types.h"
  26. #include "math_utils.h"
  27. #include "math_types.h"
  28. namespace crown
  29. {
  30. /// Negates @a a and returns the result.
  31. Vector2 operator-(const Vector2& a);
  32. /// Adds the vector @a a to @a b and returns the result.
  33. Vector2 operator+(Vector2 a, const Vector2& b);
  34. /// Subtracts the vector @a b from @a a and returns the result.
  35. Vector2 operator-(Vector2 a, const Vector2& b);
  36. /// Multiplies the vector @a a by the scalar @a k and returns the result.
  37. Vector2 operator*(Vector2 a, float k);
  38. /// @copydoc operator*(Vector2, float)
  39. Vector2 operator*(float k, Vector2 a);
  40. /// Divides the vector @a a by the scalar @a k and returns the result.
  41. Vector2 operator/(Vector2 a, float k);
  42. /// Returns true whether the vectors @a a and @a b are equal.
  43. bool operator==(const Vector2& a, const Vector2& b);
  44. /// Functions to manipulate Vector2.
  45. ///
  46. /// @ingroup Math
  47. namespace vector2
  48. {
  49. const Vector2 ZERO = Vector2(0, 0);
  50. /// Returns the dot product between the vectors @a a and @a b.
  51. float dot(const Vector2& a, const Vector2& b);
  52. /// Returns the lenght of @a a.
  53. float length(const Vector2& a);
  54. /// Returns the squared length of @a a.
  55. float squared_length(const Vector2& a);
  56. /// Sets the lenght of @a a to @a len.
  57. void set_length(Vector2& a, float len);
  58. /// Normalizes @a a and returns the result.
  59. Vector2 normalize(Vector2& a);
  60. /// Returns the distance between the points @a a and @a b.
  61. float distance(const Vector2& a, const Vector2& b);
  62. /// Returns the angle between the vectors @a a and @a b.
  63. float angle(const Vector2& a, const Vector2& b);
  64. /// Returns the pointer to the data of @a a.
  65. float* to_float_ptr(Vector2& a);
  66. /// @copydoc to_float_ptr(Vector2&)
  67. const float* to_float_ptr(const Vector2& a);
  68. } // namespace vector2
  69. inline Vector2 operator-(const Vector2& a)
  70. {
  71. return Vector2(-a.x, -a.y);
  72. }
  73. inline Vector2 operator+(Vector2 a, const Vector2& b)
  74. {
  75. a += b;
  76. return a;
  77. }
  78. inline Vector2 operator-(Vector2 a, const Vector2& b)
  79. {
  80. a -= b;
  81. return a;
  82. }
  83. inline Vector2 operator*(Vector2 a, float k)
  84. {
  85. a *= k;
  86. return a;
  87. }
  88. inline Vector2 operator*(float k, Vector2 a)
  89. {
  90. a *= k;
  91. return a;
  92. }
  93. inline Vector2 operator/(Vector2 a, float k)
  94. {
  95. a /= k;
  96. return a;
  97. }
  98. inline bool operator==(const Vector2& a, const Vector2& b)
  99. {
  100. return math::equals(a.x, b.x) && math::equals(a.y, b.y);
  101. }
  102. namespace vector2
  103. {
  104. inline float dot(const Vector2& a, const Vector2& b)
  105. {
  106. return a.x * b.x + a.y * b.y;
  107. }
  108. inline float length(const Vector2& a)
  109. {
  110. return math::sqrt(a.x * a.x + a.y * a.y);
  111. }
  112. inline float squared_length(const Vector2& a)
  113. {
  114. return a.x * a.x + a.y * a.y;
  115. }
  116. inline void set_length(Vector2& a, float len)
  117. {
  118. normalize(a);
  119. a.x *= len;
  120. a.y *= len;
  121. }
  122. inline Vector2 normalize(Vector2& a)
  123. {
  124. float inv_len = 1.0f / length(a);
  125. a.x *= inv_len;
  126. a.y *= inv_len;
  127. return a;
  128. }
  129. inline float distance(const Vector2& a, const Vector2& b)
  130. {
  131. return length(b - a);
  132. }
  133. inline float angle(const Vector2& a, const Vector2& b)
  134. {
  135. return math::acos(dot(a, b) / (length(a) * length(b)));
  136. }
  137. inline float* to_float_ptr(Vector2& a)
  138. {
  139. return &a.x;
  140. }
  141. inline const float* to_float_ptr(const Vector2& a)
  142. {
  143. return &a.x;
  144. }
  145. } // namespace vector2
  146. inline Vector2::Vector2()
  147. {
  148. // Do not initialize
  149. }
  150. inline Vector2::Vector2(float val) : x(val), y(val)
  151. {
  152. }
  153. inline Vector2::Vector2(float nx, float ny) : x(nx), y(ny)
  154. {
  155. }
  156. inline Vector2::Vector2(const float a[2]) : x(a[0]), y(a[1])
  157. {
  158. }
  159. inline const float& Vector2::operator[](uint32_t i) const
  160. {
  161. CE_ASSERT(i < 2, "Index out of bounds");
  162. return (&x)[i];
  163. }
  164. inline float& Vector2::operator[](uint32_t i)
  165. {
  166. CE_ASSERT(i < 2, "Index out of bounds");
  167. return (&x)[i];
  168. }
  169. inline Vector2& Vector2::operator+=(const Vector2& a)
  170. {
  171. x += a.x;
  172. y += a.y;
  173. return *this;
  174. }
  175. inline Vector2& Vector2::operator-=(const Vector2& a)
  176. {
  177. x -= a.x;
  178. y -= a.y;
  179. return *this;
  180. }
  181. inline Vector2& Vector2::operator*=(float k)
  182. {
  183. x *= k;
  184. y *= k;
  185. return *this;
  186. }
  187. inline Vector2& Vector2::operator/=(float k)
  188. {
  189. CE_ASSERT(k != (float)0.0, "Division by zero");
  190. float inv = (float)(1.0 / k);
  191. x *= inv;
  192. y *= inv;
  193. return *this;
  194. }
  195. } // namespace crown