Vector2.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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 "Str.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 without epsilon.
  60. bool operator == (const Vector2& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_; }
  61. /// Test for inequality with another vector without epsilon.
  62. bool operator != (const Vector2& rhs) const { return x_ != rhs.x_ || 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. /// Return length.
  133. float Length() const { return sqrtf(x_ * x_ + y_ * y_); }
  134. /// Return squared length.
  135. float LengthSquared() const { return x_ * x_ + y_ * y_; }
  136. /// Calculate dot product.
  137. float DotProduct(const Vector2& rhs) const { return x_ * rhs.x_ + y_ * rhs.y_; }
  138. /// Calculate absolute dot product.
  139. float AbsDotProduct(const Vector2& rhs) const { return fabsf(x_ * rhs.x_) + fabsf(y_ * rhs.y_); }
  140. /// Return absolute vector.
  141. Vector2 Abs() const { return Vector2(fabsf(x_), fabsf(y_)); }
  142. /// Linear interpolation with another vector.
  143. Vector2 Lerp(const Vector2& rhs, float t) const { return *this * (1.0f - t) + rhs * t; }
  144. ///// Test for equality with another vectir with epsilon.
  145. bool Equals(const Vector2& rhs) const { return ::Equals(x_, rhs.x_) && ::Equals(y_, rhs.y_); }
  146. /// Return normalized to unit length.
  147. Vector2 Normalized() const
  148. {
  149. float len = Length();
  150. if (len >= M_EPSILON)
  151. return *this * (1.0f / len);
  152. else
  153. return *this;
  154. }
  155. /// Return float data.
  156. const float* Data() const { return &x_; }
  157. /// Return as string.
  158. String ToString() const;
  159. /// X coordinate.
  160. float x_;
  161. /// Y coordinate.
  162. float y_;
  163. /// Zero vector.
  164. static const Vector2 ZERO;
  165. /// (-1,0) vector.
  166. static const Vector2 LEFT;
  167. /// (1,0) vector.
  168. static const Vector2 RIGHT;
  169. /// (0,1) vector.
  170. static const Vector2 UP;
  171. /// (0,-1) vector.
  172. static const Vector2 DOWN;
  173. /// (1,1) vector.
  174. static const Vector2 ONE;
  175. };
  176. /// Multiply Vector2 with a scalar
  177. inline Vector2 operator * (float lhs, const Vector2& rhs) { return rhs * lhs; }
  178. /// Two-dimensional vector with integer values.
  179. class IntVector2
  180. {
  181. public:
  182. /// Construct undefined.
  183. IntVector2()
  184. {
  185. }
  186. /// Construct from coordinates.
  187. IntVector2(int x, int y) :
  188. x_(x),
  189. y_(y)
  190. {
  191. }
  192. /// Copy-construct from another vector.
  193. IntVector2(const IntVector2& rhs) :
  194. x_(rhs.x_),
  195. y_(rhs.y_)
  196. {
  197. }
  198. /// Test for equality with another vector.
  199. bool operator == (const IntVector2& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_; }
  200. /// Test for inequality with another vector.
  201. bool operator != (const IntVector2& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_; }
  202. /// Add a vector.
  203. IntVector2 operator + (const IntVector2& rhs) const { return IntVector2(x_ + rhs.x_, y_ + rhs.y_); }
  204. /// Return negation.
  205. IntVector2 operator - () const { return IntVector2(-x_, -y_); }
  206. /// Subtract a vector.
  207. IntVector2 operator - (const IntVector2& rhs) const { return IntVector2(x_ - rhs.x_, y_ - rhs.y_); }
  208. /// Multiply with a scalar.
  209. IntVector2 operator * (int rhs) const { return IntVector2(x_ * rhs, y_ * rhs); }
  210. /// Divide by a scalar.
  211. IntVector2 operator / (int rhs) const { return IntVector2(x_ / rhs, y_ / rhs); }
  212. /// Add-assign a vector.
  213. IntVector2& operator += (const IntVector2& rhs)
  214. {
  215. x_ += rhs.x_;
  216. y_ += rhs.y_;
  217. return *this;
  218. }
  219. /// Subtract-assign a vector.
  220. IntVector2& operator -= (const IntVector2& rhs)
  221. {
  222. x_ -= rhs.x_;
  223. y_ -= rhs.y_;
  224. return *this;
  225. }
  226. /// Multiply-assign a scalar.
  227. IntVector2& operator *= (int rhs)
  228. {
  229. x_ *= rhs;
  230. y_ *= rhs;
  231. return *this;
  232. }
  233. /// Divide-assign a scalar.
  234. IntVector2& operator /= (int rhs)
  235. {
  236. x_ /= rhs;
  237. y_ /= rhs;
  238. return *this;
  239. }
  240. /// Return integer data.
  241. const int* Data() const { return &x_; }
  242. /// Return as string.
  243. String ToString() const;
  244. /// X coordinate.
  245. int x_;
  246. /// Y coordinate.
  247. int y_;
  248. /// Zero vector.
  249. static const IntVector2 ZERO;
  250. };
  251. /// Multiply IntVector2 with a scalar.
  252. inline IntVector2 operator * (int lhs, const IntVector2& rhs) { return rhs * lhs; }