00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 #ifndef __OSL_VECTOR_2D_H
00018 #define __OSL_VECTOR_2D_H
00019 
00020 #include <math.h>
00021 
00022 typedef double real;
00023 
00024 
00025 class vector2d {
00026 public:
00027     real x,y;
00028     vector2d(void) {}
00029     
00030     explicit vector2d(const real init) {x=y=init;}
00031     
00032     explicit vector2d(int init) {x=y=init;}
00033     
00034     vector2d(const real Nx,const real Ny) {x=Nx;y=Ny;}
00035     
00036     vector2d(const vector2d ©) {x=copy.x;y=copy.y;}
00037     
00038     
00039     operator real *() {return &x;}
00040     operator const real *() const {return &x;}
00041     
00042 
00043 
00044 
00045 
00046 
00047 
00048 
00049     vector2d &operator=(const vector2d &b) {x=b.x;y=b.y;return *this;}
00050     int operator==(const vector2d &b) const {return (x==b.x)&&(y==b.y);}
00051     int operator!=(const vector2d &b) const {return (x!=b.x)||(y!=b.y);}
00052     vector2d operator+(const vector2d &b) const {return vector2d(x+b.x,y+b.y);}
00053     vector2d operator-(const vector2d &b) const {return vector2d(x-b.x,y-b.y);}
00054     vector2d operator*(const real scale) const 
00055         {return vector2d(x*scale,y*scale);}
00056     friend vector2d operator*(const real scale,const vector2d &v)
00057         {return vector2d(v.x*scale,v.y*scale);}
00058     vector2d operator/(const real &div) const
00059         {real scale=1.0/div;return vector2d(x*scale,y*scale);}
00060     vector2d operator-(void) const {return vector2d(-x,-y);}
00061     void operator+=(const vector2d &b) {x+=b.x;y+=b.y;}
00062     void operator-=(const vector2d &b) {x-=b.x;y-=b.y;}
00063     void operator*=(const real scale) {x*=scale;y*=scale;}
00064     void operator/=(const real div) {real scale=1.0/div;x*=scale;y*=scale;}
00065 
00066 
00067     
00068     real magSqr(void) const {return x*x+y*y;}
00069     
00070     real mag(void) const {return sqrt(magSqr());}
00071     
00072     
00073     real distSqr(const vector2d &b) const 
00074         {return (x-b.x)*(x-b.x)+(y-b.y)*(y-b.y);}
00075     
00076     real dist(const vector2d &b) const {return sqrt(distSqr(b));}
00077     
00078     
00079     real dot(const vector2d &b) const {return x*b.x+y*b.y;}
00080     
00081     real cosAng(const vector2d &b) const {return dot(b)/(mag()*b.mag());}
00082     
00083     
00084     vector2d dir(void) const {return (*this)/mag();}
00085 
00086     
00087     vector2d perp(void) const {return vector2d(-y,x);}
00088 
00089     
00090     vector2d &scale(const vector2d &b) {x*=b.x;y*=b.y;return *this;}
00091     
00092     
00093     real max(void) {return (x>y)?x:y;}
00094     
00095     
00096     void enlarge(const vector2d &by)
00097     {if (by.x>x) x=by.x; if (by.y>y) y=by.y;}
00098 };
00099 
00100 #endif //__OSL_VECTOR2D_H
00101 
00102