-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector2d.h
338 lines (282 loc) · 8.18 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#ifndef VECTOR2D_H
#define VECTOR2D_H
#include <cmath>
#include <sstream>
#include <iostream>
#include <cstdlib>
/*!
* \class Vec2D
* \brief here, it is included the necessary functions to operate with vectors
*/
class Vec2D
{
public:
/*!
* \brief the vector components
*/
/*
* \todo: Make these private.
* \todo what is the idea of this constructor?
* These should be private so we can implement things like a cvec etc.
* Use getters / setters.
*/
// Vec2D(int i);
// private:
double X, Y;
/*!
* \brief constructor
*/
Vec2D()
{ setZero(); }
/*!
* \brief Alternative constructor, taking the three elements as arguments
* \details Alternative constructor, lets you define all three elements.
* \param[in] x the x-component
* \param[in] y the y-component
*/
Vec2D(const double x, const double y)
{
X = x;
Y = y;
}
/*!
* \brief Sets all elements to zero
*/
void setZero();
/*!
* \brief Checks if ALL elements are zero
*/
bool isZero() const
{ return X == 0.0 && Y == 0.0; }
/*!
* \brief Adds another vector
* \details Adds vector to itself
* \param[in] a vector to be added
* \return resulting 3D vector
*/
Vec2D operator+(const Vec2D& a) const
{
return Vec2D(X + a.X, Y + a.Y);
}
/*!
* \brief Binary vector subtraction
* \details Subtracts a vector from another vector
* \param[in] a vector to be subtracted
* \return resulting vector
*/
inline Vec2D operator-(const Vec2D a) const
{
return Vec2D(X - a.X, Y - a.Y);
};
/*!
* \brief Multiplies by a scalar
* \details Multiplies each element with a scalar
* \param[in] a the scalar to be multiplied with
* \return the resulting vector
*/
Vec2D operator*(const double a) const
{
return Vec2D(X * a, Y * a);
}
/*!
* \brief Divides by a scalar
* \details Divides each element by a scalar
* \param[in] a the scalar to be divided by
* \return resulting vector
*/
Vec2D operator/(double a) const {
return Vec2D(X / a, Y / a);
}
/*!
* \brief Adds another vector
* \details Adds a vector to itself
* \param[in] a vector to be added
* \return (reference to) itself, i.e. resulting vector
*/
Vec2D& operator+=(const Vec2D& a)
{
X += a.X;
Y += a.Y;
return *this;
}
/*!
* \brief Checks if all coordinates satisfy this>=a
*/
bool operator>=(const Vec2D& a) const {
return X>=a.X && Y>=a.Y;
}
bool operator<(const Vec2D& a) const {
return X<a.X && Y<a.Y;
}
/*!
* \brief Subtracts another vector
* \details Subtracts a vector from itself
* \param[in] a vector to be subtracted
* \return (reference to) itself, i.e. resulting vector
*/
Vec2D& operator-=(const Vec2D& a)
{
X -= a.X;
Y -= a.Y;
return *this;
}
/*!
* \brief Multiplies by a scalar
* \details Multiplies each element by a scalar
* \param[in] a scalar to be multiplied by
* \return (reference to) itself, i.e. resulting vector
*/
Vec2D& operator*=(double a) {
X *= a;
Y *= a;
return *this;
}
/*!
* \brief Divides by a scalar
* \details Divides each element by a scalar
* \param[in] a scalar to be divided by
* \return (reference to) itself, i.e. resulting vector
*/
Vec2D& operator/=(const double a)
{
X /= a;
Y /= a;
return *this;
}
/*!
* \brief Calculates the dot product of two Vec2D: \f$ a \cdot b\f$
*/
static double dot(const Vec2D& a, const Vec2D& b);
/*!
* \brief Makes this Vec2D unit length
*/
void normalise();
/*!
* \brief Make this Vec2D a certain length
*/
void setLength(double length);
/*!
* \brief Calculates the cross product of two Vec2D: \f$ a \times b\f$.
* Note: the result if a scalar denoting the z-component only since we are dealing only with 2D
* systems
*/
static double cross(const Vec2D& a, const Vec2D& b);
/*!
* \brief Calculates the distance between two Vec2D: \f$ \sqrt{\left(a-b\right) \cdot \left(a-b\right)} \f$
* \details Calculates the square of the distance (i.e. the length of the difference)
* between two vectors.
* NB: this is a STATIC function!
* \param[in] a the first vector
* \param[in] b the second vector
* \return the square of the distance between the two arguments.
*/
static double getDistance(const Vec2D& a, const Vec2D& b);
/*!
* \brief Calculates the squared distance between two Vec2D: \f$ \left(a-b\right) \cdot \left(a-b\right) \f$
*/
static double getDistanceSquared(const Vec2D& a, const Vec2D& b) {
const double X = a.X-b.X;
const double Y = a.Y-b.Y;
return (X * X + Y * Y);
//return getLengthSquared(a - b);
}
/*!
* \brief Calculates the length of a Vec2D: \f$ \sqrt{a\cdot a} \f$
*/
static double getLength(const Vec2D& a);
/*!
* \brief Calculates the squared length of a Vec2D: \f$ a\cdot a \f$
* \details Calculates the square of the length of a given vector.
* NB: this is a STATIC function!
* \param[in] a the vector.
* \return the square of the length of the argument.
*/
static double getLengthSquared(const Vec2D& a)
{
return (a.X * a.X + a.Y * a.Y);
}
/*!
* \brief Calculates the length of this Vec2D: \f$ \sqrt{a\cdot a} \f$
*/
double getLength() const;
/*!
* \brief Calculates the squared length of this Vec2D: \f$ a\cdot a \f$
*/
double getLengthSquared() const;
/*!
* \brief Returns the requested component of this Vec2D
*/
double getComponent(int index) const;
/*!
* \brief Sets the requested component of this Vec2D to the requested value
*/
void setComponent(int index, double val);
/*!
* \brief RW reference to X
*/
inline double& x()
{ return X; }
/*!
* \brief RO reference to X
*/
inline double x() const
{ return X; }
/*!
* \brief RW reference to Y
*/
inline double& y()
{ return Y; }
/*!
* \brief RO reference to Y
*/
inline double y() const
{ return Y; }
inline void setX(double x)
{ X = x; }
inline void setY(double y)
{ Y = y; }
inline double getX()
{ return X; }
inline double getY()
{ return Y; }
inline void set(double x, double y)
{
X = x;
Y = y;
}
/*!
* \brief Checks if the length this Vec2D is equal the length of other with a certain tolerance
*/
bool isEqualTo(const Vec2D& other, double tol) const;
/*!
* \brief Returns a unit Vec2D based on a.
*/
static Vec2D getUnitVector(const Vec2D& a);
/*!
* \brief Adds elements to an output stream
*/
friend std::ostream& operator<<(std::ostream& os, const Vec2D& a);
/*!
* \brief Adds elements to an input stream
*/
friend std::istream& operator>>(std::istream& is, Vec2D& a);
/*!
* \brief Reverts the direction of a vector
*/
inline friend Vec2D operator-(const Vec2D& a) {
return Vec2D(-a.X, -a.Y);
}
/*!
* \brief Multiplies all elements by a scalar
* \details Multiplies each element of a given vector (b) by a given scalar (a).
* NB: this is a global function and a friend of the Vec2D class. Gets called when
* a scalar multiplication of the form (double) * (Vec2D) is performed.
* \param[in] a the scalar
* \param[in] b the vector
* \return the resulting vector
*/
friend Vec2D operator*(double a, const Vec2D& b) {
return Vec2D(b.X * a, b.Y * a);
}
};
#endif