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

Generated on Fri Dec 23 05:15:48 2005 for Constructive Solid Geometry by doxygen1.2.15