Vec2.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 "MathUtils.h"
  27. namespace crown
  28. {
  29. /// 2D column vector.
  30. class Vec2
  31. {
  32. public:
  33. float x, y;
  34. /// Does nothing for efficiency.
  35. Vec2();
  36. /// Initializes all the components to val
  37. Vec2(float val);
  38. /// Constructs from two components
  39. Vec2(float nx, float ny);
  40. /// Constructs from array
  41. Vec2(const float v[2]);
  42. Vec2(const Vec2& a);
  43. /// Random access by index
  44. float operator[](uint32_t i) const;
  45. /// Random access by index
  46. float& operator[](uint32_t i);
  47. Vec2 operator+(const Vec2& a) const;
  48. Vec2& operator+=(const Vec2& a);
  49. Vec2 operator-(const Vec2& a) const;
  50. Vec2& operator-=(const Vec2& a);
  51. Vec2 operator*(float k) const;
  52. Vec2& operator*=(float k);
  53. Vec2 operator/(float k) const;
  54. Vec2& operator/=(float k);
  55. /// Dot product
  56. float dot(const Vec2& a) const;
  57. /// For simmetry
  58. friend Vec2 operator*(float k, const Vec2& a);
  59. bool operator==(const Vec2& other) const;
  60. bool operator!=(const Vec2& other) const;
  61. /// Returns whether all the components of this vector are smaller than all of the @a other vector
  62. bool operator<(const Vec2& other) const;
  63. /// Returns whether all the components of this vector are greater than all of the @a other vector
  64. bool operator>(const Vec2& other) const;
  65. /// Returns the vector's length
  66. float length() const;
  67. /// Returns the vector's squared length
  68. float squared_length() const;
  69. /// Sets the vector's length
  70. void set_length(float len);
  71. float get_angle() const;
  72. float get_angle_2d() const;
  73. /// Normalizes the vector
  74. Vec2& normalize();
  75. /// Returns the normalized vector
  76. Vec2 get_normalized() const;
  77. /// Negates the vector (i.e. builds the inverse)
  78. Vec2& negate();
  79. /// Negates the vector (i.e. builds the inverse)
  80. Vec2 operator-() const;
  81. /// Returns the distance
  82. float get_distance_to(const Vec2& a) const;
  83. /// Returns the angle in radian
  84. float get_angle_between(const Vec2& a) const;
  85. /// Sets all components to zero
  86. void zero();
  87. /// Returns the pointer to the vector's data
  88. float* to_float_ptr();
  89. /// Returns the pointer to the vector's data
  90. const float* to_float_ptr() const;
  91. static const Vec2 ZERO;
  92. static const Vec2 ONE;
  93. static const Vec2 XAXIS;
  94. static const Vec2 YAXIS;
  95. };
  96. //-----------------------------------------------------------------------------
  97. inline Vec2::Vec2()
  98. {
  99. }
  100. //-----------------------------------------------------------------------------
  101. inline Vec2::Vec2(float val) : x(val), y(val)
  102. {
  103. }
  104. //-----------------------------------------------------------------------------
  105. inline Vec2::Vec2(float nx, float ny) : x(nx), y(ny)
  106. {
  107. }
  108. //-----------------------------------------------------------------------------
  109. inline Vec2::Vec2(const float a[2]) : x(a[0]), y(a[1])
  110. {
  111. }
  112. //-----------------------------------------------------------------------------
  113. inline Vec2::Vec2(const Vec2& a) : x(a.x), y(a.y)
  114. {
  115. }
  116. //-----------------------------------------------------------------------------
  117. inline float Vec2::operator[](uint32_t i) const
  118. {
  119. CE_ASSERT(i < 2, "Index must be < 2");
  120. return (&x)[i];
  121. }
  122. //-----------------------------------------------------------------------------
  123. inline float& Vec2::operator[](uint32_t i)
  124. {
  125. CE_ASSERT(i < 2, "Index must be < 2");
  126. return (&x)[i];
  127. }
  128. //-----------------------------------------------------------------------------
  129. inline Vec2 Vec2::operator+(const Vec2& a) const
  130. {
  131. return Vec2(x + a.x, y + a.y);
  132. }
  133. //-----------------------------------------------------------------------------
  134. inline Vec2& Vec2::operator+=(const Vec2& a)
  135. {
  136. x += a.x;
  137. y += a.y;
  138. return *this;
  139. }
  140. //-----------------------------------------------------------------------------
  141. inline Vec2 Vec2::operator-(const Vec2& a) const
  142. {
  143. return Vec2(x - a.x, y - a.y);
  144. }
  145. //-----------------------------------------------------------------------------
  146. inline Vec2& Vec2::operator-=(const Vec2& a)
  147. {
  148. x -= a.x;
  149. y -= a.y;
  150. return *this;
  151. }
  152. //-----------------------------------------------------------------------------
  153. inline Vec2 Vec2::operator*(float k) const
  154. {
  155. return Vec2(x * k, y * k);
  156. }
  157. //-----------------------------------------------------------------------------
  158. inline Vec2& Vec2::operator*=(float k)
  159. {
  160. x *= k;
  161. y *= k;
  162. return *this;
  163. }
  164. //-----------------------------------------------------------------------------
  165. inline Vec2 Vec2::operator/(float k) const
  166. {
  167. CE_ASSERT(k != (float)0.0, "Division by zero");
  168. float inv = (float)(1.0 / k);
  169. return Vec2(x * inv, y * inv);
  170. }
  171. //-----------------------------------------------------------------------------
  172. inline Vec2& Vec2::operator/=(float k)
  173. {
  174. CE_ASSERT(k != (float)0.0, "Division by zero");
  175. float inv = (float)(1.0 / k);
  176. x *= inv;
  177. y *= inv;
  178. return *this;
  179. }
  180. //-----------------------------------------------------------------------------
  181. inline float Vec2::dot(const Vec2& a) const
  182. {
  183. return x * a.x + y * a.y;
  184. }
  185. //-----------------------------------------------------------------------------
  186. inline bool Vec2::operator==(const Vec2& other) const
  187. {
  188. return math::equals(x, other.x) && math::equals(y, other.y);
  189. }
  190. //-----------------------------------------------------------------------------
  191. inline bool Vec2::operator!=(const Vec2& other) const
  192. {
  193. return !math::equals(x, other.x) || !math::equals(y, other.y);
  194. }
  195. //-----------------------------------------------------------------------------
  196. inline bool Vec2::operator<(const Vec2& other) const
  197. {
  198. return ((x < other.x) && (y < other.y));
  199. }
  200. //-----------------------------------------------------------------------------
  201. inline bool Vec2::operator>(const Vec2& other) const
  202. {
  203. return ((x > other.x) && (y > other.y));
  204. }
  205. //-----------------------------------------------------------------------------
  206. inline float Vec2::length() const
  207. {
  208. return math::sqrt(x * x + y * y);
  209. }
  210. //-----------------------------------------------------------------------------
  211. inline float Vec2::squared_length() const
  212. {
  213. return x * x + y * y;
  214. }
  215. //-----------------------------------------------------------------------------
  216. inline void Vec2::set_length(float len)
  217. {
  218. normalize();
  219. x *= len;
  220. y *= len;
  221. }
  222. //-----------------------------------------------------------------------------
  223. inline float Vec2::get_angle() const
  224. {
  225. return math::atan2(y, x);
  226. }
  227. //-----------------------------------------------------------------------------
  228. inline float Vec2::get_angle_2d() const
  229. {
  230. return math::atan2(-y, x);
  231. }
  232. //-----------------------------------------------------------------------------
  233. inline Vec2& Vec2::normalize()
  234. {
  235. float len = length();
  236. if (math::equals(len, (float)0.0))
  237. {
  238. return *this;
  239. }
  240. x /= len;
  241. y /= len;
  242. return *this;
  243. }
  244. //-----------------------------------------------------------------------------
  245. inline Vec2 Vec2::get_normalized() const
  246. {
  247. Vec2 tmp(x, y);
  248. return tmp.normalize();
  249. }
  250. //-----------------------------------------------------------------------------
  251. inline Vec2& Vec2::negate()
  252. {
  253. x = -x;
  254. y = -y;
  255. return *this;
  256. }
  257. //-----------------------------------------------------------------------------
  258. inline Vec2 Vec2::operator-() const
  259. {
  260. return Vec2(-x, -y);
  261. }
  262. //-----------------------------------------------------------------------------
  263. inline float Vec2::get_distance_to(const Vec2& a) const
  264. {
  265. return (*this - a).length();
  266. }
  267. //-----------------------------------------------------------------------------
  268. inline float Vec2::get_angle_between(const Vec2& a) const
  269. {
  270. return math::acos(this->dot(a) / (this->length() * a.length()));
  271. }
  272. //-----------------------------------------------------------------------------
  273. inline void Vec2::zero()
  274. {
  275. x = 0.0;
  276. y = 0.0;
  277. }
  278. //-----------------------------------------------------------------------------
  279. inline float* Vec2::to_float_ptr()
  280. {
  281. return &x;
  282. }
  283. //-----------------------------------------------------------------------------
  284. inline const float* Vec2::to_float_ptr() const
  285. {
  286. return &x;
  287. }
  288. //-----------------------------------------------------------------------------
  289. inline Vec2 get_projected_parallel(const Vec2& v, const Vec2& n)
  290. {
  291. float n_len_q;
  292. n_len_q = n.length();
  293. n_len_q = n_len_q * n_len_q;
  294. return n * (v.dot(n) / n_len_q);
  295. }
  296. //-----------------------------------------------------------------------------
  297. inline Vec2 get_projected_perpendicular(const Vec2& v, const Vec2& n)
  298. {
  299. return v - get_projected_parallel(v, n);
  300. }
  301. //-----------------------------------------------------------------------------
  302. inline Vec2 operator*(float k, const Vec2& a)
  303. {
  304. return a * k;
  305. }
  306. } // namespace crown