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