Vector2.h
Go to the documentation of this file.
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 
33 #ifndef RVO_VECTOR2_H_
34 #define RVO_VECTOR2_H_
35 
36 /**
37  * \file Vector2.h
38  * \brief Contains the Vector2 class.
39  */
40 
41 #include <cmath>
42 #include <ostream>
43 
44 namespace RVO {
45  /**
46  * \brief Defines a two-dimensional vector.
47  */
48  class Vector2 {
49  public:
50  /**
51  * \brief Constructs and initializes a two-dimensional vector instance
52  * to (0.0, 0.0).
53  */
54  inline Vector2() : x_(0.0f), y_(0.0f) { }
55 
56  /**
57  * \brief Constructs and initializes a two-dimensional vector from
58  * the specified xy-coordinates.
59  * \param x The x-coordinate of the two-dimensional
60  * vector.
61  * \param y The y-coordinate of the two-dimensional
62  * vector.
63  */
64  inline Vector2(float x, float y) : x_(x), y_(y) { }
65 
66  /**
67  * \brief Returns the x-coordinate of this two-dimensional vector.
68  * \return The x-coordinate of the two-dimensional vector.
69  */
70  inline float x() const { return x_; }
71 
72  /**
73  * \brief Returns the y-coordinate of this two-dimensional vector.
74  * \return The y-coordinate of the two-dimensional vector.
75  */
76  inline float y() const { return y_; }
77 
78  /**
79  * \brief Computes the negation of this two-dimensional vector.
80  * \return The negation of this two-dimensional vector.
81  */
82  inline Vector2 operator-() const
83  {
84  return Vector2(-x_, -y_);
85  }
86 
87  /**
88  * \brief Computes the dot product of this two-dimensional vector with
89  * the specified two-dimensional vector.
90  * \param vector The two-dimensional vector with which the
91  * dot product should be computed.
92  * \return The dot product of this two-dimensional vector with a
93  * specified two-dimensional vector.
94  */
95  inline float operator*(const Vector2 &vector) const
96  {
97  return x_ * vector.x() + y_ * vector.y();
98  }
99 
100  /**
101  * \brief Computes the scalar multiplication of this
102  * two-dimensional vector with the specified scalar value.
103  * \param s The scalar value with which the scalar
104  * multiplication should be computed.
105  * \return The scalar multiplication of this two-dimensional vector
106  * with a specified scalar value.
107  */
108  inline Vector2 operator*(float s) const
109  {
110  return Vector2(x_ * s, y_ * s);
111  }
112 
113  /**
114  * \brief Computes the scalar division of this two-dimensional vector
115  * with the specified scalar value.
116  * \param s The scalar value with which the scalar
117  * division should be computed.
118  * \return The scalar division of this two-dimensional vector with a
119  * specified scalar value.
120  */
121  inline Vector2 operator/(float s) const
122  {
123  const float invS = 1.0f / s;
124 
125  return Vector2(x_ * invS, y_ * invS);
126  }
127 
128  /**
129  * \brief Computes the vector sum of this two-dimensional vector with
130  * the specified two-dimensional vector.
131  * \param vector The two-dimensional vector with which the
132  * vector sum should be computed.
133  * \return The vector sum of this two-dimensional vector with a
134  * specified two-dimensional vector.
135  */
136  inline Vector2 operator+(const Vector2 &vector) const
137  {
138  return Vector2(x_ + vector.x(), y_ + vector.y());
139  }
140 
141  /**
142  * \brief Computes the vector difference of this two-dimensional
143  * vector with the specified two-dimensional vector.
144  * \param vector The two-dimensional vector with which the
145  * vector difference should be computed.
146  * \return The vector difference of this two-dimensional vector with a
147  * specified two-dimensional vector.
148  */
149  inline Vector2 operator-(const Vector2 &vector) const
150  {
151  return Vector2(x_ - vector.x(), y_ - vector.y());
152  }
153 
154  /**
155  * \brief Tests this two-dimensional vector for equality with the
156  * specified two-dimensional vector.
157  * \param vector The two-dimensional vector with which to
158  * test for equality.
159  * \return True if the two-dimensional vectors are equal.
160  */
161  inline bool operator==(const Vector2 &vector) const
162  {
163  return x_ == vector.x() && y_ == vector.y();
164  }
165 
166  /**
167  * \brief Tests this two-dimensional vector for inequality with the
168  * specified two-dimensional vector.
169  * \param vector The two-dimensional vector with which to
170  * test for inequality.
171  * \return True if the two-dimensional vectors are not equal.
172  */
173  inline bool operator!=(const Vector2 &vector) const
174  {
175  return x_ != vector.x() || y_ != vector.y();
176  }
177 
178  /**
179  * \brief Sets the value of this two-dimensional vector to the scalar
180  * multiplication of itself with the specified scalar value.
181  * \param s The scalar value with which the scalar
182  * multiplication should be computed.
183  * \return A reference to this two-dimensional vector.
184  */
185  inline Vector2 &operator*=(float s)
186  {
187  x_ *= s;
188  y_ *= s;
189 
190  return *this;
191  }
192 
193  /**
194  * \brief Sets the value of this two-dimensional vector to the scalar
195  * division of itself with the specified scalar value.
196  * \param s The scalar value with which the scalar
197  * division should be computed.
198  * \return A reference to this two-dimensional vector.
199  */
200  inline Vector2 &operator/=(float s)
201  {
202  const float invS = 1.0f / s;
203  x_ *= invS;
204  y_ *= invS;
205 
206  return *this;
207  }
208 
209  /**
210  * \brief Sets the value of this two-dimensional vector to the vector
211  * sum of itself with the specified two-dimensional vector.
212  * \param vector The two-dimensional vector with which the
213  * vector sum should be computed.
214  * \return A reference to this two-dimensional vector.
215  */
216  inline Vector2 &operator+=(const Vector2 &vector)
217  {
218  x_ += vector.x();
219  y_ += vector.y();
220 
221  return *this;
222  }
223 
224  /**
225  * \brief Sets the value of this two-dimensional vector to the vector
226  * difference of itself with the specified two-dimensional
227  * vector.
228  * \param vector The two-dimensional vector with which the
229  * vector difference should be computed.
230  * \return A reference to this two-dimensional vector.
231  */
232  inline Vector2 &operator-=(const Vector2 &vector)
233  {
234  x_ -= vector.x();
235  y_ -= vector.y();
236 
237  return *this;
238  }
239 
240  private:
241  float x_;
242  float y_;
243  };
244 
245  /**
246  * \relates Vector2
247  * \brief Computes the scalar multiplication of the specified
248  * two-dimensional vector with the specified scalar value.
249  * \param s The scalar value with which the scalar
250  * multiplication should be computed.
251  * \param vector The two-dimensional vector with which the scalar
252  * multiplication should be computed.
253  * \return The scalar multiplication of the two-dimensional vector with the
254  * scalar value.
255  */
256  inline Vector2 operator*(float s, const Vector2 &vector)
257  {
258  return Vector2(s * vector.x(), s * vector.y());
259  }
260 
261  /**
262  * \relates Vector2
263  * \brief Inserts the specified two-dimensional vector into the specified
264  * output stream.
265  * \param os The output stream into which the two-dimensional
266  * vector should be inserted.
267  * \param vector The two-dimensional vector which to insert into
268  * the output stream.
269  * \return A reference to the output stream.
270  */
271  inline std::ostream &operator<<(std::ostream &os, const Vector2 &vector)
272  {
273  os << "(" << vector.x() << "," << vector.y() << ")";
274 
275  return os;
276  }
277 
278  /**
279  * \relates Vector2
280  * \brief Computes the length of a specified two-dimensional vector.
281  * \param vector The two-dimensional vector whose length is to be
282  * computed.
283  * \return The length of the two-dimensional vector.
284  */
285  inline float abs(const Vector2 &vector)
286  {
287  return std::sqrt(vector * vector);
288  }
289 
290  /**
291  * \relates Vector2
292  * \brief Computes the squared length of a specified two-dimensional
293  * vector.
294  * \param vector The two-dimensional vector whose squared length
295  * is to be computed.
296  * \return The squared length of the two-dimensional vector.
297  */
298  inline float absSq(const Vector2 &vector)
299  {
300  return vector * vector;
301  }
302 
303  /**
304  * \relates Vector2
305  * \brief Computes the determinant of a two-dimensional square matrix with
306  * rows consisting of the specified two-dimensional vectors.
307  * \param vector1 The top row of the two-dimensional square
308  * matrix.
309  * \param vector2 The bottom row of the two-dimensional square
310  * matrix.
311  * \return The determinant of the two-dimensional square matrix.
312  */
313  inline float det(const Vector2 &vector1, const Vector2 &vector2)
314  {
315  return vector1.x() * vector2.y() - vector1.y() * vector2.x();
316  }
317 
318  /**
319  * \relates Vector2
320  * \brief Computes the normalization of the specified two-dimensional
321  * vector.
322  * \param vector The two-dimensional vector whose normalization
323  * is to be computed.
324  * \return The normalization of the two-dimensional vector.
325  */
326  inline Vector2 normalize(const Vector2 &vector)
327  {
328  return vector / abs(vector);
329  }
330 }
331 
332 #endif /* RVO_VECTOR2_H_ */
Defines a two-dimensional vector.
Definition: Vector2.h:48
bool operator==(const Vector2 &vector) const
Tests this two-dimensional vector for equality with the specified two-dimensional vector...
Definition: Vector2.h:161
Vector2 operator-(const Vector2 &vector) const
Computes the vector difference of this two-dimensional vector with the specified two-dimensional vect...
Definition: Vector2.h:149
Vector2 operator*(float s, const Vector2 &vector)
Computes the scalar multiplication of the specified two-dimensional vector with the specified scalar ...
Definition: Vector2.h:256
Vector2 & operator/=(float s)
Sets the value of this two-dimensional vector to the scalar division of itself with the specified sca...
Definition: Vector2.h:200
Vector2 operator-() const
Computes the negation of this two-dimensional vector.
Definition: Vector2.h:82
float det(const Vector2 &vector1, const Vector2 &vector2)
Computes the determinant of a two-dimensional square matrix with rows consisting of the specified two...
Definition: Vector2.h:313
float operator*(const Vector2 &vector) const
Computes the dot product of this two-dimensional vector with the specified two-dimensional vector...
Definition: Vector2.h:95
Vector2 normalize(const Vector2 &vector)
Computes the normalization of the specified two-dimensional vector.
Definition: Vector2.h:326
Vector2 operator+(const Vector2 &vector) const
Computes the vector sum of this two-dimensional vector with the specified two-dimensional vector...
Definition: Vector2.h:136
Vector2 operator/(float s) const
Computes the scalar division of this two-dimensional vector with the specified scalar value...
Definition: Vector2.h:121
bool operator!=(const Vector2 &vector) const
Tests this two-dimensional vector for inequality with the specified two-dimensional vector...
Definition: Vector2.h:173
float abs(const Vector2 &vector)
Computes the length of a specified two-dimensional vector.
Definition: Vector2.h:285
Vector2(float x, float y)
Constructs and initializes a two-dimensional vector from the specified xy-coordinates.
Definition: Vector2.h:64
Vector2()
Constructs and initializes a two-dimensional vector instance to (0.0, 0.0).
Definition: Vector2.h:54
Vector2 operator*(float s) const
Computes the scalar multiplication of this two-dimensional vector with the specified scalar value...
Definition: Vector2.h:108
Vector2 & operator+=(const Vector2 &vector)
Sets the value of this two-dimensional vector to the vector sum of itself with the specified two-dime...
Definition: Vector2.h:216
Vector2 & operator*=(float s)
Sets the value of this two-dimensional vector to the scalar multiplication of itself with the specifi...
Definition: Vector2.h:185
Vector2 & operator-=(const Vector2 &vector)
Sets the value of this two-dimensional vector to the vector difference of itself with the specified t...
Definition: Vector2.h:232
float absSq(const Vector2 &vector)
Computes the squared length of a specified two-dimensional vector.
Definition: Vector2.h:298
float y() const
Returns the y-coordinate of this two-dimensional vector.
Definition: Vector2.h:76
float x() const
Returns the x-coordinate of this two-dimensional vector.
Definition: Vector2.h:70