[207] | 1 | // -*- c++ -*- |
---|
[201] | 2 | /** |
---|
| 3 | 2 dimensional vector (plainvector) implementation |
---|
| 4 | |
---|
| 5 | */ |
---|
| 6 | #ifndef HUGO_XY_H |
---|
| 7 | #define HUGO_XY_H |
---|
| 8 | |
---|
| 9 | #include <iostream> |
---|
| 10 | |
---|
[207] | 11 | namespace hugo { |
---|
[201] | 12 | |
---|
[207] | 13 | template<typename T> |
---|
| 14 | class xy { |
---|
| 15 | T _x,_y; |
---|
[201] | 16 | |
---|
[207] | 17 | public: |
---|
| 18 | |
---|
| 19 | ///Default constructor: both coordinates become 0 |
---|
| 20 | xy() { _x=_y=0; } |
---|
[201] | 21 | |
---|
[207] | 22 | ///Constructing from coordinates |
---|
| 23 | xy(T a, T b) { _x=a; _y=b; } |
---|
[201] | 24 | |
---|
[207] | 25 | ///Gives back the x coordinate |
---|
| 26 | T x(){ |
---|
| 27 | return _x; |
---|
| 28 | }; |
---|
[201] | 29 | |
---|
[207] | 30 | ///Gives back the y coordinate |
---|
| 31 | T y(){ |
---|
| 32 | return _y; |
---|
| 33 | }; |
---|
[201] | 34 | |
---|
[207] | 35 | ///Gives back the square of the norm of the vector |
---|
| 36 | T normSquare(){ |
---|
| 37 | return _x*_x+_y*_y; |
---|
| 38 | }; |
---|
[201] | 39 | |
---|
[207] | 40 | ///Increments the left hand side by u |
---|
| 41 | xy<T>& operator +=(const xy<T>& u){ |
---|
| 42 | _x += u._x; |
---|
| 43 | _y += u._y; |
---|
| 44 | return *this; |
---|
| 45 | }; |
---|
[201] | 46 | |
---|
[207] | 47 | ///Decrements the left hand side by u |
---|
| 48 | xy<T>& operator -=(const xy<T>& u){ |
---|
| 49 | _x -= u._x; |
---|
| 50 | _y -= u._y; |
---|
| 51 | return *this; |
---|
| 52 | }; |
---|
[201] | 53 | |
---|
[207] | 54 | ///Multiplying the left hand side with a scalar |
---|
| 55 | xy<T>& operator *=(const T &u){ |
---|
| 56 | _x *= u; |
---|
| 57 | _y *= u; |
---|
| 58 | return *this; |
---|
| 59 | }; |
---|
| 60 | |
---|
| 61 | ///Dividing the left hand side by a scalar |
---|
| 62 | xy<T>& operator /=(const T &u){ |
---|
| 63 | _x /= u; |
---|
| 64 | _y /= u; |
---|
| 65 | return *this; |
---|
| 66 | }; |
---|
[201] | 67 | |
---|
[207] | 68 | ///Returns the scalar product of two vectors |
---|
| 69 | T operator *(const xy<T>& u){ |
---|
| 70 | return _x*u._x+_y*u._y; |
---|
| 71 | }; |
---|
[201] | 72 | |
---|
[207] | 73 | ///Returns the sum of two vectors |
---|
| 74 | xy<T> operator+(const xy<T> &u) const { |
---|
| 75 | xy<T> b=*this; |
---|
| 76 | return b+=u; |
---|
| 77 | }; |
---|
[201] | 78 | |
---|
[207] | 79 | ///Returns the difference of two vectors |
---|
| 80 | xy<T> operator-(const xy<T> &u) const { |
---|
| 81 | xy<T> b=*this; |
---|
| 82 | return b-=u; |
---|
| 83 | }; |
---|
[201] | 84 | |
---|
[207] | 85 | ///Returns a vector multiplied by a scalar |
---|
| 86 | xy<T> operator*(const T &u) const { |
---|
| 87 | xy<T> b=*this; |
---|
| 88 | return b*=u; |
---|
| 89 | }; |
---|
[201] | 90 | |
---|
[207] | 91 | ///Returns a vector divided by a scalar |
---|
| 92 | xy<T> operator/(const T &u) const { |
---|
| 93 | xy<T> b=*this; |
---|
| 94 | return b/=u; |
---|
| 95 | }; |
---|
[201] | 96 | |
---|
[207] | 97 | ///Testing equality |
---|
| 98 | bool operator==(const xy<T> &u){ |
---|
| 99 | return (_x==u._x) && (_y==u._y); |
---|
| 100 | }; |
---|
[201] | 101 | |
---|
[207] | 102 | ///Testing inequality |
---|
| 103 | bool operator!=(xy u){ |
---|
| 104 | return (_x!=u._x) || (_y!=u._y); |
---|
| 105 | }; |
---|
[201] | 106 | |
---|
[207] | 107 | }; |
---|
[201] | 108 | |
---|
[207] | 109 | ///Reading a plainvector from a stream |
---|
| 110 | template<typename T> |
---|
| 111 | inline |
---|
| 112 | std::istream& operator>>(std::istream &is, xy<T> &z) |
---|
| 113 | { |
---|
| 114 | ///This is not the best solution here: I didn't know how to solve this with friend functions |
---|
| 115 | T a,b; |
---|
| 116 | is >> a >> b; |
---|
| 117 | xy<T> buf(a,b); |
---|
| 118 | z=buf; |
---|
| 119 | return is; |
---|
| 120 | } |
---|
[201] | 121 | |
---|
[207] | 122 | ///Outputting a plainvector to a stream |
---|
| 123 | template<typename T> |
---|
| 124 | inline |
---|
| 125 | std::ostream& operator<<(std::ostream &os, xy<T> z) |
---|
| 126 | { |
---|
| 127 | os << "(" << z.x() << ", " << z.y() << ")"; |
---|
| 128 | return os; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | } //namespace hugo |
---|
[201] | 132 | |
---|
| 133 | #endif //HUGO_XY_H |
---|