[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 { |
---|
[201] | 15 | |
---|
[207] | 16 | public: |
---|
[240] | 17 | |
---|
| 18 | T x,y; |
---|
[207] | 19 | |
---|
| 20 | ///Default constructor: both coordinates become 0 |
---|
[240] | 21 | xy() : x(0), y(0) {} |
---|
[201] | 22 | |
---|
[240] | 23 | ///Constructing the instance from coordinates |
---|
| 24 | xy(T a, T b) : x(a), y(a) { } |
---|
[201] | 25 | |
---|
| 26 | |
---|
[207] | 27 | ///Gives back the square of the norm of the vector |
---|
| 28 | T normSquare(){ |
---|
[240] | 29 | return x*x+y*y; |
---|
[207] | 30 | }; |
---|
[201] | 31 | |
---|
[207] | 32 | ///Increments the left hand side by u |
---|
| 33 | xy<T>& operator +=(const xy<T>& u){ |
---|
[240] | 34 | x += u.x; |
---|
| 35 | y += u.y; |
---|
[207] | 36 | return *this; |
---|
| 37 | }; |
---|
[201] | 38 | |
---|
[207] | 39 | ///Decrements the left hand side by u |
---|
| 40 | xy<T>& operator -=(const xy<T>& u){ |
---|
[240] | 41 | x -= u.x; |
---|
| 42 | y -= u.y; |
---|
[207] | 43 | return *this; |
---|
| 44 | }; |
---|
[201] | 45 | |
---|
[207] | 46 | ///Multiplying the left hand side with a scalar |
---|
| 47 | xy<T>& operator *=(const T &u){ |
---|
[240] | 48 | x *= u; |
---|
| 49 | y *= u; |
---|
[207] | 50 | return *this; |
---|
| 51 | }; |
---|
| 52 | |
---|
| 53 | ///Dividing the left hand side by a scalar |
---|
| 54 | xy<T>& operator /=(const T &u){ |
---|
[240] | 55 | x /= u; |
---|
| 56 | y /= u; |
---|
[207] | 57 | return *this; |
---|
| 58 | }; |
---|
[201] | 59 | |
---|
[207] | 60 | ///Returns the scalar product of two vectors |
---|
| 61 | T operator *(const xy<T>& u){ |
---|
[240] | 62 | return x*u.x+y*u.y; |
---|
[207] | 63 | }; |
---|
[201] | 64 | |
---|
[207] | 65 | ///Returns the sum of two vectors |
---|
| 66 | xy<T> operator+(const xy<T> &u) const { |
---|
| 67 | xy<T> b=*this; |
---|
| 68 | return b+=u; |
---|
| 69 | }; |
---|
[201] | 70 | |
---|
[207] | 71 | ///Returns the difference of two vectors |
---|
| 72 | xy<T> operator-(const xy<T> &u) const { |
---|
| 73 | xy<T> b=*this; |
---|
| 74 | return b-=u; |
---|
| 75 | }; |
---|
[201] | 76 | |
---|
[207] | 77 | ///Returns a vector multiplied by a scalar |
---|
| 78 | xy<T> operator*(const T &u) const { |
---|
| 79 | xy<T> b=*this; |
---|
| 80 | return b*=u; |
---|
| 81 | }; |
---|
[201] | 82 | |
---|
[207] | 83 | ///Returns a vector divided by a scalar |
---|
| 84 | xy<T> operator/(const T &u) const { |
---|
| 85 | xy<T> b=*this; |
---|
| 86 | return b/=u; |
---|
| 87 | }; |
---|
[201] | 88 | |
---|
[207] | 89 | ///Testing equality |
---|
| 90 | bool operator==(const xy<T> &u){ |
---|
[240] | 91 | return (x==u.x) && (y==u.y); |
---|
[207] | 92 | }; |
---|
[201] | 93 | |
---|
[207] | 94 | ///Testing inequality |
---|
| 95 | bool operator!=(xy u){ |
---|
[240] | 96 | return (x!=u.x) || (y!=u.y); |
---|
[207] | 97 | }; |
---|
[201] | 98 | |
---|
[207] | 99 | }; |
---|
[201] | 100 | |
---|
[207] | 101 | ///Reading a plainvector from a stream |
---|
| 102 | template<typename T> |
---|
| 103 | inline |
---|
| 104 | std::istream& operator>>(std::istream &is, xy<T> &z) |
---|
| 105 | { |
---|
[240] | 106 | |
---|
| 107 | is >> z.x >> z.y; |
---|
[207] | 108 | return is; |
---|
| 109 | } |
---|
[201] | 110 | |
---|
[207] | 111 | ///Outputting a plainvector to a stream |
---|
| 112 | template<typename T> |
---|
| 113 | inline |
---|
| 114 | std::ostream& operator<<(std::ostream &os, xy<T> z) |
---|
| 115 | { |
---|
[240] | 116 | os << "(" << z.x << ", " << z.y << ")"; |
---|
[207] | 117 | return os; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | } //namespace hugo |
---|
[201] | 121 | |
---|
| 122 | #endif //HUGO_XY_H |
---|