Vector2.h 8.4 KB

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