Vector2.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 an undefined vector
  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. x_ /= rhs;
  109. y_ /= rhs;
  110. return *this;
  111. }
  112. /// Divide-assign a vector
  113. Vector2& operator /= (const Vector2& rhs)
  114. {
  115. x_ /= rhs.x_;
  116. y_ /= rhs.y_;
  117. return *this;
  118. }
  119. /// Normalize to unit length and return the previous length
  120. float Normalize()
  121. {
  122. float len = Length();
  123. if (len < M_EPSILON)
  124. return len;
  125. float invLen = 1.0f / len;
  126. x_ *= invLen;
  127. y_ *= invLen;
  128. return len;
  129. }
  130. /// Normalize to unit length using fast inverse square root
  131. void NormalizeFast()
  132. {
  133. float invLen = FastInvSqrt(x_ * x_ + y_ * y_);
  134. x_ *= invLen;
  135. y_ *= invLen;
  136. }
  137. /// Return length
  138. float Length() const { return sqrtf(x_ * x_ + y_ * y_); }
  139. /// Return length using fast square root
  140. float LengthFast() const { return FastSqrt(x_ * x_ + y_ * y_); }
  141. /// Return squared length
  142. float LengthSquared() const { return x_ * x_ + y_ * y_; }
  143. /// Calculate dot product
  144. float DotProduct(const Vector2& rhs) const { return x_ * rhs.x_ + y_ * rhs.y_; }
  145. /// Calculate absolute dot product
  146. float AbsDotProduct(const Vector2& rhs) const { return fabsf(x_ * rhs.x_) + fabsf(y_ * rhs.y_); }
  147. /// Return absolute vector
  148. Vector2 Abs() const { return Vector2(fabsf(x_), fabsf(y_)); }
  149. /// Linear interpolation with another vector
  150. Vector2 Lerp(const Vector2& rhs, float t) const { return *this * (1.0f - t) + rhs * t; }
  151. /// Return normalized to unit length
  152. Vector2 Normalized() const
  153. {
  154. float len = Length();
  155. if (len < M_EPSILON)
  156. return *this;
  157. float invLen = 1.0f / len;
  158. return *this * invLen;
  159. }
  160. /// Return normalized to unit length using fast inverse square root
  161. Vector2 NormalizedFast() const
  162. {
  163. float invLen = FastInvSqrt(x_ * x_ + y_ * y_);
  164. return *this * invLen;
  165. }
  166. /// Return float data
  167. const float* GetData() const { return &x_; }
  168. /// Return as string
  169. String ToString() const;
  170. /// X coordinate
  171. float x_;
  172. /// Y coordinate
  173. float y_;
  174. /// Zero vector
  175. static const Vector2 ZERO;
  176. /// (-1,0) vector
  177. static const Vector2 LEFT;
  178. /// (1,0) vector
  179. static const Vector2 RIGHT;
  180. /// (0,1) vector
  181. static const Vector2 UP;
  182. /// (0,-1) vector
  183. static const Vector2 DOWN;
  184. /// (1,1) vector
  185. static const Vector2 UNITY;
  186. };
  187. /// Multiply Vector2 with a scalar
  188. inline Vector2 operator * (float lhs, const Vector2& rhs) { return rhs * lhs; }
  189. /// Two-dimensional vector with integer values
  190. class IntVector2
  191. {
  192. public:
  193. /// Construct an undefined vector
  194. IntVector2()
  195. {
  196. }
  197. /// Construct from coordinates
  198. IntVector2(int x, int y) :
  199. x_(x),
  200. y_(y)
  201. {
  202. }
  203. /// Copy-construct from another vector
  204. IntVector2(const IntVector2& rhs) :
  205. x_(rhs.x_),
  206. y_(rhs.y_)
  207. {
  208. }
  209. /// Test for equality with another vector
  210. bool operator == (const IntVector2& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_; }
  211. /// Test for inequality with another vector
  212. bool operator != (const IntVector2& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_; }
  213. /// Add a vector
  214. IntVector2 operator + (const IntVector2& rhs) const { return IntVector2(x_ + rhs.x_, y_ + rhs.y_); }
  215. /// Return negation
  216. IntVector2 operator - () const { return IntVector2(-x_, -y_); }
  217. /// Subtract a vector
  218. IntVector2 operator - (const IntVector2& rhs) const { return IntVector2(x_ - rhs.x_, y_ - rhs.y_); }
  219. /// Multiply with a scalar
  220. IntVector2 operator * (int rhs) const { return IntVector2(x_ * rhs, y_ * rhs); }
  221. /// Divide by a scalar
  222. IntVector2 operator / (int rhs) const { return IntVector2(x_ / rhs, y_ / rhs); }
  223. /// Add-assign a vector
  224. IntVector2& operator += (const IntVector2& rhs)
  225. {
  226. x_ += rhs.x_;
  227. y_ += rhs.y_;
  228. return *this;
  229. }
  230. /// Subtract-assign a vector
  231. IntVector2& operator -= (const IntVector2& rhs)
  232. {
  233. x_ -= rhs.x_;
  234. y_ -= rhs.y_;
  235. return *this;
  236. }
  237. /// Multiply-assign a scalar
  238. IntVector2& operator *= (int rhs)
  239. {
  240. x_ *= rhs;
  241. y_ *= rhs;
  242. return *this;
  243. }
  244. /// Divide-assign a scalar
  245. IntVector2& operator /= (int rhs)
  246. {
  247. x_ /= rhs;
  248. y_ /= rhs;
  249. return *this;
  250. }
  251. /// Return integer data
  252. const int* GetData() const { return &x_; }
  253. /// Return as string
  254. String ToString() const;
  255. /// X coordinate
  256. int x_;
  257. /// Y coordinate
  258. int y_;
  259. /// Zero vector
  260. static const IntVector2 ZERO;
  261. };
  262. /// Multiply IntVector2 with a scalar
  263. inline IntVector2 operator * (int lhs, const IntVector2& rhs) { return rhs * lhs; }