Vector2.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. #pragma once
  24. #include "MathDefs.h"
  25. /// Two-dimensional vector
  26. class Vector2
  27. {
  28. public:
  29. /// Construct an undefined vector
  30. Vector2()
  31. {
  32. }
  33. /// Copy-construct from another vector
  34. Vector2(const Vector2& vector) :
  35. x_(vector.x_),
  36. y_(vector.y_)
  37. {
  38. }
  39. /// Construct from coordinates
  40. Vector2(float x, float y) :
  41. x_(x),
  42. y_(y)
  43. {
  44. }
  45. /// Construct from a float array
  46. Vector2(const float* data) :
  47. x_(data[0]),
  48. y_(data[1])
  49. {
  50. }
  51. /// Assign from another vector
  52. Vector2& operator = (const Vector2& rhs)
  53. {
  54. x_ = rhs.x_;
  55. y_ = rhs.y_;
  56. return *this;
  57. }
  58. /// Test for equality with another vector
  59. bool operator == (const Vector2& rhs) const { return Equals(x_, rhs.x_) && Equals(y_, rhs.y_); }
  60. /// Test for inequality with another vector
  61. bool operator != (const Vector2& rhs) const { return (!Equals(x_, rhs.x_)) || (!Equals(y_, rhs.y_)); }
  62. /// Add a vector
  63. Vector2 operator + (const Vector2& rhs) const { return Vector2(x_ + rhs.x_, y_ + rhs.y_); }
  64. /// Return negation
  65. Vector2 operator - () const { return Vector2(-x_, -y_); }
  66. /// Subtract a vector
  67. Vector2 operator - (const Vector2& rhs) const { return Vector2(x_ - rhs.x_, y_ - rhs.y_); }
  68. /// Multiply with a scalar
  69. Vector2 operator * (float rhs) const { return Vector2(x_ * rhs, y_ * rhs); }
  70. /// Multiply with a vector
  71. Vector2 operator * (const Vector2& rhs) const { return Vector2(x_ * rhs.x_, y_ * rhs.y_); }
  72. /// Divide by a scalar
  73. Vector2 operator / (float rhs) const { return Vector2(x_ / rhs, y_ / rhs); }
  74. /// Divide by a vector
  75. Vector2 operator / (const Vector2& rhs) const { return Vector2(x_ / rhs.x_, y_ / rhs.y_); }
  76. /// Add-assign a vector
  77. Vector2& operator += (const Vector2& rhs)
  78. {
  79. x_ += rhs.x_;
  80. y_ += rhs.y_;
  81. return *this;
  82. }
  83. /// Subtract-assign a vector
  84. Vector2& operator -= (const Vector2& rhs)
  85. {
  86. x_ -= rhs.x_;
  87. y_ -= rhs.y_;
  88. return *this;
  89. }
  90. /// Multiply-assign a scalar
  91. Vector2& operator *= (float rhs)
  92. {
  93. x_ *= rhs;
  94. y_ *= rhs;
  95. return *this;
  96. }
  97. /// Multiply-assign a vector
  98. Vector2& operator *= (const Vector2& rhs)
  99. {
  100. x_ *= rhs.x_;
  101. y_ *= rhs.y_;
  102. return *this;
  103. }
  104. /// Divide-assign a scalar
  105. Vector2& operator /= (float rhs)
  106. {
  107. x_ /= rhs;
  108. y_ /= rhs;
  109. return *this;
  110. }
  111. /// Divide-assign a vector
  112. Vector2& operator /= (const Vector2& rhs)
  113. {
  114. x_ /= rhs.x_;
  115. y_ /= rhs.y_;
  116. return *this;
  117. }
  118. /// Normalize to unit length and return the previous length
  119. float Normalize()
  120. {
  121. float len = GetLength();
  122. if (len < M_EPSILON)
  123. return len;
  124. float invLen = 1.0f / len;
  125. x_ *= invLen;
  126. y_ *= invLen;
  127. return len;
  128. }
  129. /// Normalize to unit length using fast inverse square root
  130. void NormalizeFast()
  131. {
  132. float invLen = FastInvSqrt(x_ * x_ + y_ * y_);
  133. x_ *= invLen;
  134. y_ *= invLen;
  135. }
  136. /// Return length
  137. float GetLength() const { return sqrtf(x_ * x_ + y_ * y_); }
  138. /// Return length using fast square root
  139. float GetLengthFast() const { return FastSqrt(x_ * x_ + y_ * y_); }
  140. /// Return squared length
  141. float GetLengthSquared() const { return x_ * x_ + y_ * y_; }
  142. /// Calculate dot product
  143. float DotProduct(const Vector2& rhs) const { return x_ * rhs.x_ + y_ * rhs.y_; }
  144. /// Calculate absolute dot product
  145. float AbsDotProduct(const Vector2& rhs) const { return fabsf(x_ * rhs.x_) + fabsf(y_ * rhs.y_); }
  146. /// Linear interpolation with another vector
  147. Vector2 Lerp(const Vector2& rhs, float t) const { return *this * (1.0f - t) + rhs * t; }
  148. /// Return normalized to unit length
  149. Vector2 GetNormalized() const
  150. {
  151. float len = GetLength();
  152. if (len < M_EPSILON)
  153. return *this;
  154. float invLen = 1.0f / len;
  155. return *this * invLen;
  156. }
  157. /// Return normalized to unit length using fast inverse square root
  158. Vector2 GetNormalizedFast() const
  159. {
  160. float invLen = FastInvSqrt(x_ * x_ + y_ * y_);
  161. return *this * invLen;
  162. }
  163. /// Return float data
  164. const float* GetData() const { return &x_; }
  165. /// X coordinate
  166. float x_;
  167. /// Y coordinate
  168. float y_;
  169. /// Zero vector
  170. static const Vector2 ZERO;
  171. /// (-1,0) vector
  172. static const Vector2 LEFT;
  173. /// (1,0) vector
  174. static const Vector2 RIGHT;
  175. /// (0,1) vector
  176. static const Vector2 UP;
  177. /// (0,-1) vector
  178. static const Vector2 DOWN;
  179. /// (1,1) vector
  180. static const Vector2 UNITY;
  181. };
  182. /// Multiply Vector2 with a scalar
  183. inline Vector2 operator * (float lhs, const Vector2& rhs) { return rhs * lhs; }
  184. /// Two-dimensional vector with integer values
  185. class IntVector2
  186. {
  187. public:
  188. /// Construct an undefined vector
  189. IntVector2()
  190. {
  191. }
  192. /// Construct from coordinates
  193. IntVector2(int x, int y) :
  194. x_(x),
  195. y_(y)
  196. {
  197. }
  198. /// Copy-construct from another vector
  199. IntVector2(const IntVector2& rhs) :
  200. x_(rhs.x_),
  201. y_(rhs.y_)
  202. {
  203. }
  204. /// Test for equality with another vector
  205. bool operator == (const IntVector2& rhs) const { return (x_ == rhs.x_) && (y_ == rhs.y_); }
  206. /// Test for inequality with another vector
  207. bool operator != (const IntVector2& rhs) const { return (x_ != rhs.x_) ||(y_ != rhs.y_); }
  208. /// Add a vector
  209. IntVector2 operator + (const IntVector2& rhs) const { return IntVector2(x_ + rhs.x_, y_ + rhs.y_); }
  210. /// Return negation
  211. IntVector2 operator - () const { return IntVector2(-x_, -y_); }
  212. /// Subtract a vector
  213. IntVector2 operator - (const IntVector2& rhs) const { return IntVector2(x_ - rhs.x_, y_ - rhs.y_); }
  214. /// Multiply with a scalar
  215. IntVector2 operator * (int rhs) const { return IntVector2(x_ * rhs, y_ * rhs); }
  216. /// Divide by a scalar
  217. IntVector2 operator / (int rhs) const { return IntVector2(x_ / rhs, y_ / rhs); }
  218. /// Add-assign a vector
  219. IntVector2& operator += (const IntVector2& rhs)
  220. {
  221. x_ += rhs.x_;
  222. y_ += rhs.y_;
  223. return *this;
  224. }
  225. /// Subtract-assign a vector
  226. IntVector2& operator -= (const IntVector2& rhs)
  227. {
  228. x_ -= rhs.x_;
  229. y_ -= rhs.y_;
  230. return *this;
  231. }
  232. /// Multiply-assign a scalar
  233. IntVector2& operator *= (int rhs)
  234. {
  235. x_ *= rhs;
  236. y_ *= rhs;
  237. return *this;
  238. }
  239. /// Divide-assign a scalar
  240. IntVector2& operator /= (int rhs)
  241. {
  242. x_ /= rhs;
  243. y_ /= rhs;
  244. return *this;
  245. }
  246. /// Return integer data
  247. const int* GetData() const { return &x_; }
  248. /// X coordinate
  249. int x_;
  250. /// Y coordinate
  251. int y_;
  252. /// Zero vector
  253. static const IntVector2 ZERO;
  254. };
  255. /// Multiply IntVector2 with a scalar
  256. inline IntVector2 operator * (int lhs, const IntVector2& rhs) { return rhs * lhs; }