BsVector2.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "Prerequisites/BsPrerequisitesUtil.h"
  5. #include "Math/BsMath.h"
  6. namespace bs
  7. {
  8. /** @addtogroup Math
  9. * @{
  10. */
  11. /** A two dimensional vector. */
  12. class BS_UTILITY_EXPORT Vector2
  13. {
  14. public:
  15. float x, y;
  16. public:
  17. Vector2()
  18. { }
  19. Vector2(BS_ZERO zero)
  20. :x(0.0f), y(0.0f)
  21. { }
  22. Vector2(float x, float y)
  23. :x(x), y(y)
  24. { }
  25. /** Exchange the contents of this vector with another. */
  26. void swap(Vector2& other)
  27. {
  28. std::swap(x, other.x);
  29. std::swap(y, other.y);
  30. }
  31. float operator[] (UINT32 i) const
  32. {
  33. assert(i < 2);
  34. return *(&x+i);
  35. }
  36. float& operator[] (UINT32 i)
  37. {
  38. assert(i < 2);
  39. return *(&x+i);
  40. }
  41. /** Pointer accessor for direct copying. */
  42. float* ptr()
  43. {
  44. return &x;
  45. }
  46. /** Pointer accessor for direct copying. */
  47. const float* ptr() const
  48. {
  49. return &x;
  50. }
  51. Vector2& operator= (const Vector2& rhs)
  52. {
  53. x = rhs.x;
  54. y = rhs.y;
  55. return *this;
  56. }
  57. Vector2& operator= (float rhs)
  58. {
  59. x = rhs;
  60. y = rhs;
  61. return *this;
  62. }
  63. bool operator== (const Vector2& rhs) const
  64. {
  65. return (x == rhs.x && y == rhs.y);
  66. }
  67. bool operator!= (const Vector2& rhs) const
  68. {
  69. return (x != rhs.x || y != rhs.y);
  70. }
  71. Vector2 operator+ (const Vector2& rhs) const
  72. {
  73. return Vector2(x + rhs.x, y + rhs.y);
  74. }
  75. Vector2 operator- (const Vector2& rhs) const
  76. {
  77. return Vector2(x - rhs.x, y - rhs.y);
  78. }
  79. Vector2 operator* (const float rhs) const
  80. {
  81. return Vector2(x * rhs, y * rhs);
  82. }
  83. Vector2 operator* (const Vector2& rhs) const
  84. {
  85. return Vector2(x * rhs.x, y * rhs.y);
  86. }
  87. Vector2 operator/ (const float rhs) const
  88. {
  89. assert(rhs != 0.0);
  90. float fInv = 1.0f / rhs;
  91. return Vector2(x * fInv, y * fInv);
  92. }
  93. Vector2 operator/ (const Vector2& rhs) const
  94. {
  95. return Vector2(x / rhs.x, y / rhs.y);
  96. }
  97. const Vector2& operator+ () const
  98. {
  99. return *this;
  100. }
  101. Vector2 operator- () const
  102. {
  103. return Vector2(-x, -y);
  104. }
  105. friend Vector2 operator* (float lhs, const Vector2& rhs)
  106. {
  107. return Vector2(lhs * rhs.x, lhs * rhs.y);
  108. }
  109. friend Vector2 operator/ (float lhs, const Vector2& rhs)
  110. {
  111. return Vector2(lhs / rhs.x, lhs / rhs.y);
  112. }
  113. friend Vector2 operator+ (Vector2& lhs, float rhs)
  114. {
  115. return Vector2(lhs.x + rhs, lhs.y + rhs);
  116. }
  117. friend Vector2 operator+ (float lhs, const Vector2& rhs)
  118. {
  119. return Vector2(lhs + rhs.x, lhs + rhs.y);
  120. }
  121. friend Vector2 operator- (const Vector2& lhs, float rhs)
  122. {
  123. return Vector2(lhs.x - rhs, lhs.y - rhs);
  124. }
  125. friend Vector2 operator- (const float lhs, const Vector2& rhs)
  126. {
  127. return Vector2(lhs - rhs.x, lhs - rhs.y);
  128. }
  129. Vector2& operator+= (const Vector2& rhs)
  130. {
  131. x += rhs.x;
  132. y += rhs.y;
  133. return *this;
  134. }
  135. Vector2& operator+= (float rhs)
  136. {
  137. x += rhs;
  138. y += rhs;
  139. return *this;
  140. }
  141. Vector2& operator-= (const Vector2& rhs)
  142. {
  143. x -= rhs.x;
  144. y -= rhs.y;
  145. return *this;
  146. }
  147. Vector2& operator-= (float rhs)
  148. {
  149. x -= rhs;
  150. y -= rhs;
  151. return *this;
  152. }
  153. Vector2& operator*= (float rhs)
  154. {
  155. x *= rhs;
  156. y *= rhs;
  157. return *this;
  158. }
  159. Vector2& operator*= (const Vector2& rhs)
  160. {
  161. x *= rhs.x;
  162. y *= rhs.y;
  163. return *this;
  164. }
  165. Vector2& operator/= (float rhs)
  166. {
  167. assert(rhs != 0.0f);
  168. float inv = 1.0f / rhs;
  169. x *= inv;
  170. y *= inv;
  171. return *this;
  172. }
  173. Vector2& operator/= (const Vector2& rhs)
  174. {
  175. x /= rhs.x;
  176. y /= rhs.y;
  177. return *this;
  178. }
  179. /** Returns the length (magnitude) of the vector. */
  180. float length() const
  181. {
  182. return Math::sqrt(x * x + y * y);
  183. }
  184. /** Returns the square of the length(magnitude) of the vector. */
  185. float squaredLength() const
  186. {
  187. return x * x + y * y;
  188. }
  189. /** Returns the distance to another vector. */
  190. float distance(const Vector2& rhs) const
  191. {
  192. return (*this - rhs).length();
  193. }
  194. /** Returns the square of the distance to another vector. */
  195. float sqrdDistance(const Vector2& rhs) const
  196. {
  197. return (*this - rhs).squaredLength();
  198. }
  199. /** Calculates the dot (scalar) product of this vector with another. */
  200. float dot(const Vector2& vec) const
  201. {
  202. return x * vec.x + y * vec.y;
  203. }
  204. /** Normalizes the vector. */
  205. float normalize()
  206. {
  207. float len = Math::sqrt(x * x + y * y);
  208. // Will also work for zero-sized vectors, but will change nothing
  209. if (len > 1e-08)
  210. {
  211. float invLen = 1.0f / len;
  212. x *= invLen;
  213. y *= invLen;
  214. }
  215. return len;
  216. }
  217. /** Generates a vector perpendicular to this vector. */
  218. Vector2 perpendicular() const
  219. {
  220. return Vector2 (-y, x);
  221. }
  222. /**
  223. * Calculates the 2 dimensional cross-product of 2 vectors, which results in a single floating point value which
  224. * is 2 times the area of the triangle.
  225. */
  226. float cross(const Vector2& other) const
  227. {
  228. return x * other.y - y * other.x;
  229. }
  230. /** Sets this vector's components to the minimum of its own and the ones of the passed in vector. */
  231. void floor(const Vector2& cmp)
  232. {
  233. if(cmp.x < x) x = cmp.x;
  234. if(cmp.y < y) y = cmp.y;
  235. }
  236. /** Sets this vector's components to the maximum of its own and the ones of the passed in vector. */
  237. void ceil(const Vector2& cmp)
  238. {
  239. if(cmp.x > x) x = cmp.x;
  240. if(cmp.y > y) y = cmp.y;
  241. }
  242. /** Returns true if this vector is zero length. */
  243. bool isZeroLength() const
  244. {
  245. float sqlen = (x * x) + (y * y);
  246. return (sqlen < (1e-06 * 1e-06));
  247. }
  248. /** Calculates a reflection vector to the plane with the given normal. */
  249. Vector2 reflect(const Vector2& normal) const
  250. {
  251. return Vector2(*this - (2 * this->dot(normal) * normal));
  252. }
  253. /** Performs Gram-Schmidt orthonormalization. */
  254. static void orthonormalize(Vector2& u, Vector2& v)
  255. {
  256. u.normalize();
  257. float dot = u.dot(v);
  258. v -= u*dot;
  259. v.normalize();
  260. }
  261. /** Normalizes the provided vector and returns a new normalized instance. */
  262. static Vector2 normalize(const Vector2& val)
  263. {
  264. float len = Math::sqrt(val.x * val.x + val.y * val.y);
  265. // Will also work for zero-sized vectors, but will change nothing
  266. Vector2 normalizedVec = val;
  267. if (len > 1e-08)
  268. {
  269. float invLen = 1.0f / len;
  270. normalizedVec.x *= invLen;
  271. normalizedVec.y *= invLen;
  272. }
  273. return normalizedVec;
  274. }
  275. /** Checks are any of the vector components NaN. */
  276. bool isNaN() const
  277. {
  278. return Math::isNaN(x) || Math::isNaN(y);
  279. }
  280. /** Returns the minimum of all the vector components as a new vector. */
  281. static Vector2 min(const Vector2& a, const Vector2& b)
  282. {
  283. return Vector2(std::min(a.x, b.x), std::min(a.y, b.y));
  284. }
  285. /** Returns the maximum of all the vector components as a new vector. */
  286. static Vector2 max(const Vector2& a, const Vector2& b)
  287. {
  288. return Vector2(std::max(a.x, b.x), std::max(a.y, b.y));
  289. }
  290. static const Vector2 ZERO;
  291. static const Vector2 ONE;
  292. static const Vector2 UNIT_X;
  293. static const Vector2 UNIT_Y;
  294. };
  295. /** @} */
  296. /** @cond SPECIALIZATIONS */
  297. BS_ALLOW_MEMCPY_SERIALIZATION(Vector2);
  298. /** @endcond */
  299. }