BsVector2.h 8.6 KB

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