athos@207: // -*- c++ -*- athos@201: /** athos@201: 2 dimensional vector (plainvector) implementation athos@201: athos@201: */ athos@201: #ifndef HUGO_XY_H athos@201: #define HUGO_XY_H athos@201: athos@201: #include athos@201: athos@207: namespace hugo { athos@201: athos@207: template athos@207: class xy { athos@207: T _x,_y; athos@201: athos@207: public: athos@207: athos@207: ///Default constructor: both coordinates become 0 athos@207: xy() { _x=_y=0; } athos@201: athos@207: ///Constructing from coordinates athos@207: xy(T a, T b) { _x=a; _y=b; } athos@201: athos@207: ///Gives back the x coordinate athos@207: T x(){ athos@207: return _x; athos@207: }; athos@201: athos@207: ///Gives back the y coordinate athos@207: T y(){ athos@207: return _y; athos@207: }; athos@201: athos@207: ///Gives back the square of the norm of the vector athos@207: T normSquare(){ athos@207: return _x*_x+_y*_y; athos@207: }; athos@201: athos@207: ///Increments the left hand side by u athos@207: xy& operator +=(const xy& u){ athos@207: _x += u._x; athos@207: _y += u._y; athos@207: return *this; athos@207: }; athos@201: athos@207: ///Decrements the left hand side by u athos@207: xy& operator -=(const xy& u){ athos@207: _x -= u._x; athos@207: _y -= u._y; athos@207: return *this; athos@207: }; athos@201: athos@207: ///Multiplying the left hand side with a scalar athos@207: xy& operator *=(const T &u){ athos@207: _x *= u; athos@207: _y *= u; athos@207: return *this; athos@207: }; athos@207: athos@207: ///Dividing the left hand side by a scalar athos@207: xy& operator /=(const T &u){ athos@207: _x /= u; athos@207: _y /= u; athos@207: return *this; athos@207: }; athos@201: athos@207: ///Returns the scalar product of two vectors athos@207: T operator *(const xy& u){ athos@207: return _x*u._x+_y*u._y; athos@207: }; athos@201: athos@207: ///Returns the sum of two vectors athos@207: xy operator+(const xy &u) const { athos@207: xy b=*this; athos@207: return b+=u; athos@207: }; athos@201: athos@207: ///Returns the difference of two vectors athos@207: xy operator-(const xy &u) const { athos@207: xy b=*this; athos@207: return b-=u; athos@207: }; athos@201: athos@207: ///Returns a vector multiplied by a scalar athos@207: xy operator*(const T &u) const { athos@207: xy b=*this; athos@207: return b*=u; athos@207: }; athos@201: athos@207: ///Returns a vector divided by a scalar athos@207: xy operator/(const T &u) const { athos@207: xy b=*this; athos@207: return b/=u; athos@207: }; athos@201: athos@207: ///Testing equality athos@207: bool operator==(const xy &u){ athos@207: return (_x==u._x) && (_y==u._y); athos@207: }; athos@201: athos@207: ///Testing inequality athos@207: bool operator!=(xy u){ athos@207: return (_x!=u._x) || (_y!=u._y); athos@207: }; athos@201: athos@207: }; athos@201: athos@207: ///Reading a plainvector from a stream athos@207: template athos@207: inline athos@207: std::istream& operator>>(std::istream &is, xy &z) athos@207: { athos@207: ///This is not the best solution here: I didn't know how to solve this with friend functions athos@207: T a,b; athos@207: is >> a >> b; athos@207: xy buf(a,b); athos@207: z=buf; athos@207: return is; athos@207: } athos@201: athos@207: ///Outputting a plainvector to a stream athos@207: template athos@207: inline athos@207: std::ostream& operator<<(std::ostream &os, xy z) athos@207: { athos@207: os << "(" << z.x() << ", " << z.y() << ")"; athos@207: return os; athos@207: } athos@207: athos@207: } //namespace hugo athos@201: athos@201: #endif //HUGO_XY_H