Al's Programming Resource Homepage  Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

vector.h

Go to the documentation of this file.
00001 // Vector Class    by Alan Baylis 2001
00002 
00003 #ifndef VectorH
00004 #define VectorH
00005 
00006 // A floating point number
00007 typedef float SCALAR;
00008 
00009 class VECTOR
00010 {
00011     public:
00012            VECTOR(float sx = 0, float sy = 0, float sz = 0);
00013         VECTOR(const VECTOR& Vector);
00014           ~VECTOR();
00015 
00016         float GetMagnitude();
00017         void Normalize();
00018         void Reset();
00019           void Set(float sx, float sy, float sz) {x = sx, y = sy, z = sz;}
00020         void CrossVector(VECTOR vect);
00021         float DotProduct(VECTOR vect);
00022 
00023         //equal within an error ‘e’
00024         const bool nearlyEquals( const VECTOR& v, const SCALAR e ) const
00025         {
00026             return fabs(x-v.x)<e && fabs(y-v.y)<e && fabs(z-v.z)<e;
00027         }
00028 
00029         //cross product
00030         const VECTOR cross( const VECTOR& v ) const
00031         {
00032             //Davis, Snider, "Introduction to Vector Analysis", p. 44
00033             return VECTOR( y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x );
00034         }
00035 
00036         //scalar dot product
00037         const SCALAR dot( const VECTOR& v ) const
00038         {
00039             return x*v.x + y*v.y + z*v.z;
00040         }
00041 
00042         //length
00043         const SCALAR length() const
00044         {
00045             return (SCALAR)sqrt( (double)this->dot(*this) );
00046         }
00047 
00048         //unit vector
00049         const VECTOR unit() const
00050         {
00051             return (*this) / length();
00052         }
00053 
00054         //make this a unit vector
00055         void normalize()
00056         {
00057             (*this) /= length();
00058         }
00059 
00060         //Members
00061           float x;
00062         float y;
00063         float z;
00064 
00065         //index a component
00066         //NOTE: returning a reference allows
00067         //you to assign the indexed element
00068         SCALAR& operator [] ( const long i )
00069         {
00070             return *((&x) + i);
00071         }
00072 
00073         //compare
00074         const bool operator == ( const VECTOR& v ) const
00075         {
00076             return (v.x==x && v.y==y && v.z==z);
00077         }
00078 
00079         const bool operator != ( const VECTOR& v ) const
00080         {
00081             return !(v == *this);
00082         }
00083 
00084         //negate
00085         const VECTOR operator - () const
00086         {
00087             return VECTOR( -x, -y, -z );
00088         }
00089 
00090         //assign
00091         const VECTOR& operator = ( const VECTOR& v )
00092         {
00093             x = v.x;
00094             y = v.y;
00095             z = v.z;
00096             return *this;
00097         }
00098 
00099         //increment
00100         const VECTOR& operator += ( const VECTOR& v )
00101         {
00102             x+=v.x;
00103             y+=v.y;
00104             z+=v.z;
00105             return *this;
00106         }
00107 
00108         //decrement
00109         const VECTOR& operator -= ( const VECTOR& v )
00110         {
00111             x-=v.x;
00112             y-=v.y;
00113             z-=v.z;
00114             return *this;
00115         }
00116 
00117         //self-multiply
00118         const VECTOR& operator *= ( const SCALAR& s )
00119         {
00120             x*=s;
00121             y*=s;
00122             z*=s;
00123             return *this;
00124         }
00125 
00126         //self-divide
00127         const VECTOR& operator /= ( const SCALAR& s )
00128         {
00129             const SCALAR r = 1 / s;
00130             x *= r;
00131             y *= r;
00132             z *= r;
00133             return *this;
00134         }
00135 
00136         //add
00137         const VECTOR operator + ( const VECTOR& v ) const
00138         {
00139             return VECTOR(x + v.x, y + v.y, z + v.z);
00140         }
00141 
00142         //subtract
00143         const VECTOR operator - ( const VECTOR& v ) const
00144         {
00145             return VECTOR(x - v.x, y - v.y, z - v.z);
00146         }
00147 
00148         //post-multiply by a scalar
00149         const VECTOR operator * ( const SCALAR& s ) const
00150         {
00151             return VECTOR( x*s, y*s, z*s );
00152         }
00153 
00154         //pre-multiply by a scalar
00155         friend inline const VECTOR operator * ( const SCALAR& s, const VECTOR& v )
00156         {
00157             return v * s;
00158         }
00159 
00160         //divide
00161         const VECTOR operator / (SCALAR s) const
00162         {
00163             s = 1/s;
00164             return VECTOR( s*x, s*y, s*z );
00165         }
00166 };
00167 
00168 #endif
00169 

Generated on Fri Dec 23 05:20:59 2005 for Polygon Selection by doxygen1.2.15