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