Vector2.h 8.7 KB

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