Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

Vector2.h

Go to the documentation of this file.
00001 #ifndef VECTOR2_H
00002 #define VECTOR2_H
00003 
00004 #include "ThyrixParameters.h"
00005 
00006 
00007 #include <assert.h>
00008 
00011 class Vector2 {
00012 
00013 public:
00014    
00016    Vector2 ()                          { x=0.0; y=0.0; }
00017    
00019    Vector2 (real x, real y)            { this->x=x; this->y=y; }
00020    
00022    virtual ~Vector2 ()                 {}
00023 
00024    real x;
00025    real y;
00026 
00028    void setToZero()                    { x=0.0; y=0.0; }
00029 
00031    void setXY(real x, real y)          { this->x=x; this->y=y; }
00032 
00034    void setElement(int i, real value)  {assert(i>=1 && i<=2);  i==1? x=value :   y=value; }
00035 
00037    real getModule()                    { 
00038       double ax=fabs(x), ay=fabs(y);
00039       if(ax>ay) return ax*sqrt(1.0+sqr(ay/ax));
00040       else return (ay==0.0 ? 0.0 : ay*sqrt(1.0+sqr(ax/ay)));
00041    }
00042 
00044    real getSquaredModule()             { return x*x+y*y; }
00045 
00047    void normalize() { 
00048       real module=getModule(); 
00049       assert(module!=0);
00050       x/=module; y/=module;
00051    }
00052 
00054    void updateMin(const Vector2& r){
00055       if(r.x<x) x=r.x;
00056       if(r.y<y) y=r.y;
00057    }
00058 
00060    void updateMax(const Vector2& r){
00061       if(r.x>x) x=r.x;
00062       if(r.y>y) y=r.y;
00063    }
00064 
00065    
00069    void rotate(real alpha) {
00070       real xOld=x;
00071       real c=cos(alpha);
00072       real s=sin(alpha);
00073       x=x*c-y*s;
00074       y=xOld*s+y*c;
00075    }
00076 
00078    void cross(real w){
00079       real xOld=x;
00080       x=-w*y;
00081       y=w*xOld;
00082    }
00083 
00084    
00086    real operator[](int i){
00087       assert(i>=0 && i<=1);
00088       if (i==0)
00089          return x;
00090       else return y;
00091    }
00092 
00093    Vector2 operator+(const Vector2 &v) const {
00094       return Vector2(x + v.x, y + v.y);
00095    }
00096 
00097    Vector2 operator-(const Vector2 &v) const {
00098          return Vector2(x - v.x, y - v.y);
00099    }
00100 
00101    Vector2 operator*(const real r) const {
00102          return Vector2(x * r, y * r);
00103    }
00104 
00105    Vector2& operator+=(const Vector2& v)  { x+=v.x; y+=v.y; return *this; }
00106    Vector2& operator-=(const Vector2& v)  { x-=v.x; y-=v.y; return *this; }
00107    Vector2& operator+=(const real r)      { x+=r; y+=r; return *this; }
00108    Vector2& operator-=(const real r)      { x-=r; y-=r; return *this; }
00109    Vector2& operator*=(const real r)      { x*=r; y*=r; return *this; }
00110    Vector2& operator/=(const real r)      { assert(r!=0.0); x/=r; y/=r; return *this; }
00112    real operator*(Vector2 v)              { return x*v.x+y*v.y; }
00114    real operator^(Vector2 v)              { return x*v.y-y*v.x; }
00115 };
00116 
00117 
00118 #endif //VECTOR2_H

Thyrix homepageUsers' guide

(C) Arxia 2004-2005