1
#ifndef VALHALLA_MIDGARD_VECTOR2_H_
2
#define VALHALLA_MIDGARD_VECTOR2_H_
7
#include <valhalla/midgard/constants.h>
8
#include <valhalla/midgard/point2.h>
14
* 2D vector class. float x,y components.
15
* @author David W. Nesbitt
25
* Constructor given a point. Essentially a vector from the
26
* origin to the point.
29
Vector2(const Point2& p);
32
* Constructor given components of the vector.
33
* @param x x component of the vector.
34
* @param y y component of the vector.
36
Vector2(const float x, const float y);
39
* Constructor from one point to another.
40
* @param from Point at origin of the vector.
41
* @param to Point at end of vector
43
Vector2(const Point2& from, const Point2& to);
47
* @param w Vector to copy to the new vector.
49
Vector2(const Vector2& w);
53
* @param w Vector to copy to the current vector.
54
* @return Returns the address of the current vector.
56
Vector2& operator =(const Vector2& w);
64
* Get the x component.
65
* @return Returns the x component of the vector.
70
* Get the y component.
71
* @return Returns the y component of the vector.
76
* Set the x component.
77
* @param x x coordinate value.
79
void set_x(const float x);
82
* Set the y component.
83
* @param y y coordinate value.
85
void set_y(const float y);
88
* Set the current vector to the specified components.
89
* @param x x component of the vector.
90
* @param y y component of the vector.
92
void Set(const float x, const float y);
95
* Set the vector components to those of a point. Essentially a
96
* vector from the origin to the point.
99
void Set(const Point2& p);
102
* Set the current vector to be from one point to another.
103
* @param from Point at origin of the vector.
104
* @param to Point at end of vector
106
void Set(const Point2& from, const Point2& to);
109
* Creates a new vector that is the current vector plus the
111
* @param w Vector to add to the current vector.
112
* @return Returns the resulting vector.
114
Vector2 operator +(const Vector2& w) const;
117
* Adds vector w to the current vector.
118
* @param w Vector to add to the current vector.
119
* @return Returns the address of the current vector.
121
Vector2& operator +=(const Vector2& w);
124
* Creates a new vector that is the current vector minus the
126
* @param w Vector to subtract from the current vector.
127
* @return Returns the resulting vector.
129
Vector2 operator -(const Vector2& w) const;
132
* Subtracts vector w from the current vector.
133
* @param w Vector to subtract from the current vector.
134
* @return Returns the address of the current vector.
136
Vector2& operator -=(const Vector2& w);
139
* Creates a new vector that is the current vector multiplied
140
* with the specified scalar.
141
* @param scalar Scalar to muliply the vector with.
142
* @return Returns the resulting vector
144
Vector2 operator *(const float scalar) const;
147
* Multiplies the current vector by a scalar
148
* @param scalar Scalar to muliply the vector with.
149
* @return Returns the address of the current vector.
151
Vector2& operator *=(const float scalar);
155
* @param w Vector to test if equal to the current vector.
156
* @return Returns true if vector w equals the current vector,
159
bool operator ==(const Vector2& w) const;
162
* Computes the dot product of the current vector with the
165
* @return Returns the dot product (a scalar).
167
float Dot(const Vector2& w) const;
170
* Computes the 2D cross product of current vector with w0.
171
* @param w Vector to take the cross product with (current X w)
172
* @return Returns the magnitude of the resulting vector (which is
175
float Cross(const Vector2& w) const;
178
* Get a perpendicular vector to this vector.
179
* @param clockwise If true. get the clockwise oriented perpendicular.
180
* If false, get the counter-clockwise oriented
183
Vector2 GetPerpendicular(const bool clockwise = false) const;
186
* Computes the norm (length) of the current vector.
187
* @return Returns the length of the vector.
192
* Computes the squared norm of a vector
193
* (Useful when absolute distance is not required)
194
* @return Returns the length squared of the vector.
196
float NormSquared(void) const;
199
* Normalizes the vector.
200
* @return Returns the address of the current vector.
202
Vector2& Normalize();
205
* Calculates the component of the current vector along the
207
* @param w Vector to determine component along.
208
* @return Returns the component of the current vector along w.
210
float Component(const Vector2& w) const;
213
* Creates a new vector that is the projection of the current
214
* vector along the specified vector.
215
* @param w Vector to determine projection along.
216
* @return Returns the new vector.
218
Vector2 Projection(const Vector2& w) const;
221
* Calculates the angle (radians) between the current vector and
222
* the specified vector.
223
* @param w Vector to determine angle from current vector.
224
* @return Returns the angle in radians between the two vectors.
226
float AngleBetween(const Vector2& w) const;
229
* Reflects the current vector given a normal to the reflecting surface.
230
* Assumes the normal is unit length. Note that if done properly the
231
* magnitude of the reflected vector will equal the magnitude of the
233
* @param normal unit length normal to the vector where reflection occurs
234
* @return Returns the reflected vector
236
Vector2 Reflect(const Vector2& normal) const;
239
// x,y coordinate of the point
245
* Creates a new vector that is the specified vector multiplied
246
* with the specified scalar.
247
* @param scalar Scalar to muliply the vector with.
248
* @param Vector2 Vector to be multiplied with the scalar
249
* @return Returns the resulting vector
251
Vector2 operator *(float s, const Vector2 &v);
256
#endif // VALHALLA_MIDGARD_VECTOR2_H_