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