src/tool/vector2.h

Go to the documentation of this file.
00001 #ifndef _VECTOR2_H
00002 #define _VECTOR2_H
00003 
00004 #include <math.h>
00005 #define VECTOR2_EPS_ZERO (0.05)
00006 
00010 template<class T> class Vector2
00011 {
00012         private:
00013                 static const double EPS_ZERO = 0.05;
00014 
00015         public:
00016                 T x, y;
00017 
00021                 inline Vector2(){
00022                         x = 0;
00023                         y = 0;
00024                 }
00025 
00032                 inline Vector2(T x, T y){
00033                         this->x = x;
00034                         this->y = y;
00035                 }
00036 
00040                 inline T GetX() const{
00041                         return x;
00042                 }
00043 
00047                 inline T GetY() const{
00048                         return y;
00049                 }
00050 
00054                 inline bool IsZero(T val) const{
00055                         return (val == 0 || val <= VECTOR2_EPS_ZERO) &&
00056                                (-val <= VECTOR2_EPS_ZERO);
00057                 }
00058 
00059                 // Comparators
00060 
00064                 inline bool operator==(const Vector2<T> &p2) const{
00065                         return IsZero(x - p2.x) && IsZero(y - p2.y);
00066                 }
00067 
00072                 inline bool operator!=(const Vector2<T> &p2) const{
00073                         return (x != p2.x) || (y != p2.y);
00074                 }
00075 
00080                 inline bool operator>=(const Vector2<T> &p2) const{
00081                         return (x >= p2.x) && (y >= p2.y);
00082                 }
00083 
00088                 inline bool operator<=(const Vector2<T> &p2) const{
00089                         return (x <= p2.x) && (y <= p2.y);
00090                 }
00091 
00092                 // Vector/Vector operations
00093 
00098                 inline Vector2<T> operator+(const Vector2<T> &p2) const{
00099                         return Vector2<T>(x + p2.x, y + p2.y);
00100                 }
00101 
00106                 inline Vector2<T> operator-(const Vector2<T> &p2) const{
00107                         return Vector2<T>(x - p2.x, y - p2.y);
00108                 }
00109 
00114                 inline Vector2<T> operator-() const{
00115                         return Vector2<T>(-x, -y);
00116                 }
00117 
00122                 inline Vector2<T> operator*(const Vector2<T> &p2) const{
00123                         return Vector2<T>(x * p2.x, y * p2.y);
00124                 }
00125 
00130                 inline Vector2<T> operator/(const Vector2<T> &p2) const{
00131                         return Vector2<T>(x / p2.x, y / p2.y);
00132                 }
00133 
00134                 inline Vector2<T> operator%(const Vector2<T> &p2) const{
00135                         return Vector2<T>(x % p2.x, y % p2.y);
00136                 }
00137 
00138                 // Vector/Scalar opertations
00139 
00144                 inline Vector2<T> operator+(const T val) const{
00145                         return Vector2<T>(x + val, y + val);
00146                 }
00147 
00152                 inline Vector2<T> operator-(const T val) const{
00153                         return Vector2<T>(x - val, y - val);
00154                 }
00155 
00159                 inline Vector2<T> operator*(const T val) const{
00160                         return Vector2<T>(x * val, y * val);
00161                 }
00162 
00167                 inline Vector2<T> operator/(const T val) const{
00168                         return Vector2<T>(x / val, y / val);
00169         }
00170 
00171 
00172                 // Operators on itself with a scalar
00173 
00178                 inline void operator+=(const T val){
00179                         x += val;
00180                         y += val;
00181                 }
00182 
00187                 inline void operator-=(const T val){
00188                         x -= val;
00189                         y += val;
00190                 }
00191 
00195                 inline void operator*=(const T val){
00196                         x *= val;
00197                         y *= val;
00198                 }
00199 
00200                 // Operators on itself with an other vector
00201 
00206                 inline void operator+=(const Vector2<T> &p2){
00207                         x += p2.x;
00208                         y += p2.y;
00209                 }
00210 
00214                 inline void operator-=(const Vector2<T> &p2){
00215                         x -= p2.x;
00216                         y -= p2.y;
00217                 }
00218 
00219                 // Special operators
00220 
00224                 inline Vector2<T> operator*(const Vector2<double> &p2){
00225                         Vector2<T> r;
00226 
00227                         r.x = (T)((double)x * p2.x);
00228                         r.y = (T)((double)y * p2.y);
00229 
00230                         return r;
00231                 }
00232 
00239                 inline Vector2<T> inf(const Vector2<T> &p2){
00240                         return Vector2<T>( x < p2.x ? 1:0,
00241                                         y < p2.y ? 1:0);
00242                 }
00243 
00251                 inline Vector2<T> min(const Vector2<T> &p2) const{
00252                         return Vector2<T>( x < p2.x? x:p2.x,
00253                                         y < p2.y? y:p2.y );
00254                 }
00255 
00260                 inline Vector2<T> max(const Vector2<T> &p2) const{
00261                         return Vector2<T>( x > p2.x? x:p2.x,
00262                                                y > p2.y? y:p2.y );
00263                 }
00264 
00265                 inline Vector2<T> clamp(const Vector2<T> &min, const Vector2<T> &max) const{
00266                         Vector2<T> r = *this;
00267                         r = r.max(min);
00268                         return r.min(max);
00269                 }
00270 
00275                 inline double Distance(const Vector2<T> p2) const{
00276                         double distPow2 = (p2.x-x)*(p2.x-x) + (p2.y-y)*(p2.y-y);
00277                         return sqrt( distPow2 );
00278                 }
00279 
00283                 double Norm() const{
00284                         return Distance( Vector2(0,0) );
00285                 }
00286 
00290                 void Clear(){
00291                         x = 0;
00292                         y = 0;
00293                 }
00294 
00298                 void SetValues( T xx, T yy ){
00299                         x = xx;
00300                         y = yy;
00301                 }
00302 
00306                 void SetValues( Vector2<T> v2){
00307                         x = v2.x;
00308                         y = v2.y;
00309                 }
00310 
00314                 inline bool IsXNull() const{
00315                         return IsZero( x );
00316                 }
00317 
00321                 inline bool IsYNull() const{
00322                         return IsZero( y );
00323                 }
00324 
00328                 bool IsNull() const{
00329                         return IsXNull() && IsYNull();
00330                 }
00331 
00342                 double ComputeAngle() const{
00343                   double angle;
00344 
00345                   if( !IsZero( x ) )
00346                     if( !IsZero( y ) ){
00347                       angle = atan(double(y)/double(x));
00348                       if( x < 0 )
00349                         if( y > 0 )
00350                           angle += M_PI;
00351                         else
00352                           angle -= M_PI;
00353                     }
00354                     else
00355                       if( x > 0)
00356                         angle = 0;
00357                       else
00358                         angle = M_PI;
00359                   else
00360                     if( y > 0 )
00361                       angle = M_PI_2;
00362                     else if(y < 0)
00363                       angle = -M_PI_2;
00364                     else
00365                       angle = 0;
00366 
00367                   return angle;
00368                 }
00369 
00374                 double ComputeAngle(const Vector2<T> v2) const{
00375                         Vector2<T> veq( v2.x - x, v2.y - y);
00376 
00377                         return veq.ComputeAngle();
00378                 }
00379 };
00380 
00381 #endif //_VECTOR2_H

Generated on Mon Jan 1 13:10:59 2007 for Wormux by  doxygen 1.4.7