Vector2.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. //
  2. // Copyright (c) 2008-2016 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 "../Container/Str.h"
  24. #include "../Math/MathDefs.h"
  25. namespace Atomic
  26. {
  27. /// Two-dimensional vector.
  28. class ATOMIC_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. explicit 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 (!Atomic::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 Atomic::Abs(x_ * rhs.x_) + Atomic::Abs(y_ * rhs.y_); }
  142. /// Returns the angle between this vector and another vector in degrees.
  143. float Angle(const Vector2& rhs) const { return Atomic::Acos(DotProduct(rhs) / (Length() * rhs.Length())); }
  144. /// Return absolute vector.
  145. Vector2 Abs() const { return Vector2(Atomic::Abs(x_), Atomic::Abs(y_)); }
  146. /// Linear interpolation with another vector.
  147. Vector2 Lerp(const Vector2& rhs, float t) const { return *this * (1.0f - t) + rhs * t; }
  148. /// Test for equality with another vector with epsilon.
  149. bool Equals(const Vector2& rhs) const { return Atomic::Equals(x_, rhs.x_) && Atomic::Equals(y_, rhs.y_); }
  150. /// Return whether is NaN.
  151. bool IsNaN() const { return Atomic::IsNaN(x_) || Atomic::IsNaN(y_); }
  152. /// Return normalized to unit length.
  153. Vector2 Normalized() const
  154. {
  155. float lenSquared = LengthSquared();
  156. if (!Atomic::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  157. {
  158. float invLen = 1.0f / sqrtf(lenSquared);
  159. return *this * invLen;
  160. }
  161. else
  162. return *this;
  163. }
  164. /// Return float data.
  165. const float* Data() const { return &x_; }
  166. /// Return as string.
  167. String ToString() const;
  168. /// X coordinate.
  169. float x_;
  170. /// Y coordinate.
  171. float y_;
  172. /// Zero vector.
  173. static const Vector2 ZERO;
  174. /// (-1,0) vector.
  175. static const Vector2 LEFT;
  176. /// (1,0) vector.
  177. static const Vector2 RIGHT;
  178. /// (0,1) vector.
  179. static const Vector2 UP;
  180. /// (0,-1) vector.
  181. static const Vector2 DOWN;
  182. /// (1,1) vector.
  183. static const Vector2 ONE;
  184. };
  185. /// Multiply Vector2 with a scalar
  186. inline Vector2 operator *(float lhs, const Vector2& rhs) { return rhs * lhs; }
  187. /// Two-dimensional vector with integer values.
  188. class ATOMIC_API IntVector2
  189. {
  190. public:
  191. /// Construct a zero vector.
  192. IntVector2() :
  193. x_(0),
  194. y_(0)
  195. {
  196. }
  197. /// Construct from coordinates.
  198. IntVector2(int x, int y) :
  199. x_(x),
  200. y_(y)
  201. {
  202. }
  203. /// Construct from an int array.
  204. IntVector2(const int* data) :
  205. x_(data[0]),
  206. y_(data[1])
  207. {
  208. }
  209. /// Copy-construct from another vector.
  210. IntVector2(const IntVector2& rhs) :
  211. x_(rhs.x_),
  212. y_(rhs.y_)
  213. {
  214. }
  215. /// Assign from another vector.
  216. IntVector2& operator =(const IntVector2& rhs)
  217. {
  218. x_ = rhs.x_;
  219. y_ = rhs.y_;
  220. return *this;
  221. }
  222. /// Test for equality with another vector.
  223. bool operator ==(const IntVector2& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_; }
  224. /// Test for inequality with another vector.
  225. bool operator !=(const IntVector2& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_; }
  226. /// Add a vector.
  227. IntVector2 operator +(const IntVector2& rhs) const { return IntVector2(x_ + rhs.x_, y_ + rhs.y_); }
  228. /// Return negation.
  229. IntVector2 operator -() const { return IntVector2(-x_, -y_); }
  230. /// Subtract a vector.
  231. IntVector2 operator -(const IntVector2& rhs) const { return IntVector2(x_ - rhs.x_, y_ - rhs.y_); }
  232. /// Multiply with a scalar.
  233. IntVector2 operator *(int rhs) const { return IntVector2(x_ * rhs, y_ * rhs); }
  234. /// Divide by a scalar.
  235. IntVector2 operator /(int rhs) const { return IntVector2(x_ / rhs, y_ / rhs); }
  236. /// Add-assign a vector.
  237. IntVector2& operator +=(const IntVector2& rhs)
  238. {
  239. x_ += rhs.x_;
  240. y_ += rhs.y_;
  241. return *this;
  242. }
  243. /// Subtract-assign a vector.
  244. IntVector2& operator -=(const IntVector2& rhs)
  245. {
  246. x_ -= rhs.x_;
  247. y_ -= rhs.y_;
  248. return *this;
  249. }
  250. /// Multiply-assign a scalar.
  251. IntVector2& operator *=(int rhs)
  252. {
  253. x_ *= rhs;
  254. y_ *= rhs;
  255. return *this;
  256. }
  257. /// Divide-assign a scalar.
  258. IntVector2& operator /=(int rhs)
  259. {
  260. x_ /= rhs;
  261. y_ /= rhs;
  262. return *this;
  263. }
  264. /// Return integer data.
  265. const int* Data() const { return &x_; }
  266. /// Return as string.
  267. String ToString() const;
  268. /// Return hash value for HashSet & HashMap.
  269. unsigned ToHash() const { return (unsigned)x_ * 31 + (unsigned)y_; }
  270. /// X coordinate.
  271. int x_;
  272. /// Y coordinate.
  273. int y_;
  274. /// Zero vector.
  275. static const IntVector2 ZERO;
  276. };
  277. /// Multiply IntVector2 with a scalar.
  278. inline IntVector2 operator *(int lhs, const IntVector2& rhs) { return rhs * lhs; }
  279. }