BsVector2.h 8.5 KB

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