Home | Namespaces | Hierarchy | Alphabetical List | Class list | Files | Namespace Members | Class members | File members | Tutorials

vector2d.h

Go to the documentation of this file.
00001 // Copyright (C) 2002-2010 Nikolaus Gebhardt
00002 // This file is part of the "Irrlicht Engine".
00003 // For conditions of distribution and use, see copyright notice in irrlicht.h
00004 
00005 #ifndef __IRR_POINT_2D_H_INCLUDED__
00006 #define __IRR_POINT_2D_H_INCLUDED__
00007 
00008 #include "irrMath.h"
00009 #include "dimension2d.h"
00010 
00011 namespace irr
00012 {
00013 namespace core
00014 {
00015 
00016 
00018 
00020 template <class T>
00021 class vector2d
00022 {
00023 public:
00025         vector2d() : X(0), Y(0) {}
00027         vector2d(T nx, T ny) : X(nx), Y(ny) {}
00029         explicit vector2d(T n) : X(n), Y(n) {}
00031         vector2d(const vector2d<T>& other) : X(other.X), Y(other.Y) {}
00032 
00033         vector2d(const dimension2d<T>& other) : X(other.Width), Y(other.Height) {}
00034 
00035         // operators
00036 
00037         vector2d<T> operator-() const { return vector2d<T>(-X, -Y); }
00038 
00039         vector2d<T>& operator=(const vector2d<T>& other) { X = other.X; Y = other.Y; return *this; }
00040 
00041         vector2d<T>& operator=(const dimension2d<T>& other) { X = other.Width; Y = other.Height; return *this; }
00042 
00043         vector2d<T> operator+(const vector2d<T>& other) const { return vector2d<T>(X + other.X, Y + other.Y); }
00044         vector2d<T> operator+(const dimension2d<T>& other) const { return vector2d<T>(X + other.Width, Y + other.Height); }
00045         vector2d<T>& operator+=(const vector2d<T>& other) { X+=other.X; Y+=other.Y; return *this; }
00046         vector2d<T> operator+(const T v) const { return vector2d<T>(X + v, Y + v); }
00047         vector2d<T>& operator+=(const T v) { X+=v; Y+=v; return *this; }
00048         vector2d<T>& operator+=(const dimension2d<T>& other) { X += other.Width; Y += other.Height; return *this;  }
00049 
00050         vector2d<T> operator-(const vector2d<T>& other) const { return vector2d<T>(X - other.X, Y - other.Y); }
00051         vector2d<T> operator-(const dimension2d<T>& other) const { return vector2d<T>(X - other.Width, Y - other.Height); }
00052         vector2d<T>& operator-=(const vector2d<T>& other) { X-=other.X; Y-=other.Y; return *this; }
00053         vector2d<T> operator-(const T v) const { return vector2d<T>(X - v, Y - v); }
00054         vector2d<T>& operator-=(const T v) { X-=v; Y-=v; return *this; }
00055         vector2d<T>& operator-=(const dimension2d<T>& other) { X -= other.Width; Y -= other.Height; return *this;  }
00056 
00057         vector2d<T> operator*(const vector2d<T>& other) const { return vector2d<T>(X * other.X, Y * other.Y); }
00058         vector2d<T>& operator*=(const vector2d<T>& other) { X*=other.X; Y*=other.Y; return *this; }
00059         vector2d<T> operator*(const T v) const { return vector2d<T>(X * v, Y * v); }
00060         vector2d<T>& operator*=(const T v) { X*=v; Y*=v; return *this; }
00061 
00062         vector2d<T> operator/(const vector2d<T>& other) const { return vector2d<T>(X / other.X, Y / other.Y); }
00063         vector2d<T>& operator/=(const vector2d<T>& other) { X/=other.X; Y/=other.Y; return *this; }
00064         vector2d<T> operator/(const T v) const { return vector2d<T>(X / v, Y / v); }
00065         vector2d<T>& operator/=(const T v) { X/=v; Y/=v; return *this; }
00066 
00068         bool operator<=(const vector2d<T>&other) const
00069         {
00070                 return  (X<other.X || core::equals(X, other.X)) ||
00071                                 (core::equals(X, other.X) && (Y<other.Y || core::equals(Y, other.Y)));
00072         }
00073 
00075         bool operator>=(const vector2d<T>&other) const
00076         {
00077                 return  (X>other.X || core::equals(X, other.X)) ||
00078                                 (core::equals(X, other.X) && (Y>other.Y || core::equals(Y, other.Y)));
00079         }
00080 
00082         bool operator<(const vector2d<T>&other) const
00083         {
00084                 return  (X<other.X && !core::equals(X, other.X)) ||
00085                                 (core::equals(X, other.X) && Y<other.Y && !core::equals(Y, other.Y));
00086         }
00087 
00089         bool operator>(const vector2d<T>&other) const
00090         {
00091                 return  (X>other.X && !core::equals(X, other.X)) ||
00092                                 (core::equals(X, other.X) && Y>other.Y && !core::equals(Y, other.Y));
00093         }
00094 
00095         bool operator==(const vector2d<T>& other) const { return equals(other); }
00096         bool operator!=(const vector2d<T>& other) const { return !equals(other); }
00097 
00098         // functions
00099 
00101 
00104         bool equals(const vector2d<T>& other) const
00105         {
00106                 return core::equals(X, other.X) && core::equals(Y, other.Y);
00107         }
00108 
00109         vector2d<T>& set(T nx, T ny) {X=nx; Y=ny; return *this; }
00110         vector2d<T>& set(const vector2d<T>& p) { X=p.X; Y=p.Y; return *this; }
00111 
00113 
00114         T getLength() const { return core::squareroot( X*X + Y*Y ); }
00115 
00117 
00119         T getLengthSQ() const { return X*X + Y*Y; }
00120 
00122 
00124         T dotProduct(const vector2d<T>& other) const
00125         {
00126                 return X*other.X + Y*other.Y;
00127         }
00128 
00130 
00133         T getDistanceFrom(const vector2d<T>& other) const
00134         {
00135                 return vector2d<T>(X - other.X, Y - other.Y).getLength();
00136         }
00137 
00139 
00142         T getDistanceFromSQ(const vector2d<T>& other) const
00143         {
00144                 return vector2d<T>(X - other.X, Y - other.Y).getLengthSQ();
00145         }
00146 
00148 
00151         vector2d<T>& rotateBy(f64 degrees, const vector2d<T>& center=vector2d<T>())
00152         {
00153                 degrees *= DEGTORAD64;
00154                 const f64 cs = cos(degrees);
00155                 const f64 sn = sin(degrees);
00156 
00157                 X -= center.X;
00158                 Y -= center.Y;
00159 
00160                 set((T)(X*cs - Y*sn), (T)(X*sn + Y*cs));
00161 
00162                 X += center.X;
00163                 Y += center.Y;
00164                 return *this;
00165         }
00166 
00168 
00170         vector2d<T>& normalize()
00171         {
00172                 f32 length = (f32)(X*X + Y*Y);
00173                 if (core::equals(length, 0.f))
00174                         return *this;
00175                 length = core::reciprocal_squareroot ( length );
00176                 X = (T)(X * length);
00177                 Y = (T)(Y * length);
00178                 return *this;
00179         }
00180 
00182 
00185         f64 getAngleTrig() const
00186         {
00187                 if (Y == 0)
00188                         return X < 0 ? 180 : 0;
00189                 else
00190                 if (X == 0)
00191                         return Y < 0 ? 270 : 90;
00192 
00193                 if ( Y > 0)
00194                         if (X > 0)
00195                                 return atan((irr::f64)Y/(irr::f64)X) * RADTODEG64;
00196                         else
00197                                 return 180.0-atan((irr::f64)Y/-(irr::f64)X) * RADTODEG64;
00198                 else
00199                         if (X > 0)
00200                                 return 360.0-atan(-(irr::f64)Y/(irr::f64)X) * RADTODEG64;
00201                         else
00202                                 return 180.0+atan(-(irr::f64)Y/-(irr::f64)X) * RADTODEG64;
00203         }
00204 
00206 
00208         inline f64 getAngle() const
00209         {
00210                 if (Y == 0) // corrected thanks to a suggestion by Jox
00211                         return X < 0 ? 180 : 0;
00212                 else if (X == 0)
00213                         return Y < 0 ? 90 : 270;
00214 
00215                 // don't use getLength here to avoid precision loss with s32 vectors
00216                 f64 tmp = Y / sqrt((f64)(X*X + Y*Y));
00217                 tmp = atan( core::squareroot(1 - tmp*tmp) / tmp) * RADTODEG64;
00218 
00219                 if (X>0 && Y>0)
00220                         return tmp + 270;
00221                 else
00222                 if (X>0 && Y<0)
00223                         return tmp + 90;
00224                 else
00225                 if (X<0 && Y<0)
00226                         return 90 - tmp;
00227                 else
00228                 if (X<0 && Y>0)
00229                         return 270 - tmp;
00230 
00231                 return tmp;
00232         }
00233 
00235 
00237         inline f64 getAngleWith(const vector2d<T>& b) const
00238         {
00239                 f64 tmp = X*b.X + Y*b.Y;
00240 
00241                 if (tmp == 0.0)
00242                         return 90.0;
00243 
00244                 tmp = tmp / core::squareroot((f64)((X*X + Y*Y) * (b.X*b.X + b.Y*b.Y)));
00245                 if (tmp < 0.0)
00246                         tmp = -tmp;
00247 
00248                 return atan(sqrt(1 - tmp*tmp) / tmp) * RADTODEG64;
00249         }
00250 
00252 
00256         bool isBetweenPoints(const vector2d<T>& begin, const vector2d<T>& end) const
00257         {
00258                 if (begin.X != end.X)
00259                 {
00260                         return ((begin.X <= X && X <= end.X) ||
00261                                 (begin.X >= X && X >= end.X));
00262                 }
00263                 else
00264                 {
00265                         return ((begin.Y <= Y && Y <= end.Y) ||
00266                                 (begin.Y >= Y && Y >= end.Y));
00267                 }
00268         }
00269 
00271 
00275         vector2d<T> getInterpolated(const vector2d<T>& other, f64 d) const
00276         {
00277                 f64 inv = 1.0f - d;
00278                 return vector2d<T>((T)(other.X*inv + X*d), (T)(other.Y*inv + Y*d));
00279         }
00280 
00282 
00287         vector2d<T> getInterpolated_quadratic(const vector2d<T>& v2, const vector2d<T>& v3, f64 d) const
00288         {
00289                 // this*(1-d)*(1-d) + 2 * v2 * (1-d) + v3 * d * d;
00290                 const f64 inv = 1.0f - d;
00291                 const f64 mul0 = inv * inv;
00292                 const f64 mul1 = 2.0f * d * inv;
00293                 const f64 mul2 = d * d;
00294 
00295                 return vector2d<T> ( (T)(X * mul0 + v2.X * mul1 + v3.X * mul2),
00296                                         (T)(Y * mul0 + v2.Y * mul1 + v3.Y * mul2));
00297         }
00298 
00300 
00305         vector2d<T>& interpolate(const vector2d<T>& a, const vector2d<T>& b, f64 d)
00306         {
00307                 X = (T)((f64)b.X + ( ( a.X - b.X ) * d ));
00308                 Y = (T)((f64)b.Y + ( ( a.Y - b.Y ) * d ));
00309                 return *this;
00310         }
00311 
00313         T X;
00314 
00316         T Y;
00317 };
00318 
00320         typedef vector2d<f32> vector2df;
00321 
00323         typedef vector2d<s32> vector2di;
00324 
00325         template<class S, class T>
00326         vector2d<T> operator*(const S scalar, const vector2d<T>& vector) { return vector*scalar; }
00327 
00328         // These methods are declared in dimension2d, but need definitions of vector2d
00329         template<class T>
00330         dimension2d<T>::dimension2d(const vector2d<T>& other) : Width(other.X), Height(other.Y) { }
00331 
00332         template<class T>
00333         bool dimension2d<T>::operator==(const vector2d<T>& other) const { return Width == other.X && Height == other.Y; }
00334 
00335 } // end namespace core
00336 } // end namespace irr
00337 
00338 #endif
00339 

The Irrlicht Engine
The Irrlicht Engine Documentation © 2003-2010 by Nikolaus Gebhardt. Generated on Sun Oct 24 12:41:58 2010 by Doxygen (1.6.2)