Vector.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /**
  2. * Copyright (c) 2006-2013 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #ifndef LOVE_VECTOR_H
  21. #define LOVE_VECTOR_H
  22. // STD
  23. #include <cmath>
  24. // LOVE
  25. #include "Matrix.h"
  26. namespace love
  27. {
  28. /**
  29. * 2D Vector class.
  30. *
  31. * @author Anders Ruud
  32. * @date 2006-05-13
  33. **/
  34. class Vector
  35. {
  36. public:
  37. // The components.
  38. float x, y;
  39. /**
  40. * Creates a new (1,1) Vector.
  41. **/
  42. Vector();
  43. /**
  44. * Creates a new Vector.
  45. * @param x The x position/dimension.
  46. * @param y The y position/dimension.
  47. **/
  48. Vector(float x, float y);
  49. /**
  50. * Gets the length of the Vector.
  51. * @return The length of the Vector.
  52. *
  53. * This method requires sqrtf() and should be used
  54. * carefully.
  55. **/
  56. float getLength() const;
  57. /**
  58. * Normalizes the Vector.
  59. * @param length Desired length of the vector.
  60. * @return The old length of the Vector.
  61. **/
  62. float normalize(float length = 1.0);
  63. /**
  64. * Gets a vector perpendicular to the Vector.
  65. * To get the true (normalized) normal, use v.getNormal(1.0f / v.getLength())
  66. * @return A normal to the Vector.
  67. **/
  68. Vector getNormal() const;
  69. /**
  70. * Gets a vector perpendicular to the Vector.
  71. * To get the true (normalized) normal, use v.getNormal(1.0f / v.getLength())
  72. * @param Scale factor to apply.
  73. * @return A normal to the Vector.
  74. **/
  75. Vector getNormal(float scale) const;
  76. /**
  77. * Adds a Vector to this Vector.
  78. * @param v The Vector we want to add to this Vector.
  79. * @return The resulting Vector.
  80. **/
  81. Vector operator + (const Vector &v) const;
  82. /**
  83. * Substracts a Vector to this Vector.
  84. * @param v The Vector we want to subtract to this Vector.
  85. * @return The resulting Vector.
  86. **/
  87. Vector operator - (const Vector &v) const;
  88. /**
  89. * Resizes a Vector by a scalar.
  90. * @param s The scalar with which to resize the Vector.
  91. * @return The resulting Vector.
  92. **/
  93. Vector operator * (float s) const;
  94. /**
  95. * Resizes a Vector by a scalar.
  96. * @param s The scalar with which to resize the Vector.
  97. * @return The resulting Vector.
  98. **/
  99. Vector operator / (float s) const;
  100. /**
  101. * Reverses the Vector.
  102. * @return The reversed Vector.
  103. **/
  104. Vector operator - () const;
  105. /**
  106. * Adds a Vector to this Vector, and also saves changes in the first Vector.
  107. * @param v The Vector we want to add to this Vector.
  108. **/
  109. void operator += (const Vector &v);
  110. /**
  111. * Subtracts a Vector to this Vector, and also saves changes in the first Vector.
  112. * @param v The Vector we want to subtract to this Vector.
  113. **/
  114. void operator -= (const Vector &v);
  115. /**
  116. * Resizes the Vector, and also saves changes in the first Vector.
  117. * @param s The scalar by which we want to resize the Vector.
  118. **/
  119. void operator *= (float s);
  120. /**
  121. * Resizes the Vector, and also saves changes in the first Vector.
  122. * @param s The scalar by which we want to resize the Vector.
  123. **/
  124. void operator /= (float s);
  125. /**
  126. * Calculates the dot product of two Vectors.
  127. * @return The dot product of the two Vectors.
  128. **/
  129. float operator * (const Vector &v) const;
  130. /**
  131. * Calculates the cross product of two Vectors.
  132. * @return The cross product of the two Vectors.
  133. **/
  134. float operator ^ (const Vector &v) const;
  135. bool operator == (const Vector &v) const;
  136. bool operator < (const Vector &v) const;
  137. /**
  138. * Gets the x value of the Vector.
  139. * @return The x value of the Vector.
  140. **/
  141. float getX() const;
  142. /**
  143. * Gets the x value of the Vector.
  144. * @return The x value of the Vector.
  145. **/
  146. float getY() const;
  147. /**
  148. * Sets the x value of the Vector.
  149. * @param The x value of the Vector.
  150. **/
  151. void setX(float x);
  152. /**
  153. * Sets the x value of the Vector.
  154. * @param The x value of the Vector.
  155. **/
  156. void setY(float y);
  157. };
  158. inline float Vector::getLength() const
  159. {
  160. return sqrtf(x*x + y*y);
  161. }
  162. inline Vector Vector::getNormal() const
  163. {
  164. return Vector(-y, x);
  165. }
  166. inline Vector Vector::getNormal(float scale) const
  167. {
  168. return Vector(-y * scale, x * scale);
  169. }
  170. inline float Vector::normalize(float length)
  171. {
  172. float length_current = getLength();
  173. if (length_current > 0)
  174. (*this) *= length / length_current;
  175. return length_current;
  176. }
  177. /**
  178. * Inline methods must have body in header.
  179. **/
  180. inline Vector::Vector()
  181. {
  182. x = 1;
  183. y = 1;
  184. }
  185. inline Vector::Vector(float x, float y)
  186. {
  187. this->x = x;
  188. this->y = y;
  189. }
  190. inline Vector Vector::operator + (const Vector &v) const
  191. {
  192. return Vector(x + v.x, y + v.y);
  193. }
  194. inline Vector Vector::operator - (const Vector &v) const
  195. {
  196. return Vector(x - v.getX(), y - v.getY());
  197. }
  198. inline Vector Vector::operator * (float s) const
  199. {
  200. return Vector(x*s, y*s);
  201. }
  202. inline Vector Vector::operator / (float s) const
  203. {
  204. return Vector(x/s, y/s);
  205. }
  206. inline Vector Vector::operator - () const
  207. {
  208. return Vector(-x, -y);
  209. }
  210. inline void Vector::operator += (const Vector &v)
  211. {
  212. x += v.getX();
  213. y += v.getY();
  214. }
  215. inline void Vector::operator -= (const Vector &v)
  216. {
  217. x -= v.getX();
  218. y -= v.getY();
  219. }
  220. inline void Vector::operator *= (float s)
  221. {
  222. x *= s;
  223. y *= s;
  224. }
  225. inline void Vector::operator /= (float s)
  226. {
  227. x /= s;
  228. y /= s;
  229. }
  230. inline float Vector::operator * (const Vector &v) const
  231. {
  232. return x * v.getX() + y * v.getY();
  233. }
  234. inline float Vector::operator ^ (const Vector &v) const
  235. {
  236. return x * v.getY() - y * v.getX();
  237. }
  238. inline bool Vector::operator == (const Vector &v) const
  239. {
  240. return getLength() == v.getLength();
  241. }
  242. inline bool Vector::operator < (const Vector &v) const
  243. {
  244. return getLength() < v.getLength();
  245. }
  246. /**
  247. * Accessor methods
  248. **/
  249. inline float Vector::getX() const
  250. {
  251. return x;
  252. }
  253. inline float Vector::getY() const
  254. {
  255. return y;
  256. }
  257. inline void Vector::setX(float x)
  258. {
  259. this->x = x;
  260. }
  261. inline void Vector::setY(float y)
  262. {
  263. this->y = y;
  264. }
  265. } //love
  266. #endif// LOVE_VECTOR_H