Vector2.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #ifndef MATH_VECTOR2_H
  24. #define MATH_VECTOR2_H
  25. #include "MathDefs.h"
  26. //! A two-dimensional vector
  27. class Vector2
  28. {
  29. public:
  30. //! Construct an undefined vector
  31. Vector2()
  32. {
  33. }
  34. //! Copy-construct from another vector
  35. Vector2(const Vector2& vector) :
  36. mX(vector.mX),
  37. mY(vector.mY)
  38. {
  39. }
  40. //! Construct from coordinates
  41. Vector2(float x, float y) :
  42. mX(x),
  43. mY(y)
  44. {
  45. }
  46. //! Construct from a float array
  47. Vector2(const float* data) :
  48. mX(data[0]),
  49. mY(data[1])
  50. {
  51. }
  52. //! Assign from another vector
  53. Vector2& operator = (const Vector2& rhs)
  54. {
  55. mX = rhs.mX;
  56. mY = rhs.mY;
  57. return *this;
  58. }
  59. //! Test for equality with another vector
  60. bool operator == (const Vector2& rhs) const
  61. {
  62. return (mX == rhs.mX) && (mY == rhs.mY);
  63. }
  64. //! Test for inequality with another vector
  65. bool operator != (const Vector2& rhs) const
  66. {
  67. return (mX != rhs.mX) || (mY != rhs.mY);
  68. }
  69. //! Add a vector
  70. Vector2 operator + (const Vector2& rhs) const
  71. {
  72. return Vector2(mX + rhs.mX, mY + rhs.mY);
  73. }
  74. //! Return negation
  75. Vector2 operator - () const
  76. {
  77. return Vector2(-mX, -mY);
  78. }
  79. //! Subtract a vector
  80. Vector2 operator - (const Vector2& rhs) const
  81. {
  82. return Vector2(mX - rhs.mX, mY - rhs.mY);
  83. }
  84. //! Multiply with a scalar
  85. Vector2 operator * (float rhs) const
  86. {
  87. return Vector2(mX * rhs, mY * rhs);
  88. }
  89. //! Multiply with a vector
  90. Vector2 operator * (const Vector2& rhs) const
  91. {
  92. return Vector2(mX * rhs.mX, mY * rhs.mY);
  93. }
  94. //! Divide by a scalar
  95. Vector2 operator / (float rhs) const
  96. {
  97. return Vector2(mX / rhs, mY / rhs);
  98. }
  99. //! Divide by a vector
  100. Vector2 operator / (const Vector2& rhs) const
  101. {
  102. return Vector2(mX / rhs.mX, mY / rhs.mY);
  103. }
  104. //! Add-assign a vector
  105. Vector2& operator += (const Vector2& rhs)
  106. {
  107. mX += rhs.mX;
  108. mY += rhs.mY;
  109. return *this;
  110. }
  111. //! Subtract-assign a vector
  112. Vector2& operator -= (const Vector2& rhs)
  113. {
  114. mX -= rhs.mX;
  115. mY -= rhs.mY;
  116. return *this;
  117. }
  118. //! Multiply-assign a scalar
  119. Vector2& operator *= (float rhs)
  120. {
  121. mX *= rhs;
  122. mY *= rhs;
  123. return *this;
  124. }
  125. //! Multiply-assign a vector
  126. Vector2& operator *= (const Vector2& rhs)
  127. {
  128. mX *= rhs.mX;
  129. mY *= rhs.mY;
  130. return *this;
  131. }
  132. //! Divide-assign a scalar
  133. Vector2& operator /= (float rhs)
  134. {
  135. mX /= rhs;
  136. mY /= rhs;
  137. return *this;
  138. }
  139. //! Divide-assign a vector
  140. Vector2& operator /= (const Vector2& rhs)
  141. {
  142. mX /= rhs.mX;
  143. mY /= rhs.mY;
  144. return *this;
  145. }
  146. //! Normalize to unit length and return the previous length
  147. float normalize()
  148. {
  149. float len = getLength();
  150. if (len < M_EPSILON)
  151. return len;
  152. float invLen = 1.0f / len;
  153. mX *= invLen;
  154. mY *= invLen;
  155. return len;
  156. }
  157. //! Normalize to unit length using fast inverse square root
  158. void normalizeFast()
  159. {
  160. float invLen = fastInvSqrt(mX * mX + mY * mY);
  161. mX *= invLen;
  162. mY *= invLen;
  163. }
  164. //! Return length
  165. float getLength() const
  166. {
  167. return sqrtf(mX * mX + mY * mY);
  168. }
  169. //! Return length using fast square root
  170. float getLengthFast() const
  171. {
  172. return fastSqrt(mX * mX + mY * mY);
  173. }
  174. //! Return squared length
  175. float getLengthSquared() const
  176. {
  177. return mX * mX + mY * mY;
  178. }
  179. //! Calculate dot product
  180. float dotProduct(const Vector2& rhs) const
  181. {
  182. return mX * rhs.mX + mY * rhs.mY;
  183. }
  184. //! Calculate absolute dot product
  185. float absDotProduct(const Vector2& rhs) const
  186. {
  187. return fabsf(mX * rhs.mX) + fabsf(mY * rhs.mY);
  188. }
  189. //! Linear interpolation with another vector
  190. Vector2 lerp(const Vector2& rhs, float t) const
  191. {
  192. return *this * (1.0f - t) + rhs * t;
  193. }
  194. //! Return normalized to unit length
  195. Vector2 getNormalized() const
  196. {
  197. float len = getLength();
  198. if (len < M_EPSILON)
  199. return *this;
  200. float invLen = 1.0f / len;
  201. return *this * invLen;
  202. }
  203. //! Return normalized to unit length using fast inverse square root
  204. Vector2 getNormalizedFast() const
  205. {
  206. float invLen = fastInvSqrt(mX * mX + mY * mY);
  207. return *this * invLen;
  208. }
  209. //! Return float data
  210. const float* getData() const { return &mX; }
  211. //! X coordinate
  212. float mX;
  213. //! Y coordinate
  214. float mY;
  215. //! Zero vector
  216. static const Vector2 sZero;
  217. //! (-1,0) vector
  218. static const Vector2 sLeft;
  219. //! (1,0) vector
  220. static const Vector2 sRight;
  221. //! (0,1) vector
  222. static const Vector2 sUp;
  223. //! (0,-1) vector
  224. static const Vector2 sDown;
  225. //! (1,1) vector
  226. static const Vector2 sUnity;
  227. };
  228. //! Multiply Vector2 with a scalar
  229. inline Vector2 operator * (float lhs, const Vector2& rhs) { return rhs * lhs; }
  230. //! A two-dimensional vector with integer values
  231. class IntVector2
  232. {
  233. public:
  234. //! Construct an undefined vector
  235. IntVector2()
  236. {
  237. }
  238. //! Construct from coordinates
  239. IntVector2(int x, int y) :
  240. mX(x),
  241. mY(y)
  242. {
  243. }
  244. //! Test for equality with another vector
  245. bool operator == (const IntVector2& rhs) const
  246. {
  247. return (mX == rhs.mX) && (mY == rhs.mY);
  248. }
  249. //! Test for inequality with another vector
  250. bool operator != (const IntVector2& rhs) const
  251. {
  252. return (mX != rhs.mX) ||(mY != rhs.mY);
  253. }
  254. //! Add a vector
  255. IntVector2 operator + (const IntVector2& rhs) const
  256. {
  257. return IntVector2(mX + rhs.mX, mY + rhs.mY);
  258. }
  259. //! Return negation
  260. IntVector2 operator - () const
  261. {
  262. return IntVector2(-mX, -mY);
  263. }
  264. //! Subtract a vector
  265. IntVector2 operator - (const IntVector2& rhs) const
  266. {
  267. return IntVector2(mX - rhs.mX, mY - rhs.mY);
  268. }
  269. //! Multiply with a scalar
  270. IntVector2 operator * (int rhs) const
  271. {
  272. return IntVector2(mX * rhs, mY * rhs);
  273. }
  274. //! Divide by a scalar
  275. IntVector2 operator / (int rhs) const
  276. {
  277. return IntVector2(mX / rhs, mY / rhs);
  278. }
  279. //! Add-assign a vector
  280. IntVector2& operator += (const IntVector2& rhs)
  281. {
  282. mX += rhs.mX;
  283. mY += rhs.mY;
  284. return *this;
  285. }
  286. //! Subtract-assign a vector
  287. IntVector2& operator -= (const IntVector2& rhs)
  288. {
  289. mX -= rhs.mX;
  290. mY -= rhs.mY;
  291. return *this;
  292. }
  293. //! Multiply-assign a scalar
  294. IntVector2& operator *= (int rhs)
  295. {
  296. mX *= rhs;
  297. mY *= rhs;
  298. return *this;
  299. }
  300. //! Divide-assign a scalar
  301. IntVector2& operator /= (int rhs)
  302. {
  303. mX /= rhs;
  304. mY /= rhs;
  305. return *this;
  306. }
  307. //! Return integer data
  308. const int* getData() const { return &mX; }
  309. //! X coordinate
  310. int mX;
  311. //! Y coordinate
  312. int mY;
  313. //! Zero vector
  314. static const IntVector2 sZero;
  315. };
  316. //! Multiply IntVector2 with a scalar
  317. inline IntVector2 operator * (int lhs, const IntVector2& rhs) { return rhs * lhs; }
  318. #endif // MATH_VECTOR2_H