Vector2.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. //
  2. // Copyright (c) 2008-2017 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 Urho3D
  26. {
  27. /// Two-dimensional vector with integer values.
  28. class URHO3D_API IntVector2
  29. {
  30. public:
  31. /// Construct a zero vector.
  32. IntVector2() :
  33. x_(0),
  34. y_(0)
  35. {
  36. }
  37. /// Construct from coordinates.
  38. IntVector2(int x, int y) :
  39. x_(x),
  40. y_(y)
  41. {
  42. }
  43. /// Construct from an int array.
  44. IntVector2(const int* data) :
  45. x_(data[0]),
  46. y_(data[1])
  47. {
  48. }
  49. /// Copy-construct from another vector.
  50. IntVector2(const IntVector2& rhs) :
  51. x_(rhs.x_),
  52. y_(rhs.y_)
  53. {
  54. }
  55. /// Assign from another vector.
  56. IntVector2& operator =(const IntVector2& rhs)
  57. {
  58. x_ = rhs.x_;
  59. y_ = rhs.y_;
  60. return *this;
  61. }
  62. /// Test for equality with another vector.
  63. bool operator ==(const IntVector2& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_; }
  64. /// Test for inequality with another vector.
  65. bool operator !=(const IntVector2& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_; }
  66. /// Add a vector.
  67. IntVector2 operator +(const IntVector2& rhs) const { return IntVector2(x_ + rhs.x_, y_ + rhs.y_); }
  68. /// Return negation.
  69. IntVector2 operator -() const { return IntVector2(-x_, -y_); }
  70. /// Subtract a vector.
  71. IntVector2 operator -(const IntVector2& rhs) const { return IntVector2(x_ - rhs.x_, y_ - rhs.y_); }
  72. /// Multiply with a scalar.
  73. IntVector2 operator *(int rhs) const { return IntVector2(x_ * rhs, y_ * rhs); }
  74. /// Multiply with a vector.
  75. IntVector2 operator *(const IntVector2& rhs) const { return IntVector2(x_ * rhs.x_, y_ * rhs.y_); }
  76. /// Divide by a scalar.
  77. IntVector2 operator /(int rhs) const { return IntVector2(x_ / rhs, y_ / rhs); }
  78. /// Divide by a vector.
  79. IntVector2 operator /(const IntVector2& rhs) const { return IntVector2(x_ / rhs.x_, y_ / rhs.y_); }
  80. /// Add-assign a vector.
  81. IntVector2& operator +=(const IntVector2& rhs)
  82. {
  83. x_ += rhs.x_;
  84. y_ += rhs.y_;
  85. return *this;
  86. }
  87. /// Subtract-assign a vector.
  88. IntVector2& operator -=(const IntVector2& rhs)
  89. {
  90. x_ -= rhs.x_;
  91. y_ -= rhs.y_;
  92. return *this;
  93. }
  94. /// Multiply-assign a scalar.
  95. IntVector2& operator *=(int rhs)
  96. {
  97. x_ *= rhs;
  98. y_ *= rhs;
  99. return *this;
  100. }
  101. /// Multiply-assign a vector.
  102. IntVector2& operator *=(const IntVector2& rhs)
  103. {
  104. x_ *= rhs.x_;
  105. y_ *= rhs.y_;
  106. return *this;
  107. }
  108. /// Divide-assign a scalar.
  109. IntVector2& operator /=(int rhs)
  110. {
  111. x_ /= rhs;
  112. y_ /= rhs;
  113. return *this;
  114. }
  115. /// Divide-assign a vector.
  116. IntVector2& operator /=(const IntVector2& rhs)
  117. {
  118. x_ /= rhs.x_;
  119. y_ /= rhs.y_;
  120. return *this;
  121. }
  122. /// Return integer data.
  123. const int* Data() const { return &x_; }
  124. /// Return as string.
  125. String ToString() const;
  126. /// Return hash value for HashSet & HashMap.
  127. unsigned ToHash() const { return (unsigned)x_ * 31 + (unsigned)y_; }
  128. /// Return length.
  129. float Length() const { return sqrtf((float)(x_ * x_ + y_ * y_)); }
  130. /// X coordinate.
  131. int x_;
  132. /// Y coordinate.
  133. int y_;
  134. /// Zero vector.
  135. static const IntVector2 ZERO;
  136. /// (-1,0) vector.
  137. static const IntVector2 LEFT;
  138. /// (1,0) vector.
  139. static const IntVector2 RIGHT;
  140. /// (0,1) vector.
  141. static const IntVector2 UP;
  142. /// (0,-1) vector.
  143. static const IntVector2 DOWN;
  144. /// (1,1) vector.
  145. static const IntVector2 ONE;
  146. };
  147. /// Two-dimensional vector.
  148. class URHO3D_API Vector2
  149. {
  150. public:
  151. /// Construct a zero vector.
  152. Vector2() :
  153. x_(0.0f),
  154. y_(0.0f)
  155. {
  156. }
  157. /// Copy-construct from another vector.
  158. Vector2(const Vector2& vector) :
  159. x_(vector.x_),
  160. y_(vector.y_)
  161. {
  162. }
  163. /// Construct from an IntVector2.
  164. explicit Vector2(const IntVector2& vector) :
  165. x_((float)vector.x_),
  166. y_((float)vector.y_)
  167. {
  168. }
  169. /// Construct from coordinates.
  170. Vector2(float x, float y) :
  171. x_(x),
  172. y_(y)
  173. {
  174. }
  175. /// Construct from a float array.
  176. explicit Vector2(const float* data) :
  177. x_(data[0]),
  178. y_(data[1])
  179. {
  180. }
  181. /// Assign from another vector.
  182. Vector2& operator =(const Vector2& rhs)
  183. {
  184. x_ = rhs.x_;
  185. y_ = rhs.y_;
  186. return *this;
  187. }
  188. /// Test for equality with another vector without epsilon.
  189. bool operator ==(const Vector2& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_; }
  190. /// Test for inequality with another vector without epsilon.
  191. bool operator !=(const Vector2& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_; }
  192. /// Add a vector.
  193. Vector2 operator +(const Vector2& rhs) const { return Vector2(x_ + rhs.x_, y_ + rhs.y_); }
  194. /// Return negation.
  195. Vector2 operator -() const { return Vector2(-x_, -y_); }
  196. /// Subtract a vector.
  197. Vector2 operator -(const Vector2& rhs) const { return Vector2(x_ - rhs.x_, y_ - rhs.y_); }
  198. /// Multiply with a scalar.
  199. Vector2 operator *(float rhs) const { return Vector2(x_ * rhs, y_ * rhs); }
  200. /// Multiply with a vector.
  201. Vector2 operator *(const Vector2& rhs) const { return Vector2(x_ * rhs.x_, y_ * rhs.y_); }
  202. /// Divide by a scalar.
  203. Vector2 operator /(float rhs) const { return Vector2(x_ / rhs, y_ / rhs); }
  204. /// Divide by a vector.
  205. Vector2 operator /(const Vector2& rhs) const { return Vector2(x_ / rhs.x_, y_ / rhs.y_); }
  206. /// Add-assign a vector.
  207. Vector2& operator +=(const Vector2& rhs)
  208. {
  209. x_ += rhs.x_;
  210. y_ += rhs.y_;
  211. return *this;
  212. }
  213. /// Subtract-assign a vector.
  214. Vector2& operator -=(const Vector2& rhs)
  215. {
  216. x_ -= rhs.x_;
  217. y_ -= rhs.y_;
  218. return *this;
  219. }
  220. /// Multiply-assign a scalar.
  221. Vector2& operator *=(float rhs)
  222. {
  223. x_ *= rhs;
  224. y_ *= rhs;
  225. return *this;
  226. }
  227. /// Multiply-assign a vector.
  228. Vector2& operator *=(const Vector2& rhs)
  229. {
  230. x_ *= rhs.x_;
  231. y_ *= rhs.y_;
  232. return *this;
  233. }
  234. /// Divide-assign a scalar.
  235. Vector2& operator /=(float rhs)
  236. {
  237. float invRhs = 1.0f / rhs;
  238. x_ *= invRhs;
  239. y_ *= invRhs;
  240. return *this;
  241. }
  242. /// Divide-assign a vector.
  243. Vector2& operator /=(const Vector2& rhs)
  244. {
  245. x_ /= rhs.x_;
  246. y_ /= rhs.y_;
  247. return *this;
  248. }
  249. /// Normalize to unit length.
  250. void Normalize()
  251. {
  252. float lenSquared = LengthSquared();
  253. if (!Urho3D::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  254. {
  255. float invLen = 1.0f / sqrtf(lenSquared);
  256. x_ *= invLen;
  257. y_ *= invLen;
  258. }
  259. }
  260. /// Return length.
  261. float Length() const { return sqrtf(x_ * x_ + y_ * y_); }
  262. /// Return squared length.
  263. float LengthSquared() const { return x_ * x_ + y_ * y_; }
  264. /// Calculate dot product.
  265. float DotProduct(const Vector2& rhs) const { return x_ * rhs.x_ + y_ * rhs.y_; }
  266. /// Calculate absolute dot product.
  267. float AbsDotProduct(const Vector2& rhs) const { return Urho3D::Abs(x_ * rhs.x_) + Urho3D::Abs(y_ * rhs.y_); }
  268. /// Project vector onto axis.
  269. float ProjectOntoAxis(const Vector2& axis) const { return DotProduct(axis.Normalized()); }
  270. /// Returns the angle between this vector and another vector in degrees.
  271. float Angle(const Vector2& rhs) const { return Urho3D::Acos(DotProduct(rhs) / (Length() * rhs.Length())); }
  272. /// Return absolute vector.
  273. Vector2 Abs() const { return Vector2(Urho3D::Abs(x_), Urho3D::Abs(y_)); }
  274. /// Linear interpolation with another vector.
  275. Vector2 Lerp(const Vector2& rhs, float t) const { return *this * (1.0f - t) + rhs * t; }
  276. /// Test for equality with another vector with epsilon.
  277. bool Equals(const Vector2& rhs) const { return Urho3D::Equals(x_, rhs.x_) && Urho3D::Equals(y_, rhs.y_); }
  278. /// Return whether is NaN.
  279. bool IsNaN() const { return Urho3D::IsNaN(x_) || Urho3D::IsNaN(y_); }
  280. /// Return normalized to unit length.
  281. Vector2 Normalized() const
  282. {
  283. float lenSquared = LengthSquared();
  284. if (!Urho3D::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  285. {
  286. float invLen = 1.0f / sqrtf(lenSquared);
  287. return *this * invLen;
  288. }
  289. else
  290. return *this;
  291. }
  292. /// Return float data.
  293. const float* Data() const { return &x_; }
  294. /// Return as string.
  295. String ToString() const;
  296. /// X coordinate.
  297. float x_;
  298. /// Y coordinate.
  299. float y_;
  300. /// Zero vector.
  301. static const Vector2 ZERO;
  302. /// (-1,0) vector.
  303. static const Vector2 LEFT;
  304. /// (1,0) vector.
  305. static const Vector2 RIGHT;
  306. /// (0,1) vector.
  307. static const Vector2 UP;
  308. /// (0,-1) vector.
  309. static const Vector2 DOWN;
  310. /// (1,1) vector.
  311. static const Vector2 ONE;
  312. };
  313. /// Multiply Vector2 with a scalar
  314. inline Vector2 operator *(float lhs, const Vector2& rhs) { return rhs * lhs; }
  315. /// Multiply IntVector2 with a scalar.
  316. inline IntVector2 operator *(int lhs, const IntVector2& rhs) { return rhs * lhs; }
  317. /// Per-component linear interpolation between two 2-vectors.
  318. inline Vector2 VectorLerp(const Vector2& lhs, const Vector2& rhs, const Vector2& t) { return lhs + (rhs - lhs) * t; }
  319. /// Per-component min of two 2-vectors.
  320. inline Vector2 VectorMin(const Vector2& lhs, const Vector2& rhs) { return Vector2(Min(lhs.x_, rhs.x_), Min(lhs.y_, rhs.y_)); }
  321. /// Per-component max of two 2-vectors.
  322. inline Vector2 VectorMax(const Vector2& lhs, const Vector2& rhs) { return Vector2(Max(lhs.x_, rhs.x_), Max(lhs.y_, rhs.y_)); }
  323. /// Per-component floor of 2-vector.
  324. inline Vector2 VectorFloor(const Vector2& vec) { return Vector2(Floor(vec.x_), Floor(vec.y_)); }
  325. /// Per-component round of 2-vector.
  326. inline Vector2 VectorRound(const Vector2& vec) { return Vector2(Round(vec.x_), Round(vec.y_)); }
  327. /// Per-component ceil of 2-vector.
  328. inline Vector2 VectorCeil(const Vector2& vec) { return Vector2(Ceil(vec.x_), Ceil(vec.y_)); }
  329. /// Per-component floor of 2-vector. Returns IntVector2.
  330. inline IntVector2 VectorFloorToInt(const Vector2& vec) { return IntVector2(FloorToInt(vec.x_), FloorToInt(vec.y_)); }
  331. /// Per-component round of 2-vector. Returns IntVector2.
  332. inline IntVector2 VectorRoundToInt(const Vector2& vec) { return IntVector2(RoundToInt(vec.x_), RoundToInt(vec.y_)); }
  333. /// Per-component ceil of 2-vector. Returns IntVector2.
  334. inline IntVector2 VectorCeilToInt(const Vector2& vec) { return IntVector2(CeilToInt(vec.x_), CeilToInt(vec.y_)); }
  335. /// Per-component min of two 2-vectors.
  336. inline IntVector2 VectorMin(const IntVector2& lhs, const IntVector2& rhs) { return IntVector2(Min(lhs.x_, rhs.x_), Min(lhs.y_, rhs.y_)); }
  337. /// Per-component max of two 2-vectors.
  338. inline IntVector2 VectorMax(const IntVector2& lhs, const IntVector2& rhs) { return IntVector2(Max(lhs.x_, rhs.x_), Max(lhs.y_, rhs.y_)); }
  339. /// Return a random value from [0, 1) from 2-vector seed.
  340. /// http://stackoverflow.com/questions/12964279/whats-the-origin-of-this-glsl-rand-one-liner
  341. inline float StableRandom(const Vector2& seed) { return Fract(Sin(seed.DotProduct(Vector2(12.9898f, 78.233f)) * M_RADTODEG) * 43758.5453f); }
  342. /// Return a random value from [0, 1) from scalar seed.
  343. inline float StableRandom(float seed) { return StableRandom(Vector2(seed, seed)); }
  344. }