Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Vector2.h

Go to the documentation of this file.
00001 
00002 /*
00003     TEDDY - General graphics application library
00004     Copyright (C) 1999-2002  Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007         Adapted from
00008 
00009         The Universe Development Kit
00010         Copyright (C) 2000  Sean O'Neil
00011         s_p_oneil@hotmail.com
00012 
00013     This library is free software; you can redistribute it and/or
00014     modify it under the terms of the GNU Lesser General Public
00015     License as published by the Free Software Foundation; either
00016     version 2.1 of the License, or (at your option) any later version.
00017 
00018     This library is distributed in the hope that it will be useful,
00019     but WITHOUT ANY WARRANTY; without even the implied warranty of
00020     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021     Lesser General Public License for more details.
00022 
00023     You should have received a copy of the GNU Lesser General Public
00024     License along with this library; if not, write to the Free Software
00025     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026 
00027     $Id: Vector.h,v 1.5 2002/01/17 18:57:37 tksuoran Exp $
00028 */
00029 
00030 
00031 #ifndef TEDDY__MATHS__VECTOR_2__H
00032 #define TEDDY__MATHS__VECTOR_2__H
00033 
00034 
00035 #include "Teddy/SysSupport/StdMaths.h"
00036 #include "Teddy/SysSupport/StdIO.h"
00037 
00038 
00039 namespace Teddy {
00040 namespace Maths {
00041 
00042 
00043 /*
00044     \class   Vector
00045     \author  Sean O'Neil
00046 
00047     This template class implements a simple 2D vector with v[0] and v[1] coordinates.
00048     Several functions and operators are defined to make working with vectors easier,
00049     and because it's templatized, any numeric type can be used with it. Macros are
00050     defined for the most common types.
00051 */
00052 #ifndef SWIG
00053 #define Vector2        TVector2<float>
00054 #define DoubleVector2  TVector2<double>
00055 #define IntVector2     TVector2<int>
00056 
00057 #ifndef VECTOR_EPSILON
00058 #define VECTOR_EPSILON 0.00001
00059 #endif
00060 #endif
00061 
00062 
00072 template <typename T> class TVector2
00073 {
00074 public:
00075     T v[2];
00076 
00077     // Constructors
00078     TVector2(){}
00079     TVector2( const int a, const int b ){
00080         v[0] = T(a);
00081         v[1] = T(b);
00082     }
00083     TVector2( const float a, const float b ){
00084         v[0] = T(a);
00085         v[1] = T(b);
00086     }
00087     TVector2( const double a, const double b ){
00088         v[0] = T(a);
00089         v[1] = T(b);
00090     }
00091 
00092     TVector2( const TVector2<double> &v  ){ this->v[0] = (T)(v.v[0]); this->v[1] = (T)(v.v[1]); }
00093     TVector2( const TVector2<float>  &v  ){ this->v[0] = (T)(v.v[0]); this->v[1] = (T)(v.v[1]); }
00094     TVector2( const TVector2<int>    &v  ){ this->v[0] = (T)(v.v[0]); this->v[1] = (T)(v.v[1]); }
00095 
00096     // Casting and unary operators
00097     T           &operator               [] ( const int n )       { 
00098         if( n>1 ){
00099             printf( "Vector2::operator[] ERROR\n" );
00100         }
00101         return (&v[0])[n]; 
00102     }
00103     T            operator               [] ( const int n ) const { 
00104         if( n>1 ){
00105             printf( "Vector2::operator[] ERROR\n" );
00106         }
00107         return (&v[0])[n]; 
00108     }
00109     TVector2<T>  operator                - ()              const { return TVector2<T>(-v[0], -v[1]); }
00110 
00111     // Equal and comparison operators
00112 //  void operator= ( const T            t   )       { v[0] = v[1] = t; }
00113 //  void operator= ( const T           *pt  )       { v[0] = pt[0]; v[1] = pt[1]; }
00114     void operator= ( const TVector2<T> &vec )       { v[0] = vec.v[0]; v[1] = vec.v[1]; }
00115     bool operator==(       TVector2<T> &vec ) const { return (v[0] == vec.v[0]) && (v[1] == vec.v[1]); }
00116     bool operator!=(       TVector2<T> &vec ) const { return !(*this == vec); }
00117 
00118     // Arithmetic operators (vector with scalar)
00119           TVector2<T>  operator+ ( const T t ) const { return TVector2<T>(v[0]+t, v[1]+t); }
00120           TVector2<T>  operator- ( const T t ) const { return TVector2<T>(v[0]-t, v[1]-t); }
00121           TVector2<T>  operator* ( const T t ) const { return TVector2<T>(v[0]*t, v[1]*t); }
00122           TVector2<T>  operator/ ( const T t ) const { return TVector2<T>(v[0]/t, v[1]/t); }
00123     const TVector2<T> &operator+=( const T t )       { v[0] += t; v[1] += t; return *this; }
00124     const TVector2<T> &operator-=( const T t )       { v[0] -= t; v[1] -= t; return *this; }
00125     const TVector2<T> &operator*=( const T t )       { v[0] *= t; v[1] *= t; return *this; }
00126     const TVector2<T> &operator/=( const T t )       { v[0] /= t; v[1] /= t; return *this; }
00127 
00128     // Arithmetic operators (vector with vector)
00129           TVector2<T>  operator+ ( const TVector2<T> &vec ) const { return TVector2<T>(v[0]+vec.v[0], v[1]+vec.v[1]); }
00130           TVector2<T>  operator- ( const TVector2<T> &vec ) const { return TVector2<T>(v[0]-vec.v[0], v[1]-vec.v[1]); }
00131           TVector2<T>  operator* ( const TVector2<T> &vec ) const { return TVector2<T>(v[0]*vec.v[0], v[1]*vec.v[1]); }
00132           TVector2<T>  operator/ ( const TVector2<T> &vec ) const { return TVector2<T>(v[0]/vec.v[0], v[1]/vec.v[1]); }
00133     const TVector2<T> &operator+=( const TVector2<T> &vec )       { v[0] += vec.v[0]; v[1] += vec.v[1]; return *this; }
00134     const TVector2<T> &operator-=( const TVector2<T> &vec )       { v[0] -= vec.v[0]; v[1] -= vec.v[1]; return *this; }
00135     const TVector2<T> &operator*=( const TVector2<T> &vec )       { v[0] *= vec.v[0]; v[1] *= vec.v[1]; return *this; }
00136     const TVector2<T> &operator/=( const TVector2<T> &vec )       { v[0] /= vec.v[0]; v[1] /= vec.v[1]; return *this; }
00137 
00138     // Dot and cross product operators
00139           T            operator| ( const TVector2<T> &vec ) const { return v[0]*vec.v[0] + v[1]*vec.v[1]; }
00140 
00141     // Magnitude/distance methods
00142     T           magnitudeSquared()                         const { return v[0]*v[0] + v[1]*v[1]; }
00143     T           magnitude       ()                         const { return (T)sqrt(magnitudeSquared()); }
00144     T           distanceSquared ( const TVector2<T> &vec ) const { return (*this - vec).magnitudeSquared(); }
00145     T           distance        ( const TVector2<T> &vec ) const { return (*this - vec).magnitude(); }
00146     TVector2<T> midpoint        ( const TVector2<T> &vec ) const { return Vector((*this - vec) / 2 + v); }
00147     TVector2<T> average         ( const TVector2<T> &vec ) const { return Vector((*this + vec) / 2); }
00148 
00149     // Advanced methods (should only be used with float or double types)
00150     void        normalize()                               { *this /= magnitude(); }
00151     double      angle    ( const TVector2<T> &vec ) const { return acos(*this | vec); }
00152     TVector2<T> reflect  ( const TVector2<T> &n   ) const {
00153         T t = (T)magnitude();
00154         TVector2<T> v = *this / t;
00155         return (v - n * (2 * (v | n))) * t;
00156     }
00157 
00158 };
00159 
00160 
00161 // Returns the direction vector between two points
00162 template <class T> inline TVector2<T> directionVector( const TVector2<T> &p1, const TVector2<T> &p2 ){
00163     TVector2<T> vec = p2 - p1;
00164     vec.normalize();
00165     return vec;
00166 }
00167 
00168 
00169 };  //  namespace Maths
00170 };  //  namespace Teddy
00171 
00172 
00173 #endif  //  TEDDY__MATHS__VECTOR_2__H
00174