Vector2.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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(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);
  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. void normalize();
  206. /**
  207. * Normalizes this vector and stores the result in dst.
  208. *
  209. * If the vector already has unit length or if the length
  210. * of the vector is zero, this method simply copies the
  211. * current vector into dst.
  212. *
  213. * @param dst The destination vector.
  214. */
  215. void normalize(Vector2* dst);
  216. /**
  217. * Scales all elements of this vector by the specified value.
  218. *
  219. * @param scalar The scalar value.
  220. */
  221. void scale(float scalar);
  222. /**
  223. * Scales each element of this vector by the matching component of scale.
  224. *
  225. * @param scale The vector to scale by.
  226. */
  227. void scale(const Vector2& scale);
  228. /**
  229. * Rotates this vector by angle (specified in radians) around the given point.
  230. *
  231. * @param point The point to rotate around.
  232. * @param angle The angle to rotate by (in radians).
  233. */
  234. void rotate(const Vector2& point, float angle);
  235. /**
  236. * Sets the elements of this vector to the specified values.
  237. *
  238. * @param x The new x coordinate.
  239. * @param y The new y coordinate.
  240. */
  241. void set(float x, float y);
  242. /**
  243. * Sets the elements of this vector from the values in the specified array.
  244. *
  245. * @param array An array containing the elements of the vector in the order x, y.
  246. */
  247. void set(float* array);
  248. /**
  249. * Sets the elements of this vector to those in the specified vector.
  250. *
  251. * @param v The vector to copy.
  252. */
  253. void set(const Vector2& v);
  254. /**
  255. * Sets this vector to the directional vector between the specified points.
  256. *
  257. * @param p1 The first point.
  258. * @param p2 The second point.
  259. */
  260. void set(const Vector2& p1, const Vector2& p2);
  261. /**
  262. * Subtracts this vector and the specified vector as (this - v)
  263. * and stores the result in this vector.
  264. *
  265. * @param v The vector to subtract.
  266. */
  267. void subtract(const Vector2& v);
  268. /**
  269. * Subtracts the specified vectors and stores the result in dst.
  270. * The resulting vector is computed as (v1 - v2).
  271. *
  272. * @param v1 The first vector.
  273. * @param v2 The second vector.
  274. * @param dst The destination vector.
  275. */
  276. static void subtract(const Vector2& v1, const Vector2& v2, Vector2* dst);
  277. /**
  278. * Calculates the sum of this vector with the given vector.
  279. *
  280. * Note: this does not modify this vector.
  281. *
  282. * @param v The vector to add.
  283. * @return The vector sum.
  284. */
  285. inline Vector2 operator+(const Vector2& v);
  286. /**
  287. * Adds the given vector to this vector.
  288. *
  289. * @param v The vector to add.
  290. * @return This vector, after the addition occurs.
  291. */
  292. inline Vector2& operator+=(const Vector2& v);
  293. /**
  294. * Calculates the sum of this vector with the given vector.
  295. *
  296. * Note: this does not modify this vector.
  297. *
  298. * @param v The vector to add.
  299. * @return The vector sum.
  300. */
  301. inline Vector2 operator-(const Vector2& v);
  302. /**
  303. * Subtracts the given vector from this vector.
  304. *
  305. * @param v The vector to subtract.
  306. * @return This vector, after the subtraction occurs.
  307. */
  308. inline Vector2& operator-=(const Vector2& v);
  309. /**
  310. * Calculates the negation of this vector.
  311. *
  312. * Note: this does not modify this vector.
  313. *
  314. * @return The negation of this vector.
  315. */
  316. inline Vector2 operator-();
  317. /**
  318. * Calculates the scalar product of this vector with the given value.
  319. *
  320. * Note: this does not modify this vector.
  321. *
  322. * @param x The value to scale by.
  323. * @return The scaled vector.
  324. */
  325. inline Vector2 operator*(float x);
  326. /**
  327. * Scales this vector by the given value.
  328. *
  329. * @param x The value to scale by.
  330. * @return This vector, after the scale occurs.
  331. */
  332. inline Vector2& operator*=(float x);
  333. /**
  334. * Determines if this vector is less than the given vector.
  335. *
  336. * @param v The vector to compare against.
  337. *
  338. * @return True if this vector is less than the given vector, false otherwise.
  339. */
  340. inline bool operator<(const Vector2& v) const;
  341. /**
  342. * Determines if this vector is equal to the given vector.
  343. *
  344. * @param v The vector to compare against.
  345. *
  346. * @return True if this vector is equal to the given vector, false otherwise.
  347. */
  348. inline bool operator==(const Vector2& v) const;
  349. };
  350. /**
  351. * Calculates the scalar product of the given vector with the given value.
  352. *
  353. * @param x The value to scale by.
  354. * @param v The vector to scale.
  355. * @return The scaled vector.
  356. */
  357. inline Vector2 operator*(float x, const Vector2& v);
  358. }
  359. #include "Vector2.inl"
  360. #endif