math/Vector2.cpp

Go to the documentation of this file.
00001 #include "../additional/streams/mystreams.h"
00002 #include <assert.h>
00003 #include <math.h>
00004 
00005 #include "Vector2.h"
00006 
00007 Vector2::Vector2 ()
00008 {
00009         elements[ 0 ] = 0.0;
00010         elements[ 1 ] = 0.0;
00011 }
00012 
00013 Vector2::Vector2 (const Vector2& right)
00014 {
00015         elements[ 0 ] = right.elements[ 0 ];
00016         elements[ 1 ] = right.elements[ 1 ];
00017 }
00018 
00019 Vector2 Vector2::operator + (const Vector2& right) const
00020 {
00021         double x = elements[ 0 ] + right.elements[ 0 ];
00022         double y = elements[ 1 ] + right.elements[ 1 ];
00023         return Vector2( x, y );
00024 }
00025 
00026 Vector2 Vector2::operator - (const Vector2& right) const
00027 {
00028         //Vector2 returnme ;
00029         double x = elements[ 0 ] - right.elements[ 0 ];
00030         double y = elements[ 1 ] - right.elements[ 1 ];
00031         return Vector2( x, y );
00032 }
00033 
00034 Vector2& Vector2::operator -= (const Vector2& right)
00035 {
00036     elements[ 0 ] -= right[ 0 ];
00037     elements[ 1 ] -= right[ 1 ];
00038         return *this;
00039 }
00040 
00041 
00042 Vector2 Vector2::operator * (const double right) const
00043 {
00044         double x = elements[ 0 ] * right;
00045         double y = elements[ 1 ] * right;
00046         return Vector2( x, y );
00047 }
00048 
00049 Vector2 Vector2::operator / (const double right) const
00050 {
00051         double x = elements[ 0 ] / right;
00052         double y = elements[ 1 ] / right;
00053         return Vector2( x, y );
00054 }
00055 
00056 double Vector2::Magnitude () const
00057 {
00058         return sqrt( elements[ 0 ] * elements[ 0 ] + elements[ 1 ] * elements[ 1 ] ) ;
00059 }
00060 
00061 double Vector2::MagSquared () const
00062 {
00063         return elements[ 0 ] * elements[ 0 ] + elements[ 1 ] * elements[ 1 ];
00064 }
00065 
00066 double Vector2::Dot (const Vector2& right) const
00067 {
00068         double returnMe = 0;
00069     returnMe += elements[ 0 ] * right[ 0 ];
00070     returnMe += elements[ 1 ] * right[ 1 ];
00071     return returnMe;
00072 }
00073 
00074 Vector2 Vector2::Cross (const Vector2& right) const
00075 {
00076         const double& ax = elements[ 0 ] ;
00077         const double& ay = elements[ 1 ] ;
00078         const double& bx = right.elements[ 0 ] ;
00079         const double& by = right.elements[ 1 ] ;
00080 
00081         Vector2 returnMe ;
00082         return returnMe ;
00083 }
00084 
00085 Vector2 Vector2::Projection (const Vector2& right) const
00086 {
00087         const Vector2& b = right ;
00088         return b * ProjectionMag( right ) ;
00089 }
00090 
00091 double Vector2::ProjectionMag (const Vector2& right) const
00092 {
00093         const Vector2& a = *this ;
00094         const Vector2& b = right ;
00095 
00096         double num = a.Dot( b ) ;
00097         double den = b.Dot( b ) ;
00098         assert( den != 0 ) ;
00099 
00100         return ( num / den ) ;
00101 }
00102 
00103 //=============================================================================
00104 // Reflect
00105 //
00106 // Description: Reflects this vector about a normal
00107 //=============================================================================
00108 void Vector2::Reflect( const Vector2& normal )
00109 {
00110         Vector2 projection = this->Projection( normal );
00111         *this -= ( projection * 2 ) ;
00112 }
00113 
00114 bool Vector2::operator == (const Vector2& right) const
00115 {
00116     //no homogenous coords
00117     if( this->elements[ 0 ] != right[ 0 ] )
00118     {
00119             return false ;
00120     }
00121     else if( this->elements[ 1 ] != right[ 1 ] )
00122     {
00123             return false ;
00124     }
00125     else
00126     {
00127             return true ;
00128     }
00129 }
00130 
00131 //=============================================================================
00132 // Compare
00133 //
00134 // Description: Compares if two vectors are equal to within a given Tolerance.
00135 //=============================================================================
00136 bool Vector2::Compare( const Vector2& right, const double& tol) const
00137 {
00138         //test that the elements are the same
00139         for( int i = 0; i < 4; i++ )
00140         {
00141                 double diff = elements[ i ] - right[ i ];
00142                 if( (diff > tol) || (diff < (-tol)) )
00143                 {
00144                         return false ;
00145                 }
00146         }
00147 
00148         // if still in the function, then all elements are equal to within tol.
00149         return true ;
00150 }
00151 
00152 void Vector2::Normalize ()
00153 {
00154         *this = *this * ( 1.0 / Magnitude() );
00155 }
00156 
00157 // Additional Declarations
00158 //-----------------------------------------------------------------------------
00159 std::ostream & operator<<(std::ostream &os, const Vector2& v)
00160 {
00161         os << "[" 
00162                 << v[ 0 ] << " , " 
00163                 << v[ 1 ] << " , " 
00164                 << "]" ;
00165         return os ;
00166 }
00167 //-----------------------------------------------------------------------------
00168 bool Vector2::operator< ( const Vector2& right ) const
00169 {
00170         assert( false ) ;
00171         return false ;
00172 }
00173 //-----------------------------------------------------------------------------
00174 bool Vector2::operator !=( const Vector2& right ) const
00175 {
00176         return !( this->operator ==( right ) );
00177 }
00178 
00179 //=============================================================================
00180 // operator *=
00181 //
00182 // Description: in place scalar multiplication operator
00183 //=============================================================================
00184 Vector2& Vector2::operator *=( const double right )
00185 {
00186         int i; 
00187         for( i = 0; i < 2; i++ )
00188         {
00189                 elements[ i ] *= right;
00190         }
00191         return *this;
00192 }
00193 
00194 //=============================================================================
00195 // operator /=
00196 //
00197 // Description: in place scalar division operator
00198 //=============================================================================
00199 Vector2& Vector2::operator /=( const double right )
00200 {
00201         int i; 
00202         for( i = 0; i < 2; ++i )
00203         {
00204                 elements[ i ] /= right;
00205         }
00206         return *this;
00207 }
00208 
00209 //=============================================================================
00210 // operator *=
00211 //
00212 // Description: in place scalar multiplication operator
00213 //=============================================================================
00214 Vector2 Vector2::operator -() const
00215 {
00216         double x = -elements[ 0 ];
00217         double y = -elements[ 1 ];
00218         return Vector2( x, y );
00219 }
00220 
00221 //-----------------------------------------------------------------------------
00222 std::istream & operator>>( std::istream &is, Vector2& v )
00223 {
00224         eatwhite( is ) ;
00225         char leading = is.get() ;                       //get the leading '['
00226         assert( leading == '[' ) ;
00227 
00228         for( int i = 0; i < 2; i++ ) 
00229         {
00230                 is >> v[ i ] ;
00231                 eatwhite( is ) ;
00232                 char delimiter = is.get() ;
00233                 assert( ( delimiter == ',' ) || ( delimiter == ']' ) ) ;
00234         }
00235 
00236     //this is to get rid of the extra homogenous coordinate
00237     double temp;
00238     is >> temp;
00239     char delimiter = is.get();
00240         return is ;
00241 };
00242 //-----------------------------------------------------------------------------

Generated on Sat Apr 1 21:30:38 2006 for Motion Planning Kernel by  doxygen 1.4.6-NO