00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00045
00046
00047
00048
00049
00050
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
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
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
00112
00113
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
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
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
00139 T operator| ( const TVector2<T> &vec ) const { return v[0]*vec.v[0] + v[1]*vec.v[1]; }
00140
00141
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
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
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 };
00170 };
00171
00172
00173 #endif // TEDDY__MATHS__VECTOR_2__H
00174