![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsVector2.cpp 00003 ******************************************************************************/ 00007 /****************************************************************************** 00008 SUKITTI PUNAK (09/09/2004) 00009 UPDATE (08/27/2010) 00010 ******************************************************************************/ 00011 #include "TAPsVector2.hpp" 00012 // Using Inclusion Model (i.e. definitions are included in declarations) 00013 // (this name.cpp is included in name.hpp) 00014 // Each friend is defined directly inside its declaration. 00015 00016 BEGIN_NAMESPACE_TAPs 00017 //============================================================================= 00018 // Constructors 00019 //----------------------------------------------------------------------------- 00020 template <typename T> 00021 TAPs::Vector2<T>::Vector2 () 00022 { this->t[0] = this->t[1] = TAPs::Math<T>::ZERO; } 00023 //----------------------------------------------------------------------------- 00024 template <typename T> 00025 TAPs::Vector2<T>::Vector2 ( T const af[2] ) 00026 { this->t[0] = af[0]; this->t[1] = af[1]; } 00027 //----------------------------------------------------------------------------- 00028 template <typename T> 00029 TAPs::Vector2<T>::Vector2 ( TAPs::Vector2<T> const &v ) 00030 { this->t[0] = v.t[0]; this->t[1] = v.t[1]; } 00031 //----------------------------------------------------------------------------- 00032 template <typename T> 00033 TAPs::Vector2<T>::Vector2 ( T x, T y ) 00034 { this->t[0] = x; this->t[1] = y; } 00035 //----------------------------------------------------------------------------- 00036 //============================================================================= 00037 // Assignment Operator 00038 //----------------------------------------------------------------------------- 00039 template <typename T> 00040 TAPs::Vector2<T> & TAPs::Vector2<T>::operator= ( TAPs::Vector2<T> const &v ) 00041 { 00042 this->t[0] = v.t[0]; 00043 this->t[1] = v.t[1]; 00044 return *this; 00045 } 00046 //============================================================================= 00047 // Comparison Operators 00048 //----------------------------------------------------------------------------- 00049 template <typename T> 00050 bool TAPs::Vector2<T>::operator== ( TAPs::Vector2<T> const &v ) const 00051 { return ( this->t[0] == v.t[0] && this->t[1] == v.t[1] ); } 00052 //----------------------------------------------------------------------------- 00053 template <typename T> 00054 bool TAPs::Vector2<T>::operator!= ( TAPs::Vector2<T> const &v ) const 00055 { return !( *this == v ); } 00056 //----------------------------------------------------------------------------- 00057 template <typename T> 00058 bool TAPs::Vector2<T>::operator<= ( TAPs::Vector2<T> const &v ) const 00059 { return ( this->t[0] <= v.t[0] && this->t[1] <= v.t[1] ); } 00060 //----------------------------------------------------------------------------- 00061 template <typename T> 00062 bool TAPs::Vector2<T>::operator> ( TAPs::Vector2<T> const &v ) const 00063 { return !( *this <= v ); } 00064 //----------------------------------------------------------------------------- 00065 template <typename T> 00066 bool TAPs::Vector2<T>::operator>= ( TAPs::Vector2<T> const &v ) const 00067 { return ( this->t[0] >= v.t[0] && this->t[1] >= v.t[1] ); } 00068 //----------------------------------------------------------------------------- 00069 template <typename T> 00070 bool TAPs::Vector2<T>::operator< ( TAPs::Vector2<T> const &v ) const 00071 { return !( *this >= v ); } 00072 //----------------------------------------------------------------------------- 00073 //============================================================================= 00074 // Arithmetic Operations 00075 //----------------------------------------------------------------------------- 00076 template <typename T> 00077 TAPs::Vector2<T> TAPs::Vector2<T>::operator- () const 00078 { return TAPs::Vector2<T>( -(this->t[0]), -(this->t[1]) ); } 00079 //----------------------------------------------------------------------------- 00080 template <typename T> 00081 TAPs::Vector2<T> TAPs::Vector2<T>::operator+ ( TAPs::Vector2<T> const &v ) const 00082 { return TAPs::Vector2<T>( this->t[0]+v.t[0], this->t[1]+v.t[1] ); } 00083 //----------------------------------------------------------------------------- 00084 template <typename T> 00085 TAPs::Vector2<T> TAPs::Vector2<T>::operator- ( TAPs::Vector2<T> const &v ) const 00086 { return TAPs::Vector2<T>( this->t[0]-v.t[0], this->t[1]-v.t[1] ); } 00087 //----------------------------------------------------------------------------- 00088 template <typename T> 00089 TAPs::Vector2<T> TAPs::Vector2<T>::operator* ( T s ) const 00090 { return TAPs::Vector2<T>( this->t[0]*s, this->t[1]*s ); } 00091 //----------------------------------------------------------------------------- 00092 template <typename T> 00093 TAPs::Vector2<T> TAPs::Vector2<T>::operator/ ( T s ) const 00094 { 00095 if ( s != TAPs::Math<T>::ZERO ) 00096 return TAPs::Vector2<T>( this->t[0]/s, this->t[1]/s ); 00097 else 00098 return TAPs::Vector2<T>( TAPs::Math<T>::INFINITY, TAPs::Math<T>::INFINITY ); 00099 } 00100 //----------------------------------------------------------------------------- 00101 //============================================================================= 00102 // Arithmetic Update Operators 00103 //----------------------------------------------------------------------------- 00104 template <typename T> 00105 TAPs::Vector2<T> & TAPs::Vector2<T>::operator+= ( TAPs::Vector2<T> const &v ) 00106 { 00107 this->t[0] += v.t[0]; 00108 this->t[1] += v.t[1]; 00109 return *this; 00110 } 00111 //----------------------------------------------------------------------------- 00112 template <typename T> 00113 TAPs::Vector2<T> & TAPs::Vector2<T>::operator-= ( TAPs::Vector2<T> const &v ) 00114 { 00115 this->t[0] -= v.t[0]; 00116 this->t[1] -= v.t[1]; 00117 return *this; 00118 } 00119 //----------------------------------------------------------------------------- 00120 template <typename T> 00121 TAPs::Vector2<T> & TAPs::Vector2<T>::operator*= ( T s ) 00122 { 00123 this->t[0] *= s; 00124 this->t[1] *= s; 00125 return *this; 00126 } 00127 //----------------------------------------------------------------------------- 00128 template <typename T> 00129 TAPs::Vector2<T> & TAPs::Vector2<T>::operator/= ( T s ) 00130 { 00131 if ( s != TAPs::Math<T>::ZERO ) 00132 { 00133 this->t[0] /= s; 00134 this->t[1] /= s; 00135 } 00136 else 00137 { 00138 this->t[0] = TAPs::Math<T>::INFINITY; 00139 this->t[1] = TAPs::Math<T>::INFINITY; 00140 } 00141 return *this; 00142 } 00143 //----------------------------------------------------------------------------- 00144 //============================================================================= 00145 // Vector Operations 00146 //----------------------------------------------------------------------------- 00147 template <typename T> 00148 T TAPs::Vector2<T>::Length () const 00149 { return TAPs::Math<T>::Sqrt( (this->t[0])*(this->t[0]) + (this->t[1])*(this->t[1]) ); } 00150 //----------------------------------------------------------------------------- 00151 template <typename T> 00152 T TAPs::Vector2<T>::Magnitude () const 00153 { return TAPs::Math<T>::Sqrt( (this->t[0])*(this->t[0]) + (this->t[1])*(this->t[1]) ); } 00154 //----------------------------------------------------------------------------- 00155 template <typename T> 00156 T TAPs::Vector2<T>::SquaredLength () const 00157 { return ( (this->t[0])*(this->t[0]) + (this->t[1])*(this->t[1]) ); } 00158 //----------------------------------------------------------------------------- 00159 template <typename T> 00160 TAPs::Vector2<T> TAPs::Vector2<T>::GetUnit ( T tolerance ) const 00161 { 00162 T fLength = TAPs::Vector2<T>::Length(); 00163 if ( fLength > tolerance ) 00164 return TAPs::Vector2<T>( this->t[0]/fLength, this->t[1]/fLength ); 00165 else 00166 return TAPs::Vector2<T>( TAPs::Math<T>::ZERO, TAPs::Math<T>::ZERO ); 00167 } 00168 //----------------------------------------------------------------------------- 00169 template <typename T> 00170 TAPs::Vector2<T> & TAPs::Vector2<T>::Normalized ( T tolerance ) 00171 { 00172 T fLength = TAPs::Vector2<T>::Length(); 00173 if ( fLength > tolerance ) 00174 { 00175 this->t[0] /= fLength; 00176 this->t[1] /= fLength; 00177 } 00178 else 00179 { 00180 this->t[0] = TAPs::Math<T>::ZERO; 00181 this->t[1] = TAPs::Math<T>::ZERO; 00182 } 00183 return *this; 00184 } 00185 //----------------------------------------------------------------------------- 00186 template <typename T> 00187 T TAPs::Vector2<T>::Dot ( Vector2<T> const &v ) const 00188 { return this->t[0] * v.t[0] + this->t[1] * v.t[1]; } 00189 //----------------------------------------------------------------------------- 00190 template <typename T> 00191 T TAPs::Vector2<T>::operator* ( Vector2<T> const &v ) const 00192 { return this->Dot(v); } 00193 //----------------------------------------------------------------------------- 00194 template <typename T> 00195 Vector2<T> Vector2<T>::ComponentWiseMult ( Vector2<T> const &v ) const 00196 { 00197 Vector2<T> R; 00198 R.t[0] = t[0] * v.t[0]; 00199 R.t[1] = t[1] * v.t[1]; 00200 return R; 00201 } 00202 //----------------------------------------------------------------------------- 00203 template <typename T> 00204 Vector2<T> Vector2<T>::ComponentWiseDivs ( Vector2<T> const &v ) const 00205 { 00206 Vector2<T> R; 00207 R.t[0] = t[0] / v.t[0]; 00208 R.t[1] = t[1] / v.t[1]; 00209 return R; 00210 } 00211 //----------------------------------------------------------------------------- 00212 template <typename T> 00213 void Vector2<T>::UpdateByComponentWiseMult ( Vector2<T> const &v ) 00214 { 00215 t[0] *= v.t[0]; 00216 t[1] *= v.t[1]; 00217 } 00218 //----------------------------------------------------------------------------- 00219 template <typename T> 00220 void Vector2<T>::UpdateByComponentWiseDivs ( Vector2<T> const &v ) 00221 { 00222 t[0] /= v.t[0]; 00223 t[1] /= v.t[1]; 00224 } 00225 //----------------------------------------------------------------------------- 00226 //============================================================================= 00227 // Vector * ColMatrix 00228 //----------------------------------------------------------------------------- 00229 template <typename T> 00230 TAPs::Vector2<T> TAPs::Vector2<T>::operator* ( ColMatrix2x2<T> const &M ) const 00231 { 00232 return Vector2<T>( 00233 this->t[0]*M[0] + this->t[1]*M[1], 00234 this->t[0]*M[2] + this->t[1]*M[3] ); 00235 } 00236 //----------------------------------------------------------------------------- 00237 END_NAMESPACE_TAPs 00238 //============================================================================= 00239 //345678901234567890123456789012345678901234567890123456789012345678901234567890 00240 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8