Vector2.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. //! Return length
  158. float getLength() const
  159. {
  160. return sqrtf(mX * mX + mY * mY);
  161. }
  162. //! Return squared length
  163. float getLengthSquared() const
  164. {
  165. return mX * mX + mY * mY;
  166. }
  167. //! Calculate dot product
  168. float dotProduct(const Vector2& rhs) const
  169. {
  170. return mX * rhs.mX + mY * rhs.mY;
  171. }
  172. //! Calculate absolute dot product
  173. float absDotProduct(const Vector2& rhs) const
  174. {
  175. return fabsf(mX * rhs.mX) + fabsf(mY * rhs.mY);
  176. }
  177. //! Linear interpolation with another vector
  178. Vector2 lerp(const Vector2& rhs, float t) const
  179. {
  180. return *this * (1.0f - t) + rhs * t;
  181. }
  182. //! Return normalized to unit length
  183. Vector2 getNormalized() const
  184. {
  185. float len = getLength();
  186. if (len < M_EPSILON)
  187. return *this;
  188. float invLen = 1.0f / len;
  189. return *this * invLen;
  190. }
  191. //! Return float data
  192. const float* getData() const { return &mX; }
  193. //! X coordinate
  194. float mX;
  195. //! Y coordinate
  196. float mY;
  197. //! Zero vector
  198. static const Vector2 sZero;
  199. //! (-1,0) vector
  200. static const Vector2 sLeft;
  201. //! (1,0) vector
  202. static const Vector2 sRight;
  203. //! (0,1) vector
  204. static const Vector2 sUp;
  205. //! (0,-1) vector
  206. static const Vector2 sDown;
  207. //! (1,1) vector
  208. static const Vector2 sUnity;
  209. };
  210. //! Multiply Vector2 with a scalar
  211. inline Vector2 operator * (float lhs, const Vector2& rhs) { return rhs * lhs; }
  212. //! A two-dimensional vector with integer values
  213. class IntVector2
  214. {
  215. public:
  216. //! Construct an undefined vector
  217. IntVector2()
  218. {
  219. }
  220. //! Construct from coordinates
  221. IntVector2(int x, int y) :
  222. mX(x),
  223. mY(y)
  224. {
  225. }
  226. //! Test for equality with another vector
  227. bool operator == (const IntVector2& rhs) const
  228. {
  229. return (mX == rhs.mX) && (mY == rhs.mY);
  230. }
  231. //! Test for inequality with another vector
  232. bool operator != (const IntVector2& rhs) const
  233. {
  234. return (mX != rhs.mX) ||(mY != rhs.mY);
  235. }
  236. //! Add a vector
  237. IntVector2 operator + (const IntVector2& rhs) const
  238. {
  239. return IntVector2(mX + rhs.mX, mY + rhs.mY);
  240. }
  241. //! Return negation
  242. IntVector2 operator - () const
  243. {
  244. return IntVector2(-mX, -mY);
  245. }
  246. //! Subtract a vector
  247. IntVector2 operator - (const IntVector2& rhs) const
  248. {
  249. return IntVector2(mX - rhs.mX, mY - rhs.mY);
  250. }
  251. //! Multiply with a scalar
  252. IntVector2 operator * (int rhs) const
  253. {
  254. return IntVector2(mX * rhs, mY * rhs);
  255. }
  256. //! Divide by a scalar
  257. IntVector2 operator / (int rhs) const
  258. {
  259. return IntVector2(mX / rhs, mY / rhs);
  260. }
  261. //! Add-assign a vector
  262. IntVector2& operator += (const IntVector2& rhs)
  263. {
  264. mX += rhs.mX;
  265. mY += rhs.mY;
  266. return *this;
  267. }
  268. //! Subtract-assign a vector
  269. IntVector2& operator -= (const IntVector2& rhs)
  270. {
  271. mX -= rhs.mX;
  272. mY -= rhs.mY;
  273. return *this;
  274. }
  275. //! Multiply-assign a scalar
  276. IntVector2& operator *= (int rhs)
  277. {
  278. mX *= rhs;
  279. mY *= rhs;
  280. return *this;
  281. }
  282. //! Divide-assign a scalar
  283. IntVector2& operator /= (int rhs)
  284. {
  285. mX /= rhs;
  286. mY /= rhs;
  287. return *this;
  288. }
  289. //! Return integer data
  290. const int* getData() const { return &mX; }
  291. //! X coordinate
  292. int mX;
  293. //! Y coordinate
  294. int mY;
  295. //! Zero vector
  296. static const IntVector2 sZero;
  297. };
  298. //! Multiply IntVector2 with a scalar
  299. inline IntVector2 operator * (int lhs, const IntVector2& rhs) { return rhs * lhs; }
  300. #endif // MATH_VECTOR2_H