Vector2.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Vector2.h
  3. */
  4. #ifndef VECTOR2_H_
  5. #define VECTOR2_H_
  6. namespace gameplay
  7. {
  8. class Matrix;
  9. /**
  10. * Defines a 2-element floating point vector.
  11. */
  12. class Vector2
  13. {
  14. public:
  15. /**
  16. * The x coordinate.
  17. */
  18. float x;
  19. /**
  20. * The y coordinate.
  21. */
  22. float y;
  23. /**
  24. * Constructs a new vector initialized to all zeros.
  25. */
  26. Vector2();
  27. /**
  28. * Constructs a new vector initialized to the specified values.
  29. *
  30. * @param x The x coordinate.
  31. * @param y The y coordinate.
  32. */
  33. Vector2(float x, float y);
  34. /**
  35. * Constructs a new vector from the values in the specified array.
  36. *
  37. * @param array An array containing the elements of the vector in the order x, y.
  38. */
  39. Vector2(float* array);
  40. /**
  41. * Constructs a vector that describes the direction between the specified points.
  42. *
  43. * @param p1 The first point.
  44. * @param p2 The second point.
  45. */
  46. Vector2(const Vector2& p1, const Vector2& p2);
  47. /**
  48. * Constructs a new vector that is a copy of the specified vector.
  49. *
  50. * @param copy The vector to copy.
  51. */
  52. Vector2(const Vector2& copy);
  53. /**
  54. * Destructor.
  55. */
  56. ~Vector2();
  57. /**
  58. * Returns the zero vector.
  59. *
  60. * @return The 2-element vector of 0s.
  61. */
  62. static const Vector2& zero();
  63. /**
  64. * Returns the one vector.
  65. *
  66. * @return The 2-element vector of 1s.
  67. */
  68. static const Vector2& one();
  69. /**
  70. * Returns the unit x vector.
  71. *
  72. * @return The 2-element unit vector along the x axis.
  73. */
  74. static const Vector2& unitX();
  75. /**
  76. * Returns the unit y vector.
  77. *
  78. * @return The 2-element unit vector along the y axis.
  79. */
  80. static const Vector2& unitY();
  81. /**
  82. * Indicates whether this vector contains all zeros.
  83. *
  84. * @return true if this vector contains all zeros, false otherwise.
  85. */
  86. bool isZero() const;
  87. /**
  88. * Indicates whether this vector contains all ones.
  89. *
  90. * @return true if this vector contains all ones, false otherwise.
  91. */
  92. bool isOne() const;
  93. /**
  94. * Returns the angle (in radians) between the specified vectors.
  95. *
  96. * @param v1 The first vector.
  97. * @param v2 The second vector.
  98. *
  99. * @return The angle between the two vectors (in radians).
  100. */
  101. static float angle(const Vector2& v1, const Vector2& v2);
  102. /**
  103. * Adds the elements of the specified vector to this one.
  104. *
  105. * @param v The vector to add.
  106. */
  107. void add(const Vector2& v);
  108. /**
  109. * Adds the specified vectors and stores the result in dst.
  110. *
  111. * @param v1 The first vector.
  112. * @param v2 The second vector.
  113. * @param dst A vector to store the result in.
  114. */
  115. static void add(const Vector2& v1, const Vector2& v2, Vector2* dst);
  116. /**
  117. * Clamps this vector within the specified range.
  118. *
  119. * @param min The minimum value.
  120. * @param max The maximum value.
  121. */
  122. void clamp(const Vector2& min, const Vector2& max);
  123. /**
  124. * Clamps the specified vector within the specified range and returns it in dst.
  125. *
  126. * @param v The vector to clamp.
  127. * @param min The minimum value.
  128. * @param max The maximum value.
  129. * @param dst A vector to store the result in.
  130. */
  131. static void clamp(const Vector2& v, const Vector2& min, const Vector2& max, Vector2* dst);
  132. /**
  133. * Returns the distance between this vector and v.
  134. *
  135. * @param v The other vector.
  136. *
  137. * @return The distance between this vector and v.
  138. *
  139. * @see distanceSquared
  140. */
  141. float distance(const Vector2& v) const;
  142. /**
  143. * Returns the squared distance between this vector and v.
  144. *
  145. * When it is not neccessary to get the exact distance between
  146. * two vectors (for example, when simply comparing the
  147. * distance between different vectors), it is advised to use
  148. * this method instead of distance.
  149. *
  150. * @param v The other vector.
  151. *
  152. * @return The squared distance between this vector and v.
  153. *
  154. * @see distance
  155. */
  156. float distanceSquared(const Vector2& v) const;
  157. /**
  158. * Returns the dot product of this vector and the specified vector.
  159. *
  160. * @param v The vector to compute the dot product with.
  161. *
  162. * @return The dot product.
  163. */
  164. float dot(const Vector2& v);
  165. /**
  166. * Returns the dot product between the specified vectors.
  167. *
  168. * @param v1 The first vector.
  169. * @param v2 The second vector.
  170. *
  171. * @return The dot product between the vectors.
  172. */
  173. static float dot(const Vector2& v1, const Vector2& v2);
  174. /**
  175. * Computes the length of this vector.
  176. *
  177. * @return The length of the vector.
  178. *
  179. * @see lengthSquared
  180. */
  181. float length() const;
  182. /**
  183. * Returns the squared length of this vector.
  184. *
  185. * When it is not neccessary to get the exact length of a
  186. * vector (for example, when simply comparing the lengths of
  187. * different vectors), it is advised to use this method
  188. * instead of length.
  189. *
  190. * @return The squared length of the vector.
  191. *
  192. * @see length
  193. */
  194. float lengthSquared() const;
  195. /**
  196. * Negates this vector.
  197. */
  198. void negate();
  199. /**
  200. * Normalizes this vector.
  201. *
  202. * This method normalizes this Vector2 so that it is of
  203. * unit length (in other words, the length of the vector
  204. * after calling this method will be 1.0f). If the vector
  205. * already has unit length or if the length of the vector
  206. * is zero, this method does nothing.
  207. */
  208. void normalize();
  209. /**
  210. * Normalizes this vector and stores the result in dst.
  211. *
  212. * If the vector already has unit length or if the length
  213. * of the vector is zero, this method simply copies the
  214. * current vector into dst.
  215. *
  216. * @param dst The destination vector.
  217. */
  218. void normalize(Vector2* dst);
  219. /**
  220. * Scales all elements of this vector by the specified value.
  221. *
  222. * @param scalar The scalar value.
  223. */
  224. void scale(float scalar);
  225. /**
  226. * Scales each element of this vector by the matching component of scale.
  227. *
  228. * @param scale The vector to scale by.
  229. */
  230. void scale(const Vector2& scale);
  231. /**
  232. * Rotates this vector by angle (specified in radians) around the given point.
  233. *
  234. * @param point The point to rotate around.
  235. * @param angle The angle to rotate by (in radians).
  236. */
  237. void rotate(const Vector2& point, float angle);
  238. /**
  239. * Sets the elements of this vector to the specified values.
  240. *
  241. * @param x The new x coordinate.
  242. * @param y The new y coordinate.
  243. */
  244. void set(float x, float y);
  245. /**
  246. * Sets the elements of this vector from the values in the specified array.
  247. *
  248. * @param array An array containing the elements of the vector in the order x, y.
  249. */
  250. void set(float* array);
  251. /**
  252. * Sets the elements of this vector to those in the specified vector.
  253. *
  254. * @param v The vector to copy.
  255. */
  256. void set(const Vector2& v);
  257. /**
  258. * Sets this vector to the directional vector between the specified points.
  259. *
  260. * @param p1 The first point.
  261. * @param p2 The second point.
  262. */
  263. void set(const Vector2& p1, const Vector2& p2);
  264. /**
  265. * Subtracts this vector and the specified vector as (this - v)
  266. * and stores the result in this vector.
  267. *
  268. * @param v The vector to subtract.
  269. */
  270. void subtract(const Vector2& v);
  271. /**
  272. * Subtracts the specified vectors and stores the result in dst.
  273. * The resulting vector is computed as (v1 - v2).
  274. *
  275. * @param v1 The first vector.
  276. * @param v2 The second vector.
  277. * @param dst The destination vector.
  278. */
  279. static void subtract(const Vector2& v1, const Vector2& v2, Vector2* dst);
  280. };
  281. }
  282. #endif