Vector2.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Vector2.h
  3. * RVO2 Library
  4. *
  5. * Copyright 2008 University of North Carolina at Chapel Hill
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * Please send all bug reports to <geom@cs.unc.edu>.
  20. *
  21. * The authors may be contacted via:
  22. *
  23. * Jur van den Berg, Stephen J. Guy, Jamie Snape, Ming C. Lin, Dinesh Manocha
  24. * Dept. of Computer Science
  25. * 201 S. Columbia St.
  26. * Frederick P. Brooks, Jr. Computer Science Bldg.
  27. * Chapel Hill, N.C. 27599-3175
  28. * United States of America
  29. *
  30. * <http://gamma.cs.unc.edu/RVO2/>
  31. */
  32. #ifndef RVO_VECTOR2_H_
  33. #define RVO_VECTOR2_H_
  34. /**
  35. * \file Vector2.h
  36. * \brief Contains the Vector2 class.
  37. */
  38. #include <cmath>
  39. #include <ostream>
  40. namespace RVO2D {
  41. /**
  42. * \brief Defines a two-dimensional vector.
  43. */
  44. class Vector2 {
  45. public:
  46. /**
  47. * \brief Constructs and initializes a two-dimensional vector instance
  48. * to (0.0, 0.0).
  49. */
  50. inline Vector2() : x_(0.0f), y_(0.0f) { }
  51. /**
  52. * \brief Constructs and initializes a two-dimensional vector from
  53. * the specified xy-coordinates.
  54. * \param x The x-coordinate of the two-dimensional
  55. * vector.
  56. * \param y The y-coordinate of the two-dimensional
  57. * vector.
  58. */
  59. inline Vector2(float x, float y) : x_(x), y_(y) { }
  60. inline Vector2(const Vector2 &vector)
  61. {
  62. x_ = vector.x();
  63. y_ = vector.y();
  64. }
  65. /**
  66. * \brief Returns the x-coordinate of this two-dimensional vector.
  67. * \return The x-coordinate of the two-dimensional vector.
  68. */
  69. inline float x() const { return x_; }
  70. /**
  71. * \brief Returns the y-coordinate of this two-dimensional vector.
  72. * \return The y-coordinate of the two-dimensional vector.
  73. */
  74. inline float y() const { return y_; }
  75. /**
  76. * \brief Computes the negation of this two-dimensional vector.
  77. * \return The negation of this two-dimensional vector.
  78. */
  79. inline Vector2 operator-() const
  80. {
  81. return Vector2(-x_, -y_);
  82. }
  83. /**
  84. * \brief Computes the dot product of this two-dimensional vector with
  85. * the specified two-dimensional vector.
  86. * \param vector The two-dimensional vector with which the
  87. * dot product should be computed.
  88. * \return The dot product of this two-dimensional vector with a
  89. * specified two-dimensional vector.
  90. */
  91. inline float operator*(const Vector2 &vector) const
  92. {
  93. return x_ * vector.x() + y_ * vector.y();
  94. }
  95. /**
  96. * \brief Computes the scalar multiplication of this
  97. * two-dimensional vector with the specified scalar value.
  98. * \param s The scalar value with which the scalar
  99. * multiplication should be computed.
  100. * \return The scalar multiplication of this two-dimensional vector
  101. * with a specified scalar value.
  102. */
  103. inline Vector2 operator*(float s) const
  104. {
  105. return Vector2(x_ * s, y_ * s);
  106. }
  107. /**
  108. * \brief Computes the scalar division of this two-dimensional vector
  109. * with the specified scalar value.
  110. * \param s The scalar value with which the scalar
  111. * division should be computed.
  112. * \return The scalar division of this two-dimensional vector with a
  113. * specified scalar value.
  114. */
  115. inline Vector2 operator/(float s) const
  116. {
  117. const float invS = 1.0f / s;
  118. return Vector2(x_ * invS, y_ * invS);
  119. }
  120. /**
  121. * \brief Computes the vector sum of this two-dimensional vector with
  122. * the specified two-dimensional vector.
  123. * \param vector The two-dimensional vector with which the
  124. * vector sum should be computed.
  125. * \return The vector sum of this two-dimensional vector with a
  126. * specified two-dimensional vector.
  127. */
  128. inline Vector2 operator+(const Vector2 &vector) const
  129. {
  130. return Vector2(x_ + vector.x(), y_ + vector.y());
  131. }
  132. /**
  133. * \brief Computes the vector difference of this two-dimensional
  134. * vector with the specified two-dimensional vector.
  135. * \param vector The two-dimensional vector with which the
  136. * vector difference should be computed.
  137. * \return The vector difference of this two-dimensional vector with a
  138. * specified two-dimensional vector.
  139. */
  140. inline Vector2 operator-(const Vector2 &vector) const
  141. {
  142. return Vector2(x_ - vector.x(), y_ - vector.y());
  143. }
  144. /**
  145. * \brief Tests this two-dimensional vector for equality with the
  146. * specified two-dimensional vector.
  147. * \param vector The two-dimensional vector with which to
  148. * test for equality.
  149. * \return True if the two-dimensional vectors are equal.
  150. */
  151. inline bool operator==(const Vector2 &vector) const
  152. {
  153. return x_ == vector.x() && y_ == vector.y();
  154. }
  155. /**
  156. * \brief Tests this two-dimensional vector for inequality with the
  157. * specified two-dimensional vector.
  158. * \param vector The two-dimensional vector with which to
  159. * test for inequality.
  160. * \return True if the two-dimensional vectors are not equal.
  161. */
  162. inline bool operator!=(const Vector2 &vector) const
  163. {
  164. return x_ != vector.x() || y_ != vector.y();
  165. }
  166. /**
  167. * \brief Sets the value of this two-dimensional vector to the scalar
  168. * multiplication of itself with the specified scalar value.
  169. * \param s The scalar value with which the scalar
  170. * multiplication should be computed.
  171. * \return A reference to this two-dimensional vector.
  172. */
  173. inline Vector2 &operator*=(float s)
  174. {
  175. x_ *= s;
  176. y_ *= s;
  177. return *this;
  178. }
  179. /**
  180. * \brief Sets the value of this two-dimensional vector to the scalar
  181. * division of itself with the specified scalar value.
  182. * \param s The scalar value with which the scalar
  183. * division should be computed.
  184. * \return A reference to this two-dimensional vector.
  185. */
  186. inline Vector2 &operator/=(float s)
  187. {
  188. const float invS = 1.0f / s;
  189. x_ *= invS;
  190. y_ *= invS;
  191. return *this;
  192. }
  193. /**
  194. * \brief Sets the value of this two-dimensional vector to the vector
  195. * sum of itself with the specified two-dimensional vector.
  196. * \param vector The two-dimensional vector with which the
  197. * vector sum should be computed.
  198. * \return A reference to this two-dimensional vector.
  199. */
  200. inline Vector2 &operator+=(const Vector2 &vector)
  201. {
  202. x_ += vector.x();
  203. y_ += vector.y();
  204. return *this;
  205. }
  206. /**
  207. * \brief Sets the value of this two-dimensional vector to the vector
  208. * difference of itself with the specified two-dimensional
  209. * vector.
  210. * \param vector The two-dimensional vector with which the
  211. * vector difference should be computed.
  212. * \return A reference to this two-dimensional vector.
  213. */
  214. inline Vector2 &operator-=(const Vector2 &vector)
  215. {
  216. x_ -= vector.x();
  217. y_ -= vector.y();
  218. return *this;
  219. }
  220. inline Vector2 &operator=(const Vector2 &vector)
  221. {
  222. x_ = vector.x();
  223. y_ = vector.y();
  224. return *this;
  225. }
  226. private:
  227. float x_;
  228. float y_;
  229. };
  230. /**
  231. * \relates Vector2
  232. * \brief Computes the scalar multiplication of the specified
  233. * two-dimensional vector with the specified scalar value.
  234. * \param s The scalar value with which the scalar
  235. * multiplication should be computed.
  236. * \param vector The two-dimensional vector with which the scalar
  237. * multiplication should be computed.
  238. * \return The scalar multiplication of the two-dimensional vector with the
  239. * scalar value.
  240. */
  241. inline Vector2 operator*(float s, const Vector2 &vector)
  242. {
  243. return Vector2(s * vector.x(), s * vector.y());
  244. }
  245. /**
  246. * \relates Vector2
  247. * \brief Inserts the specified two-dimensional vector into the specified
  248. * output stream.
  249. * \param os The output stream into which the two-dimensional
  250. * vector should be inserted.
  251. * \param vector The two-dimensional vector which to insert into
  252. * the output stream.
  253. * \return A reference to the output stream.
  254. */
  255. inline std::ostream &operator<<(std::ostream &os, const Vector2 &vector)
  256. {
  257. os << "(" << vector.x() << "," << vector.y() << ")";
  258. return os;
  259. }
  260. /**
  261. * \relates Vector2
  262. * \brief Computes the length of a specified two-dimensional vector.
  263. * \param vector The two-dimensional vector whose length is to be
  264. * computed.
  265. * \return The length of the two-dimensional vector.
  266. */
  267. inline float abs(const Vector2 &vector)
  268. {
  269. return std::sqrt(vector * vector);
  270. }
  271. /**
  272. * \relates Vector2
  273. * \brief Computes the squared length of a specified two-dimensional
  274. * vector.
  275. * \param vector The two-dimensional vector whose squared length
  276. * is to be computed.
  277. * \return The squared length of the two-dimensional vector.
  278. */
  279. inline float absSq(const Vector2 &vector)
  280. {
  281. return vector * vector;
  282. }
  283. /**
  284. * \relates Vector2
  285. * \brief Computes the determinant of a two-dimensional square matrix with
  286. * rows consisting of the specified two-dimensional vectors.
  287. * \param vector1 The top row of the two-dimensional square
  288. * matrix.
  289. * \param vector2 The bottom row of the two-dimensional square
  290. * matrix.
  291. * \return The determinant of the two-dimensional square matrix.
  292. */
  293. inline float det(const Vector2 &vector1, const Vector2 &vector2)
  294. {
  295. return vector1.x() * vector2.y() - vector1.y() * vector2.x();
  296. }
  297. /**
  298. * \relates Vector2
  299. * \brief Computes the normalization of the specified two-dimensional
  300. * vector.
  301. * \param vector The two-dimensional vector whose normalization
  302. * is to be computed.
  303. * \return The normalization of the two-dimensional vector.
  304. */
  305. inline Vector2 normalize(const Vector2 &vector)
  306. {
  307. return vector / abs(vector);
  308. }
  309. }
  310. #endif /* RVO_VECTOR2_H_ */