-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector2d.h
300 lines (262 loc) · 9.37 KB
/
vector2d.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#ifndef VECTOR2D_H
#define VECTOR2D_H
#include <iostream>
#include <cmath>
#include "vector3d.h"
class Vector3D;
/**
* @brief The Vector2D class
*/
class Vector2D
{
private:
double xp; /**< The x coordinate of the vector */
double yp; /**< The y coordinate of the vector */
public:
/**
* Default constructor, initializes xp and yp to 0
*/
Vector2D();
/**
* Constructor of Vector2D
* @param[in] xpos the x position of the vector
* @param[in] ypos the y position of the vector
*/
Vector2D(double xpos, double ypos);
/**
* @brief Return the x value of the vector
* @return the x value of the vector
*/
double x() const;
/**
* @brief Return the y value of the vector
* @return the y value of the vector
*/
double y() const;
/**
* Sets the value of xp
* @param[in] x the new value of xp
*/
void setX(double x);
/**
* Sets the value of yp
* @param[in] y the new value of yp
*/
void setY(double y);
/**
* Returns the length of the vector. Uses a square root
* @return the length of the vector
*/
double length() const;
/**
* Returns the squared length of the vector
* @return the squared length of the vector
*/
double lengthSquared() const;
/**
* Returns the normalized vector
* @return a new Vector2D corresponding to the normalized vector
*/
Vector2D normalized() const;
/**
* Normalizes the vector
*/
void normalize();
/**
* Returns the distance between the point represented by the vector and another point. Uses a square root
* @param[in] point the point we want the distance to
* @return the distance between the two points
*/
double distanceToPoint2D(const Vector2D& point) const;
/**
* Returns the square of the distance between the point represented by the vector and another point
* @param[in] point the point we want the distance to
* @return the square of the distance between the two points
*/
double distanceToPoint2DSquared(const Vector2D& point) const;
/**
* Returns the shortest distance between the 2D point and a line
* @param[in] point a point on the line
* @param[in] direction the direction of the line
* @return the shortest distance between the 2D point and the line
*/
double distanceToLine(const Vector2D& point, const Vector2D& direction) const;
/**
* Adds a vector to this vector and returns its reference
* @param[in] vector the vector to add
* @return a reference to the vector
*/
Vector2D &operator+=(const Vector2D& vector);
/**
* Substracts a vector to this vector and returns its reference
* @param[in] vector the vector to substract
* @return a reference to the vector
*/
Vector2D &operator-=(const Vector2D& vector);
/**
* Multiplies the vector by a factor and returns its reference
* @param[in] factor the factor to multiply by
* @return a reference to the vector
*/
Vector2D &operator*=(double factor);
/**
* Divides the vector by a divisor and returns its reference
* @param[in] divisor the divisor to divide by
* @return a reference to the vector
*/
Vector2D &operator/=(double divisor);
/**
* Checks if the coordinates of the two vectors are strictly equal
* @param[in] v1 the first vector to compare
* @param[in] v2 the vector to compare to
* @return true if the coordinates xp and yp of the vectors are equals, else false
*/
friend inline bool operator==(const Vector2D& v1, const Vector2D& v2);
/**
* Checks if the coordinates of the two vectors are not strictly equal
* @param[in] v1 the first vector to compare
* @param[in] v2 the vector to compare to
* @return true if the coordinates xp and yp of the vectors are not equals, else false
*/
friend inline bool operator!=(const Vector2D& v1, const Vector2D& v2);
/**
* Adds two vectors and returns the result of the addition
* @param[in] v1 the first vector to add
* @param[in] v2 the second vector to add
* @return a new Vector2D resulting of the addition of the two others
*/
friend inline const Vector2D operator+(const Vector2D& v1, const Vector2D& v2);
/**
* Substracts a vector to the other and returns the result of the addition
* @param[in] v1 the first vector to be substracted
* @param[in] v2 the second vector to substract
* @return a new Vector2D resulting of the substraction
*/
friend inline const Vector2D operator-(const Vector2D& v1, const Vector2D& v2);
/**
* Multiplies a vector by a factor and returns the result of the multiplication
* @param[in] factor the factor to multiply by
* @param[in] v2 the vector to multiply
* @return a new Vector2D resulting of the multiplication
*/
friend inline const Vector2D operator*(double factor, const Vector2D& v2);
/**
* Multiplies a vector by a factor and returns the result of the multiplication
* @param[in] v1 the vector to multiply
* @param[in] factor the factor to multiply by
* @return a new Vector2D resulting of the multiplication
*/
friend inline const Vector2D operator*(const Vector2D& v1, double factor);
/**
* Dot product between the two vectors
* @param[in] v1 the first vector
* @param[in] v2 the second vector
* @return the value of the dot product between the two vectors
*/
friend inline double operator*(const Vector2D& v1, const Vector2D& v2);
/**
* Reverses the coordinates of the vector
* @param[in] v1 the vector to reverse
* @return a new Vector2D resulting of the reversing
*/
friend inline const Vector2D operator-(const Vector2D& v1);
/**
* Divides a vector by a divisor and returns the result of the division
* @param[in] v1 the vector to divide
* @param[in] divisor the divisor to divide by
* @return a new Vector2D resulting of the division
*/
friend inline const Vector2D operator/(const Vector2D& v1, double divisor);
/**
* Compares the coordinates of the two vectors, allowing a small difference
* @param[in] v1 the first vector to compare
* @param[in] v2 the vector to compare to
* @return true if the vectors are at worst slightly different, else false
*/
friend inline bool fuzzyCompare(const Vector2D& v1, const Vector2D& v2);
/**
* Prints the coordinates of the vector in the ostream
* @param[in] os the stream to write in
* @param[in] v the vector to write in the stream
* @return a reference to the stream
*/
friend inline std::ostream& operator<<(std::ostream& os, const Vector2D& v);
/**
* Creates a Vector3D having the same xp and yp, and zp to 0
* @return a new Vector3D, having the same xp and yp, and zp to 0
*/
Vector3D toVector3D() const;
friend class Vector3D;
};
inline Vector2D::Vector2D() : xp(0.0), yp(0.0) {}
inline Vector2D::Vector2D(double xpos, double ypos) : xp(xpos), yp(ypos) {}
inline double Vector2D::x() const {return xp;}
inline double Vector2D::y() const {return yp;}
inline void Vector2D::setX(double ax) { xp = ax; }
inline void Vector2D::setY(double ay) { yp = ay; }
inline double Vector2D::length() const {
return sqrt(xp*xp+yp*yp);
}
inline double Vector2D::lengthSquared() const {
return xp*xp+yp*yp;
}
inline double Vector2D::distanceToPoint2D(const Vector2D& point) const {
return (point-(*this)).length();
}
inline double Vector2D::distanceToPoint2DSquared(const Vector2D& point) const {
return (point-(*this)).lengthSquared();
}
inline Vector2D& Vector2D::operator+=(const Vector2D& v) {
xp += v.xp;
yp += v.yp;
return *this;
}
inline Vector2D& Vector2D::operator-=(const Vector2D& v) {
xp -= v.xp;
yp -= v.yp;
return *this;
}
inline Vector2D& Vector2D::operator*=(double factor) {
xp *= factor;
yp *= factor;
return *this;
}
inline Vector2D& Vector2D::operator/=(double divisor) {
xp /= divisor;
yp /= divisor;
return *this;
}
inline bool operator==(const Vector2D& v1, const Vector2D& v2) {
return v1.xp == v2.xp && v1.yp == v2.yp ;
}
inline bool operator!=(const Vector2D& v1, const Vector2D& v2) {
return v1.xp != v2.xp && v1.yp != v2.yp ;
}
inline const Vector2D operator+(const Vector2D& v1, const Vector2D& v2) {
return Vector2D(v1.xp + v2.xp , v1.yp + v2.yp);
}
inline const Vector2D operator-(const Vector2D& v1, const Vector2D& v2) {
return Vector2D(v1.xp - v2.xp , v1.yp - v2.yp);
}
inline const Vector2D operator*(double factor, const Vector2D& v) {
return Vector2D(v.xp * factor, v.yp * factor);
}
inline const Vector2D operator*(const Vector2D& v, double factor) {
return Vector2D(v.xp * factor, v.yp * factor);
}
inline double operator*(const Vector2D& v1, const Vector2D& v2) {
return (v1.xp * v2.xp + v1.yp * v2.yp);
}
inline const Vector2D operator-(const Vector2D& v) {
return Vector2D(-v.xp, -v.yp);
}
inline const Vector2D operator/(const Vector2D& v, double divisor) {
return Vector2D(v.xp / divisor, v.yp / divisor);
}
inline bool fuzzyCompare(const Vector2D& v1, const Vector2D& v2) {
return v1.distanceToPoint2DSquared(v2) < 0.0000001;
}
inline std::ostream& operator<<(std::ostream& os, const Vector2D& v) {
return os << "(" << v.xp << ", " << v.yp << ")";
}
#endif // VECTOR2D_H