00001 #ifndef Vector2_h 00002 #define Vector2_h 1 00003 00004 #include <assert.h> 00005 #include <iostream> 00006 #include "math2.h" 00007 00008 00009 class Vector2 00010 { 00011 00012 public: 00013 //## Constructors (specified) 00014 //## Operation: Vector2%922649209 00015 Vector2 (); 00016 00017 //## Operation: Vector2%922649210 00018 Vector2 (const Vector2& right); 00019 00020 //## Operation: Vector2%925235856 00021 Vector2 (const double x, const double y ); 00022 00023 //## Destructor (generated) 00024 ~Vector2(); 00025 00026 00027 //## Other Operations (specified) 00028 //## Operation: operator[]%922649204 00029 double& operator [] (const unsigned int index); 00030 00031 //## Operation: operator+%922649205 00032 Vector2 operator + (const Vector2& right) const; 00033 00034 //## Operation: operator-%922649206 00035 Vector2 operator - (const Vector2& right) const; 00036 00037 Vector2& operator -= (const Vector2& right); 00038 00039 //## Operation: operator*%922649207 00040 Vector2 operator * (const double right) const; 00041 00042 //## Operation: operator*%922649207 00043 Vector2 operator / (const double right) const; 00044 00045 //## Operation: operator[]%922649208 00046 const double operator [] (const unsigned int index) const; 00047 00048 //## Operation: Magnitude%925235851 00049 // returns the length of the vector 00050 double Magnitude () const; 00051 00052 //## Operation: MagSquared%925235852 00053 // this returns the square of the magnitude of the vector 00054 double MagSquared () const; 00055 00056 //## Operation: Dot%926709626 00057 double Dot (const Vector2& right) const; 00058 00059 //## Operation: Cross%926806998 00060 Vector2 Cross (const Vector2& right) const; 00061 00062 //## Operation: Projection%926806999 00063 Vector2 Projection (const Vector2& right) const; 00064 00065 //## Operation: ProjectionMag%926807000 00066 double ProjectionMag (const Vector2& right) const; 00067 00068 //reflects this vector about a normal 00069 void Reflect( const Vector2& normal ); 00070 00071 //## Operation: operator==%926807001 00072 bool operator == (const Vector2& right) const; 00073 00074 bool operator< ( const Vector2& right ) const; 00075 00076 //## Operation: Normalize%927307381 00077 void Normalize (); 00078 00079 // Compares if two vectors are equal to within a given Tolerance. 00080 bool Compare( const Vector2& right, const double& tol) const; 00081 00082 00083 // Additional Public Declarations 00084 //## begin Vector2%36FEE75503CA.public preserve=yes 00085 bool operator !=( const Vector2& right ) const; 00086 Vector2& operator +=( const Vector2& right ); 00087 Vector2& operator *=( const double right ); 00088 Vector2& operator /=( const double right ); 00089 Vector2 operator -() const; 00090 //## end Vector2%36FEE75503CA.public 00091 00092 protected: 00093 double elements[ 2 ]; 00094 00095 private: 00096 00097 private: //## implementation 00098 00099 }; 00100 00101 //std::ostream & operator<<( std::ostream &os, const Vector2& v ); 00102 00103 inline double& Vector2::operator [] (const unsigned int index) 00104 { 00105 IJG_Assert( index < 2 ); 00106 IJG_Assert( index >= 0 ); 00107 return elements[ index ]; 00108 } 00109 00110 inline const double Vector2::operator [] (const unsigned int index) const 00111 { 00112 IJG_Assert( index < 2 ); 00113 IJG_Assert( index >= 0 ); 00114 return elements[ index ]; 00115 } 00116 00117 //============================================================================= 00118 // operator += 00119 // 00120 // Description: in place addition operator 00121 //============================================================================= 00122 inline Vector2& Vector2::operator +=( const Vector2& right ) 00123 { 00124 elements[ 0 ] += right.elements[ 0 ]; 00125 elements[ 1 ] += right.elements[ 1 ]; 00126 return *this; 00127 } 00128 00129 //============================================================================= 00130 // Constructor 00131 // 00132 // Description: copies x,y,z into the vector 00133 //============================================================================= 00134 inline Vector2::Vector2( const double x, const double y ) 00135 { 00136 elements[ 0 ] = x ; 00137 elements[ 1 ] = y ; 00138 } 00139 00140 //============================================================================= 00141 // Destructor 00142 // 00143 // Description: does nothign 00144 //============================================================================= 00145 inline Vector2::~Vector2() 00146 { 00147 } 00148 00149 00150 std::ostream & operator<<( std::ostream &os, const Vector2& v ) ; 00151 std::istream & operator>>( std::istream &is, Vector2& v ) ; 00152 00153 //============================================================================= 00154 // Verify 00155 // 00156 // Description: verifies that the vector is within valid ranges 00157 //============================================================================= 00158 #ifdef _DEBUG 00159 inline void Verify( const Vector2& v ) 00160 { 00161 assert( Between( v[ 0 ], -10000.0, 10000.0 ) ); //+-10000 is rather arbitrary 00162 assert( Between( v[ 1 ], -10000.0, 10000.0 ) ); //+-10000 is rather arbitrary 00163 } 00164 #else 00165 inline void Verify( const Vector2& v ) 00166 { 00167 //nothing 00168 } 00169 #endif 00170 00171 #endif