author | jacint |
Wed, 24 Mar 2004 09:36:21 +0000 | |
changeset 241 | 4acba8684811 |
parent 237 | 7fb8b67d2c5e |
child 242 | b255f25ad394 |
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@201 | 15 |
|
athos@207 | 16 |
public: |
athos@240 | 17 |
|
athos@240 | 18 |
T x,y; |
athos@207 | 19 |
|
athos@207 | 20 |
///Default constructor: both coordinates become 0 |
athos@240 | 21 |
xy() : x(0), y(0) {} |
athos@201 | 22 |
|
athos@240 | 23 |
///Constructing the instance from coordinates |
athos@240 | 24 |
xy(T a, T b) : x(a), y(a) { } |
athos@201 | 25 |
|
athos@201 | 26 |
|
athos@207 | 27 |
///Gives back the square of the norm of the vector |
athos@207 | 28 |
T normSquare(){ |
athos@240 | 29 |
return x*x+y*y; |
athos@207 | 30 |
}; |
athos@201 | 31 |
|
athos@207 | 32 |
///Increments the left hand side by u |
athos@207 | 33 |
xy<T>& operator +=(const xy<T>& u){ |
athos@240 | 34 |
x += u.x; |
athos@240 | 35 |
y += u.y; |
athos@207 | 36 |
return *this; |
athos@207 | 37 |
}; |
athos@201 | 38 |
|
athos@207 | 39 |
///Decrements the left hand side by u |
athos@207 | 40 |
xy<T>& operator -=(const xy<T>& u){ |
athos@240 | 41 |
x -= u.x; |
athos@240 | 42 |
y -= u.y; |
athos@207 | 43 |
return *this; |
athos@207 | 44 |
}; |
athos@201 | 45 |
|
athos@207 | 46 |
///Multiplying the left hand side with a scalar |
athos@207 | 47 |
xy<T>& operator *=(const T &u){ |
athos@240 | 48 |
x *= u; |
athos@240 | 49 |
y *= u; |
athos@207 | 50 |
return *this; |
athos@207 | 51 |
}; |
athos@207 | 52 |
|
athos@207 | 53 |
///Dividing the left hand side by a scalar |
athos@207 | 54 |
xy<T>& operator /=(const T &u){ |
athos@240 | 55 |
x /= u; |
athos@240 | 56 |
y /= u; |
athos@207 | 57 |
return *this; |
athos@207 | 58 |
}; |
athos@201 | 59 |
|
athos@207 | 60 |
///Returns the scalar product of two vectors |
athos@207 | 61 |
T operator *(const xy<T>& u){ |
athos@240 | 62 |
return x*u.x+y*u.y; |
athos@207 | 63 |
}; |
athos@201 | 64 |
|
athos@207 | 65 |
///Returns the sum of two vectors |
athos@207 | 66 |
xy<T> operator+(const xy<T> &u) const { |
athos@207 | 67 |
xy<T> b=*this; |
athos@207 | 68 |
return b+=u; |
athos@207 | 69 |
}; |
athos@201 | 70 |
|
athos@207 | 71 |
///Returns the difference of two vectors |
athos@207 | 72 |
xy<T> operator-(const xy<T> &u) const { |
athos@207 | 73 |
xy<T> b=*this; |
athos@207 | 74 |
return b-=u; |
athos@207 | 75 |
}; |
athos@201 | 76 |
|
athos@207 | 77 |
///Returns a vector multiplied by a scalar |
athos@207 | 78 |
xy<T> operator*(const T &u) const { |
athos@207 | 79 |
xy<T> b=*this; |
athos@207 | 80 |
return b*=u; |
athos@207 | 81 |
}; |
athos@201 | 82 |
|
athos@207 | 83 |
///Returns a vector divided by a scalar |
athos@207 | 84 |
xy<T> operator/(const T &u) const { |
athos@207 | 85 |
xy<T> b=*this; |
athos@207 | 86 |
return b/=u; |
athos@207 | 87 |
}; |
athos@201 | 88 |
|
athos@207 | 89 |
///Testing equality |
athos@207 | 90 |
bool operator==(const xy<T> &u){ |
athos@240 | 91 |
return (x==u.x) && (y==u.y); |
athos@207 | 92 |
}; |
athos@201 | 93 |
|
athos@207 | 94 |
///Testing inequality |
athos@207 | 95 |
bool operator!=(xy u){ |
athos@240 | 96 |
return (x!=u.x) || (y!=u.y); |
athos@207 | 97 |
}; |
athos@201 | 98 |
|
athos@207 | 99 |
}; |
athos@201 | 100 |
|
athos@207 | 101 |
///Reading a plainvector from a stream |
athos@207 | 102 |
template<typename T> |
athos@207 | 103 |
inline |
athos@207 | 104 |
std::istream& operator>>(std::istream &is, xy<T> &z) |
athos@207 | 105 |
{ |
athos@240 | 106 |
|
athos@240 | 107 |
is >> z.x >> z.y; |
athos@207 | 108 |
return is; |
athos@207 | 109 |
} |
athos@201 | 110 |
|
athos@207 | 111 |
///Outputting a plainvector to a stream |
athos@207 | 112 |
template<typename T> |
athos@207 | 113 |
inline |
athos@207 | 114 |
std::ostream& operator<<(std::ostream &os, xy<T> z) |
athos@207 | 115 |
{ |
athos@240 | 116 |
os << "(" << z.x << ", " << z.y << ")"; |
athos@207 | 117 |
return os; |
athos@207 | 118 |
} |
athos@207 | 119 |
|
athos@207 | 120 |
} //namespace hugo |
athos@201 | 121 |
|
athos@201 | 122 |
#endif //HUGO_XY_H |