Vector2.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "MathDefs.h"
  24. #include "Str.h"
  25. namespace Urho3D
  26. {
  27. /// Two-dimensional vector.
  28. class URHO3D_API Vector2
  29. {
  30. public:
  31. /// Construct a zero vector.
  32. Vector2() :
  33. x_(0.0f),
  34. y_(0.0f)
  35. {
  36. }
  37. /// Copy-construct from another vector.
  38. Vector2(const Vector2& vector) :
  39. x_(vector.x_),
  40. y_(vector.y_)
  41. {
  42. }
  43. /// Construct from coordinates.
  44. Vector2(float x, float y) :
  45. x_(x),
  46. y_(y)
  47. {
  48. }
  49. /// Construct from a float array.
  50. Vector2(const float* data) :
  51. x_(data[0]),
  52. y_(data[1])
  53. {
  54. }
  55. /// Assign from another vector.
  56. Vector2& operator = (const Vector2& rhs)
  57. {
  58. x_ = rhs.x_;
  59. y_ = rhs.y_;
  60. return *this;
  61. }
  62. /// Test for equality with another vector without epsilon.
  63. bool operator == (const Vector2& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_; }
  64. /// Test for inequality with another vector without epsilon.
  65. bool operator != (const Vector2& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_; }
  66. /// Add a vector.
  67. Vector2 operator + (const Vector2& rhs) const { return Vector2(x_ + rhs.x_, y_ + rhs.y_); }
  68. /// Return negation.
  69. Vector2 operator - () const { return Vector2(-x_, -y_); }
  70. /// Subtract a vector.
  71. Vector2 operator - (const Vector2& rhs) const { return Vector2(x_ - rhs.x_, y_ - rhs.y_); }
  72. /// Multiply with a scalar.
  73. Vector2 operator * (float rhs) const { return Vector2(x_ * rhs, y_ * rhs); }
  74. /// Multiply with a vector.
  75. Vector2 operator * (const Vector2& rhs) const { return Vector2(x_ * rhs.x_, y_ * rhs.y_); }
  76. /// Divide by a scalar.
  77. Vector2 operator / (float rhs) const { return Vector2(x_ / rhs, y_ / rhs); }
  78. /// Divide by a vector.
  79. Vector2 operator / (const Vector2& rhs) const { return Vector2(x_ / rhs.x_, y_ / rhs.y_); }
  80. /// Add-assign a vector.
  81. Vector2& operator += (const Vector2& rhs)
  82. {
  83. x_ += rhs.x_;
  84. y_ += rhs.y_;
  85. return *this;
  86. }
  87. /// Subtract-assign a vector.
  88. Vector2& operator -= (const Vector2& rhs)
  89. {
  90. x_ -= rhs.x_;
  91. y_ -= rhs.y_;
  92. return *this;
  93. }
  94. /// Multiply-assign a scalar.
  95. Vector2& operator *= (float rhs)
  96. {
  97. x_ *= rhs;
  98. y_ *= rhs;
  99. return *this;
  100. }
  101. /// Multiply-assign a vector.
  102. Vector2& operator *= (const Vector2& rhs)
  103. {
  104. x_ *= rhs.x_;
  105. y_ *= rhs.y_;
  106. return *this;
  107. }
  108. /// Divide-assign a scalar.
  109. Vector2& operator /= (float rhs)
  110. {
  111. float invRhs = 1.0f / rhs;
  112. x_ *= invRhs;
  113. y_ *= invRhs;
  114. return *this;
  115. }
  116. /// Divide-assign a vector.
  117. Vector2& operator /= (const Vector2& rhs)
  118. {
  119. x_ /= rhs.x_;
  120. y_ /= rhs.y_;
  121. return *this;
  122. }
  123. /// Normalize to unit length.
  124. void Normalize()
  125. {
  126. float lenSquared = LengthSquared();
  127. if (!Urho3D::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  128. {
  129. float invLen = 1.0f / sqrtf(lenSquared);
  130. x_ *= invLen;
  131. y_ *= invLen;
  132. }
  133. }
  134. /// Return length.
  135. float Length() const { return sqrtf(x_ * x_ + y_ * y_); }
  136. /// Return squared length.
  137. float LengthSquared() const { return x_ * x_ + y_ * y_; }
  138. /// Calculate dot product.
  139. float DotProduct(const Vector2& rhs) const { return x_ * rhs.x_ + y_ * rhs.y_; }
  140. /// Calculate absolute dot product.
  141. float AbsDotProduct(const Vector2& rhs) const { return Urho3D::Abs(x_ * rhs.x_) + Urho3D::Abs(y_ * rhs.y_); }
  142. /// Return absolute vector.
  143. Vector2 Abs() const { return Vector2(Urho3D::Abs(x_), Urho3D::Abs(y_)); }
  144. /// Linear interpolation with another vector.
  145. Vector2 Lerp(const Vector2& rhs, float t) const { return *this * (1.0f - t) + rhs * t; }
  146. /// Test for equality with another vector with epsilon.
  147. bool Equals(const Vector2& rhs) const { return Urho3D::Equals(x_, rhs.x_) && Urho3D::Equals(y_, rhs.y_); }
  148. /// Return whether is NaN.
  149. bool IsNaN() const { return Urho3D::IsNaN(x_) || Urho3D::IsNaN(y_); }
  150. /// Return normalized to unit length.
  151. Vector2 Normalized() const
  152. {
  153. float lenSquared = LengthSquared();
  154. if (!Urho3D::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  155. {
  156. float invLen = 1.0f / sqrtf(lenSquared);
  157. return *this * invLen;
  158. }
  159. else
  160. return *this;
  161. }
  162. /// Return float data.
  163. const float* Data() const { return &x_; }
  164. /// Return as string.
  165. String ToString() const;
  166. /// X coordinate.
  167. float x_;
  168. /// Y coordinate.
  169. float y_;
  170. /// Zero vector.
  171. static const Vector2 ZERO;
  172. /// (-1,0) vector.
  173. static const Vector2 LEFT;
  174. /// (1,0) vector.
  175. static const Vector2 RIGHT;
  176. /// (0,1) vector.
  177. static const Vector2 UP;
  178. /// (0,-1) vector.
  179. static const Vector2 DOWN;
  180. /// (1,1) vector.
  181. static const Vector2 ONE;
  182. };
  183. /// Multiply Vector2 with a scalar
  184. inline Vector2 operator * (float lhs, const Vector2& rhs) { return rhs * lhs; }
  185. /// Two-dimensional vector with integer values.
  186. class URHO3D_API IntVector2
  187. {
  188. public:
  189. /// Construct a zero vector.
  190. IntVector2() :
  191. x_(0),
  192. y_(0)
  193. {
  194. }
  195. /// Construct from coordinates.
  196. IntVector2(int x, int y) :
  197. x_(x),
  198. y_(y)
  199. {
  200. }
  201. /// Construct from an int array.
  202. IntVector2(const int* data) :
  203. x_(data[0]),
  204. y_(data[1])
  205. {
  206. }
  207. /// Copy-construct from another vector.
  208. IntVector2(const IntVector2& rhs) :
  209. x_(rhs.x_),
  210. y_(rhs.y_)
  211. {
  212. }
  213. /// Test for equality with another vector.
  214. bool operator == (const IntVector2& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_; }
  215. /// Test for inequality with another vector.
  216. bool operator != (const IntVector2& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_; }
  217. /// Add a vector.
  218. IntVector2 operator + (const IntVector2& rhs) const { return IntVector2(x_ + rhs.x_, y_ + rhs.y_); }
  219. /// Return negation.
  220. IntVector2 operator - () const { return IntVector2(-x_, -y_); }
  221. /// Subtract a vector.
  222. IntVector2 operator - (const IntVector2& rhs) const { return IntVector2(x_ - rhs.x_, y_ - rhs.y_); }
  223. /// Multiply with a scalar.
  224. IntVector2 operator * (int rhs) const { return IntVector2(x_ * rhs, y_ * rhs); }
  225. /// Divide by a scalar.
  226. IntVector2 operator / (int rhs) const { return IntVector2(x_ / rhs, y_ / rhs); }
  227. /// Add-assign a vector.
  228. IntVector2& operator += (const IntVector2& rhs)
  229. {
  230. x_ += rhs.x_;
  231. y_ += rhs.y_;
  232. return *this;
  233. }
  234. /// Subtract-assign a vector.
  235. IntVector2& operator -= (const IntVector2& rhs)
  236. {
  237. x_ -= rhs.x_;
  238. y_ -= rhs.y_;
  239. return *this;
  240. }
  241. /// Multiply-assign a scalar.
  242. IntVector2& operator *= (int rhs)
  243. {
  244. x_ *= rhs;
  245. y_ *= rhs;
  246. return *this;
  247. }
  248. /// Divide-assign a scalar.
  249. IntVector2& operator /= (int rhs)
  250. {
  251. x_ /= rhs;
  252. y_ /= rhs;
  253. return *this;
  254. }
  255. /// Return integer data.
  256. const int* Data() const { return &x_; }
  257. /// Return as string.
  258. String ToString() const;
  259. /// X coordinate.
  260. int x_;
  261. /// Y coordinate.
  262. int y_;
  263. /// Zero vector.
  264. static const IntVector2 ZERO;
  265. };
  266. /// Multiply IntVector2 with a scalar.
  267. inline IntVector2 operator * (int lhs, const IntVector2& rhs) { return rhs * lhs; }
  268. }