Vector2.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #ifndef VECTOR2_H_
  2. #define VECTOR2_H_
  3. namespace gameplay
  4. {
  5. class Matrix;
  6. /**
  7. * Defines a 2-element floating point vector.
  8. */
  9. class Vector2
  10. {
  11. public:
  12. /**
  13. * The x coordinate.
  14. */
  15. float x;
  16. /**
  17. * The y coordinate.
  18. */
  19. float y;
  20. /**
  21. * Constructs a new vector initialized to all zeros.
  22. */
  23. Vector2();
  24. /**
  25. * Constructs a new vector initialized to the specified values.
  26. *
  27. * @param x The x coordinate.
  28. * @param y The y coordinate.
  29. */
  30. Vector2(float x, float y);
  31. /**
  32. * Constructs a new vector from the values in the specified array.
  33. *
  34. * @param array An array containing the elements of the vector in the order x, y.
  35. */
  36. Vector2(const float* array);
  37. /**
  38. * Constructs a vector that describes the direction between the specified points.
  39. *
  40. * @param p1 The first point.
  41. * @param p2 The second point.
  42. */
  43. Vector2(const Vector2& p1, const Vector2& p2);
  44. /**
  45. * Constructs a new vector that is a copy of the specified vector.
  46. *
  47. * @param copy The vector to copy.
  48. */
  49. Vector2(const Vector2& copy);
  50. /**
  51. * Destructor.
  52. */
  53. ~Vector2();
  54. /**
  55. * Returns the zero vector.
  56. *
  57. * @return The 2-element vector of 0s.
  58. */
  59. static const Vector2& zero();
  60. /**
  61. * Returns the one vector.
  62. *
  63. * @return The 2-element vector of 1s.
  64. */
  65. static const Vector2& one();
  66. /**
  67. * Returns the unit x vector.
  68. *
  69. * @return The 2-element unit vector along the x axis.
  70. */
  71. static const Vector2& unitX();
  72. /**
  73. * Returns the unit y vector.
  74. *
  75. * @return The 2-element unit vector along the y axis.
  76. */
  77. static const Vector2& unitY();
  78. /**
  79. * Indicates whether this vector contains all zeros.
  80. *
  81. * @return true if this vector contains all zeros, false otherwise.
  82. */
  83. bool isZero() const;
  84. /**
  85. * Indicates whether this vector contains all ones.
  86. *
  87. * @return true if this vector contains all ones, false otherwise.
  88. */
  89. bool isOne() const;
  90. /**
  91. * Returns the angle (in radians) between the specified vectors.
  92. *
  93. * @param v1 The first vector.
  94. * @param v2 The second vector.
  95. *
  96. * @return The angle between the two vectors (in radians).
  97. */
  98. static float angle(const Vector2& v1, const Vector2& v2);
  99. /**
  100. * Adds the elements of the specified vector to this one.
  101. *
  102. * @param v The vector to add.
  103. */
  104. void add(const Vector2& v);
  105. /**
  106. * Adds the specified vectors and stores the result in dst.
  107. *
  108. * @param v1 The first vector.
  109. * @param v2 The second vector.
  110. * @param dst A vector to store the result in.
  111. */
  112. static void add(const Vector2& v1, const Vector2& v2, Vector2* dst);
  113. /**
  114. * Clamps this vector within the specified range.
  115. *
  116. * @param min The minimum value.
  117. * @param max The maximum value.
  118. */
  119. void clamp(const Vector2& min, const Vector2& max);
  120. /**
  121. * Clamps the specified vector within the specified range and returns it in dst.
  122. *
  123. * @param v The vector to clamp.
  124. * @param min The minimum value.
  125. * @param max The maximum value.
  126. * @param dst A vector to store the result in.
  127. */
  128. static void clamp(const Vector2& v, const Vector2& min, const Vector2& max, Vector2* dst);
  129. /**
  130. * Returns the distance between this vector and v.
  131. *
  132. * @param v The other vector.
  133. *
  134. * @return The distance between this vector and v.
  135. *
  136. * @see distanceSquared
  137. */
  138. float distance(const Vector2& v) const;
  139. /**
  140. * Returns the squared distance between this vector and v.
  141. *
  142. * When it is not necessary to get the exact distance between
  143. * two vectors (for example, when simply comparing the
  144. * distance between different vectors), it is advised to use
  145. * this method instead of distance.
  146. *
  147. * @param v The other vector.
  148. *
  149. * @return The squared distance between this vector and v.
  150. *
  151. * @see distance
  152. */
  153. float distanceSquared(const Vector2& v) const;
  154. /**
  155. * Returns the dot product of this vector and the specified vector.
  156. *
  157. * @param v The vector to compute the dot product with.
  158. *
  159. * @return The dot product.
  160. */
  161. float dot(const Vector2& v) const;
  162. /**
  163. * Returns the dot product between the specified vectors.
  164. *
  165. * @param v1 The first vector.
  166. * @param v2 The second vector.
  167. *
  168. * @return The dot product between the vectors.
  169. */
  170. static float dot(const Vector2& v1, const Vector2& v2);
  171. /**
  172. * Computes the length of this vector.
  173. *
  174. * @return The length of the vector.
  175. *
  176. * @see lengthSquared
  177. */
  178. float length() const;
  179. /**
  180. * Returns the squared length of this vector.
  181. *
  182. * When it is not necessary to get the exact length of a
  183. * vector (for example, when simply comparing the lengths of
  184. * different vectors), it is advised to use this method
  185. * instead of length.
  186. *
  187. * @return The squared length of the vector.
  188. *
  189. * @see length
  190. */
  191. float lengthSquared() const;
  192. /**
  193. * Negates this vector.
  194. */
  195. void negate();
  196. /**
  197. * Normalizes this vector.
  198. *
  199. * This method normalizes this Vector2 so that it is of
  200. * unit length (in other words, the length of the vector
  201. * after calling this method will be 1.0f). If the vector
  202. * already has unit length or if the length of the vector
  203. * is zero, this method does nothing.
  204. *
  205. * @return This vector, after the normalization occurs.
  206. */
  207. Vector2& normalize();
  208. /**
  209. * Normalizes this vector and stores the result in dst.
  210. *
  211. * If the vector already has unit length or if the length
  212. * of the vector is zero, this method simply copies the
  213. * current vector into dst.
  214. *
  215. * @param dst The destination vector.
  216. */
  217. void normalize(Vector2* dst) const;
  218. /**
  219. * Scales all elements of this vector by the specified value.
  220. *
  221. * @param scalar The scalar value.
  222. */
  223. void scale(float scalar);
  224. /**
  225. * Scales each element of this vector by the matching component of scale.
  226. *
  227. * @param scale The vector to scale by.
  228. */
  229. void scale(const Vector2& scale);
  230. /**
  231. * Rotates this vector by angle (specified in radians) around the given point.
  232. *
  233. * @param point The point to rotate around.
  234. * @param angle The angle to rotate by (in radians).
  235. */
  236. void rotate(const Vector2& point, float angle);
  237. /**
  238. * Sets the elements of this vector to the specified values.
  239. *
  240. * @param x The new x coordinate.
  241. * @param y The new y coordinate.
  242. */
  243. void set(float x, float y);
  244. /**
  245. * Sets the elements of this vector from the values in the specified array.
  246. *
  247. * @param array An array containing the elements of the vector in the order x, y.
  248. */
  249. void set(const float* array);
  250. /**
  251. * Sets the elements of this vector to those in the specified vector.
  252. *
  253. * @param v The vector to copy.
  254. */
  255. void set(const Vector2& v);
  256. /**
  257. * Sets this vector to the directional vector between the specified points.
  258. *
  259. * @param p1 The first point.
  260. * @param p2 The second point.
  261. */
  262. void set(const Vector2& p1, const Vector2& p2);
  263. /**
  264. * Subtracts this vector and the specified vector as (this - v)
  265. * and stores the result in this vector.
  266. *
  267. * @param v The vector to subtract.
  268. */
  269. void subtract(const Vector2& v);
  270. /**
  271. * Subtracts the specified vectors and stores the result in dst.
  272. * The resulting vector is computed as (v1 - v2).
  273. *
  274. * @param v1 The first vector.
  275. * @param v2 The second vector.
  276. * @param dst The destination vector.
  277. */
  278. static void subtract(const Vector2& v1, const Vector2& v2, Vector2* dst);
  279. /**
  280. * Updates this vector towards the given target using a smoothing function.
  281. * The given response time determines the amount of smoothing (lag). A longer
  282. * response time yields a smoother result and more lag. To force this vector to
  283. * follow the target closely, provide a response time that is very small relative
  284. * to the given elapsed time.
  285. *
  286. * @param target target value.
  287. * @param elapsedTime elapsed time between calls.
  288. * @param responseTime response time (in the same units as elapsedTime).
  289. */
  290. void smooth(const Vector2& target, float elapsedTime, float responseTime);
  291. /**
  292. * Calculates the sum of this vector with the given vector.
  293. *
  294. * Note: this does not modify this vector.
  295. *
  296. * @param v The vector to add.
  297. * @return The vector sum.
  298. */
  299. inline const Vector2 operator+(const Vector2& v) const;
  300. /**
  301. * Adds the given vector to this vector.
  302. *
  303. * @param v The vector to add.
  304. * @return This vector, after the addition occurs.
  305. */
  306. inline Vector2& operator+=(const Vector2& v);
  307. /**
  308. * Calculates the sum of this vector with the given vector.
  309. *
  310. * Note: this does not modify this vector.
  311. *
  312. * @param v The vector to add.
  313. * @return The vector sum.
  314. */
  315. inline const Vector2 operator-(const Vector2& v) const;
  316. /**
  317. * Subtracts the given vector from this vector.
  318. *
  319. * @param v The vector to subtract.
  320. * @return This vector, after the subtraction occurs.
  321. */
  322. inline Vector2& operator-=(const Vector2& v);
  323. /**
  324. * Calculates the negation of this vector.
  325. *
  326. * Note: this does not modify this vector.
  327. *
  328. * @return The negation of this vector.
  329. */
  330. inline const Vector2 operator-() const;
  331. /**
  332. * Calculates the scalar product of this vector with the given value.
  333. *
  334. * Note: this does not modify this vector.
  335. *
  336. * @param x The value to scale by.
  337. * @return The scaled vector.
  338. */
  339. inline const Vector2 operator*(float x) const;
  340. /**
  341. * Scales this vector by the given value.
  342. *
  343. * @param x The value to scale by.
  344. * @return This vector, after the scale occurs.
  345. */
  346. inline Vector2& operator*=(float x);
  347. /**
  348. * Determines if this vector is less than the given vector.
  349. *
  350. * @param v The vector to compare against.
  351. *
  352. * @return True if this vector is less than the given vector, false otherwise.
  353. */
  354. inline bool operator<(const Vector2& v) const;
  355. /**
  356. * Determines if this vector is equal to the given vector.
  357. *
  358. * @param v The vector to compare against.
  359. *
  360. * @return True if this vector is equal to the given vector, false otherwise.
  361. */
  362. inline bool operator==(const Vector2& v) const;
  363. /**
  364. * Determines if this vector is not equal to the given vector.
  365. *
  366. * @param v The vector to compare against.
  367. *
  368. * @return True if this vector is not equal to the given vector, false otherwise.
  369. */
  370. inline bool operator!=(const Vector2& v) const;
  371. };
  372. /**
  373. * Calculates the scalar product of the given vector with the given value.
  374. *
  375. * @param x The value to scale by.
  376. * @param v The vector to scale.
  377. * @return The scaled vector.
  378. */
  379. inline const Vector2 operator*(float x, const Vector2& v);
  380. }
  381. #include "Vector2.inl"
  382. #endif