1.1 --- a/src/work/athos/xy/xy.h Fri Mar 19 09:09:20 2004 +0000
1.2 +++ b/src/work/athos/xy/xy.h Fri Mar 19 14:47:36 2004 +0000
1.3 @@ -1,3 +1,4 @@
1.4 +// -*- c++ -*-
1.5 /**
1.6 2 dimensional vector (plainvector) implementation
1.7
1.8 @@ -7,111 +8,126 @@
1.9
1.10 #include <iostream>
1.11
1.12 -using namespace std;
1.13 -template<typename T>
1.14 -class xy {
1.15 - T _x,_y;
1.16 +namespace hugo {
1.17
1.18 -public:
1.19 + template<typename T>
1.20 + class xy {
1.21 + T _x,_y;
1.22
1.23 - ///Default constructor: both coordinates become 0
1.24 - xy() { _x=_y=0; }
1.25 + public:
1.26 +
1.27 + ///Default constructor: both coordinates become 0
1.28 + xy() { _x=_y=0; }
1.29
1.30 - ///Constructing from coordinates
1.31 - xy(T a, T b) { _x=a; _y=b; }
1.32 + ///Constructing from coordinates
1.33 + xy(T a, T b) { _x=a; _y=b; }
1.34
1.35 - ///Gives back the x coordinate
1.36 - T x(){
1.37 - return _x;
1.38 - };
1.39 + ///Gives back the x coordinate
1.40 + T x(){
1.41 + return _x;
1.42 + };
1.43
1.44 - ///Gives back the y coordinate
1.45 - T y(){
1.46 - return _y;
1.47 - };
1.48 + ///Gives back the y coordinate
1.49 + T y(){
1.50 + return _y;
1.51 + };
1.52
1.53 - ///Gives back the square of the norm of the vector
1.54 - T normSquare(){
1.55 - return _x*_x+_y*_y;
1.56 - };
1.57 + ///Gives back the square of the norm of the vector
1.58 + T normSquare(){
1.59 + return _x*_x+_y*_y;
1.60 + };
1.61
1.62 - ///Increments the left hand side by u
1.63 - xy<T>& operator +=(const xy<T>& u){
1.64 - _x += u._x;
1.65 - _y += u._y;
1.66 - return *this;
1.67 - };
1.68 + ///Increments the left hand side by u
1.69 + xy<T>& operator +=(const xy<T>& u){
1.70 + _x += u._x;
1.71 + _y += u._y;
1.72 + return *this;
1.73 + };
1.74
1.75 - ///Decrements the left hand side by u
1.76 - xy<T>& operator -=(const xy<T>& u){
1.77 - _x -= u._x;
1.78 - _y -= u._y;
1.79 - return *this;
1.80 - };
1.81 + ///Decrements the left hand side by u
1.82 + xy<T>& operator -=(const xy<T>& u){
1.83 + _x -= u._x;
1.84 + _y -= u._y;
1.85 + return *this;
1.86 + };
1.87
1.88 - ///Multiplying the left hand side with a scalar
1.89 - xy<T>& operator *=(const T &u){
1.90 - _x *= u;
1.91 - _y *= u;
1.92 - return *this;
1.93 - };
1.94 + ///Multiplying the left hand side with a scalar
1.95 + xy<T>& operator *=(const T &u){
1.96 + _x *= u;
1.97 + _y *= u;
1.98 + return *this;
1.99 + };
1.100 +
1.101 + ///Dividing the left hand side by a scalar
1.102 + xy<T>& operator /=(const T &u){
1.103 + _x /= u;
1.104 + _y /= u;
1.105 + return *this;
1.106 + };
1.107
1.108 - ///Returns the scalar product of two vectors
1.109 - T operator *(const xy<T>& u){
1.110 - return _x*u._x+_y*u._y;
1.111 - };
1.112 + ///Returns the scalar product of two vectors
1.113 + T operator *(const xy<T>& u){
1.114 + return _x*u._x+_y*u._y;
1.115 + };
1.116
1.117 - ///Returns the sum of two vectors
1.118 - xy<T> operator+(const xy<T> &u) const {
1.119 - xy<T> b=*this;
1.120 - return b+=u;
1.121 - };
1.122 + ///Returns the sum of two vectors
1.123 + xy<T> operator+(const xy<T> &u) const {
1.124 + xy<T> b=*this;
1.125 + return b+=u;
1.126 + };
1.127
1.128 - ///Returns the difference of two vectors
1.129 - xy<T> operator-(const xy<T> &u) const {
1.130 - xy<T> b=*this;
1.131 - return b-=u;
1.132 - };
1.133 + ///Returns the difference of two vectors
1.134 + xy<T> operator-(const xy<T> &u) const {
1.135 + xy<T> b=*this;
1.136 + return b-=u;
1.137 + };
1.138
1.139 - ///Returns a vector multiplied by a scalar
1.140 - xy<T> operator*(const T &u) const {
1.141 - xy<T> b=*this;
1.142 - return b*=u;
1.143 - };
1.144 + ///Returns a vector multiplied by a scalar
1.145 + xy<T> operator*(const T &u) const {
1.146 + xy<T> b=*this;
1.147 + return b*=u;
1.148 + };
1.149
1.150 - ///Testing equality
1.151 - bool operator==(const xy<T> &u){
1.152 - return (_x==u._x) && (_y==u._y);
1.153 - };
1.154 + ///Returns a vector divided by a scalar
1.155 + xy<T> operator/(const T &u) const {
1.156 + xy<T> b=*this;
1.157 + return b/=u;
1.158 + };
1.159
1.160 - ///Testing inequality
1.161 - bool operator!=(xy u){
1.162 - return (_x!=u._x) || (_y!=u._y);
1.163 - };
1.164 + ///Testing equality
1.165 + bool operator==(const xy<T> &u){
1.166 + return (_x==u._x) && (_y==u._y);
1.167 + };
1.168
1.169 -};
1.170 -///Reading a plainvector from a stream
1.171 -template<typename T>
1.172 -inline
1.173 -istream& operator>>(istream &is, xy<T> &z)
1.174 -{
1.175 - ///This is not the best solution here: I didn't know how to solve this with friend functions
1.176 - T a,b;
1.177 - is >> a >> b;
1.178 - xy<T> buf(a,b);
1.179 - z=buf;
1.180 - return is;
1.181 -}
1.182 + ///Testing inequality
1.183 + bool operator!=(xy u){
1.184 + return (_x!=u._x) || (_y!=u._y);
1.185 + };
1.186
1.187 -///Outputting a plainvector to a stream
1.188 -template<typename T>
1.189 -inline
1.190 -ostream& operator<<(ostream &os, xy<T> z)
1.191 -{
1.192 - os << "(" << z.x() << ", " << z.y() << ")";
1.193 - return os;
1.194 -}
1.195 + };
1.196
1.197 + ///Reading a plainvector from a stream
1.198 + template<typename T>
1.199 + inline
1.200 + std::istream& operator>>(std::istream &is, xy<T> &z)
1.201 + {
1.202 + ///This is not the best solution here: I didn't know how to solve this with friend functions
1.203 + T a,b;
1.204 + is >> a >> b;
1.205 + xy<T> buf(a,b);
1.206 + z=buf;
1.207 + return is;
1.208 + }
1.209
1.210 + ///Outputting a plainvector to a stream
1.211 + template<typename T>
1.212 + inline
1.213 + std::ostream& operator<<(std::ostream &os, xy<T> z)
1.214 + {
1.215 + os << "(" << z.x() << ", " << z.y() << ")";
1.216 + return os;
1.217 + }
1.218 +
1.219 +} //namespace hugo
1.220
1.221 #endif //HUGO_XY_H