35
|
1 |
#ifndef COOR_H
|
|
2 |
#define COOR_H
|
|
3 |
|
|
4 |
#include <iostream>
|
41
|
5 |
#include <cmath>
|
35
|
6 |
|
|
7 |
/**
|
|
8 |
* 2D Vector class. Implements standard vector operations.
|
|
9 |
*/
|
|
10 |
template <typename T>
|
|
11 |
class _Vector {
|
|
12 |
public:
|
|
13 |
T x;
|
|
14 |
T y;
|
|
15 |
|
|
16 |
_Vector() : x(0), y(0){}
|
|
17 |
/**
|
|
18 |
* @param x Initial x-coordinate.
|
|
19 |
* @param y Initial y-coordinate.
|
|
20 |
*/
|
|
21 |
_Vector(T x, T y) : x(x), y(y) {}
|
|
22 |
/**
|
|
23 |
* @param v Other vector to be copied.
|
|
24 |
*/
|
|
25 |
_Vector(const _Vector &v) : x(v.x), y(v.y) {}
|
|
26 |
|
|
27 |
void operator=(const _Vector &v) {
|
|
28 |
this->x = v.x;
|
|
29 |
this->y = v.y;
|
|
30 |
}
|
|
31 |
_Vector operator+(const _Vector &v) const {
|
|
32 |
return _Vector(this->x+v.x, this->y+v.y);
|
|
33 |
}
|
|
34 |
_Vector operator-(const _Vector &v) const {
|
|
35 |
return _Vector(this->x-v.x, this->y-v.y);
|
|
36 |
}
|
|
37 |
_Vector operator*(const T &d) const {
|
|
38 |
return _Vector(this->x*d, this->y*d);
|
|
39 |
}
|
|
40 |
_Vector operator/(const T &d) const {
|
|
41 |
return _Vector(this->x/d, this->y/d);
|
|
42 |
}
|
|
43 |
void operator+=(const _Vector &v) {
|
|
44 |
this->x += v.x;
|
|
45 |
this->y += v.y;
|
|
46 |
}
|
|
47 |
void operator-=(const _Vector &v) {
|
|
48 |
this->x -= v.x;
|
|
49 |
this->y -= v.y;
|
|
50 |
}
|
|
51 |
void operator*=(const T &f) {
|
|
52 |
this->x *= f;
|
|
53 |
this->y *= f;
|
|
54 |
}
|
|
55 |
void operator/=(const T &d) {
|
|
56 |
this->x /= d;
|
|
57 |
this->y /= d;
|
|
58 |
}
|
41
|
59 |
T length() const {
|
|
60 |
return sqrt(x*x+y*y);
|
|
61 |
}
|
35
|
62 |
};
|
|
63 |
|
|
64 |
template<typename T>
|
|
65 |
bool operator==(const _Vector<T> &v1, const _Vector<T> &v2) {
|
|
66 |
return ((v1.x == v2.x) && (v1.y == v2.y));
|
|
67 |
}
|
|
68 |
template<typename T>
|
|
69 |
bool operator!=(const _Vector<T> &v1, const _Vector<T> &v2) {
|
|
70 |
return !(v1 == v2);
|
|
71 |
}
|
|
72 |
|
|
73 |
template<typename T>
|
|
74 |
std::ostream& operator<<(std::ostream &s, const _Vector<T> &v) {
|
|
75 |
return s<<"("<<v.x<<", "<<v.y<<")";
|
|
76 |
}
|
|
77 |
|
|
78 |
typedef _Vector<float> Vector;
|
|
79 |
|
|
80 |
#endif
|