Vector2.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //
  2. // Copyright (c) 2008-2015 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. 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. /// Return absolute vector.
  143. Vector2 Abs() const { return Vector2(Atomic::Abs(x_), Atomic::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 Atomic::Equals(x_, rhs.x_) && Atomic::Equals(y_, rhs.y_); }
  148. /// Return whether is NaN.
  149. bool IsNaN() const { return Atomic::IsNaN(x_) || Atomic::IsNaN(y_); }
  150. /// Return normalized to unit length.
  151. Vector2 Normalized() const
  152. {
  153. float lenSquared = LengthSquared();
  154. if (!Atomic::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 ATOMIC_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. /// Assign from another vector.
  214. IntVector2& operator =(const IntVector2& rhs)
  215. {
  216. x_ = rhs.x_;
  217. y_ = rhs.y_;
  218. return *this;
  219. }
  220. /// Test for equality with another vector.
  221. bool operator ==(const IntVector2& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_; }
  222. /// Test for inequality with another vector.
  223. bool operator !=(const IntVector2& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_; }
  224. /// Add a vector.
  225. IntVector2 operator +(const IntVector2& rhs) const { return IntVector2(x_ + rhs.x_, y_ + rhs.y_); }
  226. /// Return negation.
  227. IntVector2 operator -() const { return IntVector2(-x_, -y_); }
  228. /// Subtract a vector.
  229. IntVector2 operator -(const IntVector2& rhs) const { return IntVector2(x_ - rhs.x_, y_ - rhs.y_); }
  230. /// Multiply with a scalar.
  231. IntVector2 operator *(int rhs) const { return IntVector2(x_ * rhs, y_ * rhs); }
  232. /// Divide by a scalar.
  233. IntVector2 operator /(int rhs) const { return IntVector2(x_ / rhs, y_ / rhs); }
  234. /// Add-assign a vector.
  235. IntVector2& operator +=(const IntVector2& rhs)
  236. {
  237. x_ += rhs.x_;
  238. y_ += rhs.y_;
  239. return *this;
  240. }
  241. /// Subtract-assign a vector.
  242. IntVector2& operator -=(const IntVector2& rhs)
  243. {
  244. x_ -= rhs.x_;
  245. y_ -= rhs.y_;
  246. return *this;
  247. }
  248. /// Multiply-assign a scalar.
  249. IntVector2& operator *=(int rhs)
  250. {
  251. x_ *= rhs;
  252. y_ *= rhs;
  253. return *this;
  254. }
  255. /// Divide-assign a scalar.
  256. IntVector2& operator /=(int rhs)
  257. {
  258. x_ /= rhs;
  259. y_ /= rhs;
  260. return *this;
  261. }
  262. /// Return integer data.
  263. const int* Data() const { return &x_; }
  264. /// Return as string.
  265. String ToString() const;
  266. /// X coordinate.
  267. int x_;
  268. /// Y coordinate.
  269. int y_;
  270. /// Zero vector.
  271. static const IntVector2 ZERO;
  272. };
  273. /// Multiply IntVector2 with a scalar.
  274. inline IntVector2 operator *(int lhs, const IntVector2& rhs) { return rhs * lhs; }
  275. }