Vector2.h 8.2 KB

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