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 <iostream>
athos@201: 
athos@207: namespace hugo {
athos@201: 
athos@207:   template<typename T>
athos@207:     class xy {
athos@201: 
athos@207:     public:
athos@240: 
athos@240:       T x,y;     
athos@207:       
athos@207:       ///Default constructor: both coordinates become 0
athos@240:       xy() : x(0), y(0) {}
athos@201: 
athos@240:       ///Constructing the instance from coordinates
athos@240:       xy(T a, T b) : x(a), y(a) { }
athos@201: 
athos@201: 
athos@207:       ///Gives back the square of the norm of the vector
athos@207:       T normSquare(){
athos@240: 	return x*x+y*y;
athos@207:       };
athos@201:   
athos@207:       ///Increments the left hand side by u
athos@207:       xy<T>& operator +=(const xy<T>& u){
athos@240: 	x += u.x;
athos@240: 	y += u.y;
athos@207: 	return *this;
athos@207:       };
athos@201:   
athos@207:       ///Decrements the left hand side by u
athos@207:       xy<T>& operator -=(const xy<T>& u){
athos@240: 	x -= u.x;
athos@240: 	y -= u.y;
athos@207: 	return *this;
athos@207:       };
athos@201: 
athos@207:       ///Multiplying the left hand side with a scalar
athos@207:       xy<T>& operator *=(const T &u){
athos@240: 	x *= u;
athos@240: 	y *= u;
athos@207: 	return *this;
athos@207:       };
athos@207: 
athos@207:       ///Dividing the left hand side by a scalar
athos@207:       xy<T>& operator /=(const T &u){
athos@240: 	x /= u;
athos@240: 	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<T>& u){
athos@240: 	return x*u.x+y*u.y;
athos@207:       };
athos@201:   
athos@207:       ///Returns the sum of two vectors
athos@207:       xy<T> operator+(const xy<T> &u) const {
athos@207: 	xy<T> b=*this;
athos@207: 	return b+=u;
athos@207:       };
athos@201: 
athos@207:       ///Returns the difference of two vectors
athos@207:       xy<T> operator-(const xy<T> &u) const {
athos@207: 	xy<T> b=*this;
athos@207: 	return b-=u;
athos@207:       };
athos@201: 
athos@207:       ///Returns a vector multiplied by a scalar
athos@207:       xy<T> operator*(const T &u) const {
athos@207: 	xy<T> b=*this;
athos@207: 	return b*=u;
athos@207:       };
athos@201: 
athos@207:       ///Returns a vector divided by a scalar
athos@207:       xy<T> operator/(const T &u) const {
athos@207: 	xy<T> b=*this;
athos@207: 	return b/=u;
athos@207:       };
athos@201: 
athos@207:       ///Testing equality
athos@207:       bool operator==(const xy<T> &u){
athos@240: 	return (x==u.x) && (y==u.y);
athos@207:       };
athos@201: 
athos@207:       ///Testing inequality
athos@207:       bool operator!=(xy u){
athos@240: 	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<typename T>
athos@207:   inline
athos@207:   std::istream& operator>>(std::istream &is, xy<T> &z)
athos@207:   {
athos@240: 
athos@240:     is >> z.x >> z.y;
athos@207:     return is;
athos@207:   }
athos@201: 
athos@207:   ///Outputting a plainvector to a stream
athos@207:   template<typename T>
athos@207:   inline
athos@207:   std::ostream& operator<<(std::ostream &os, xy<T> z)
athos@207:   {
athos@240:     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