Gel2D - The free/open source game creation suite

gelVector.h
Go to the documentation of this file.
00001 /*
00002 Gel2D Game Engine - Cross-platform 2D gaming middleware
00003 Copyright (C) 2010-2011 Mark D. Procarione
00004 
00005 Gel2D is free software: you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation, either version 3 of the License, or
00008 (at your option) any later version.
00009 
00010 Gel2D is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013 GNU General Public License for more details.
00014 */
00015 
00016 #ifndef __GELVECTOR_H__
00017 #define __GELVECTOR_H__
00018 
00019 #include <cmath>
00020 
00021 namespace gel
00022 {
00025 
00026 
00029     class Gel2dVec
00030     {
00031         public:
00033             Gel2dVec() : x(0.0f), y(0.0f) {}
00034 
00036 
00037             Gel2dVec( const Gel2dVec &vec ) : x(vec.x), y(vec.y) {}
00038 
00040 
00041             Gel2dVec( float n ) : x(n), y(n) {}
00042 
00044 
00046             Gel2dVec( float _x, float _y ) : x(_x), y(_y) {}
00047 
00049 
00050             float getLength() const
00051             {
00052                 return sqrt(x*x + y*y);
00053             }
00054 
00056 
00057             float getSqrLength() const
00058             {
00059                 return x*x + y*y;
00060             }
00061 
00063 
00065             float getDistanceTo( const Gel2dVec &vec ) const
00066             {
00067                 return sqrt( (x - vec.x)*(x - vec.x) + (y - vec.y)*(y - vec.y) );
00068             }
00069 
00071 
00073             float getDot( const Gel2dVec &vec ) const
00074             {
00075                 return x*vec.x + y*vec.y;
00076             }
00077 
00079             void normalize()
00080             {
00081                 float len = getLength();
00082                 x /= len;
00083                 y /= len;
00084             }
00085 
00086             Gel2dVec operator+ ( const Gel2dVec &v ) const { return Gel2dVec( x+v.x, y+v.y ); }
00087             Gel2dVec operator- ( const Gel2dVec &v ) const { return Gel2dVec( x-v.x, y-v.y ); }
00088             Gel2dVec operator* ( const Gel2dVec &v ) const { return Gel2dVec( x*v.x, y*v.y ); }
00089             Gel2dVec operator/ ( const Gel2dVec &v ) const { return Gel2dVec( x/v.x, y/v.y ); }
00090             Gel2dVec operator= ( const Gel2dVec &v ) { x=v.x; y=v.y; return *this; }
00091             Gel2dVec operator+=( const Gel2dVec &v ) { x+=v.x; y+=v.y; return *this; }
00092             Gel2dVec operator-=( const Gel2dVec &v ) { x-=v.x; y-=v.y; return *this; }
00093             Gel2dVec operator*=( const Gel2dVec &v ) { x*=v.x; y*=v.y; return *this; }
00094             Gel2dVec operator/=( const Gel2dVec &v ) { x/=v.x; y/=v.y; return *this; }
00095 
00096             bool operator==( const Gel2dVec &v ) const { return ( x==v.x && y==v.y ); }
00097             bool operator!=( const Gel2dVec &v ) const { return ( x!=v.x || y!=v.y ); }
00098             bool operator< ( const Gel2dVec &v ) const { return ( x<v.x || (x==v.x && y<v.y) ); }
00099             bool operator> ( const Gel2dVec &v ) const { return ( x>v.x || (x==v.x && y>v.y) ); }
00100             bool operator<=( const Gel2dVec &v ) const
00101             {
00102                                 return ( (x<v.x || x==v.x) || ( x==v.x && (y<v.y || y==v.y) ) );
00103                         }
00104             bool operator>=( const Gel2dVec &v ) const
00105                         {
00106                                 return ( (x>v.x || x==v.x) || ( x==v.x && (y>v.y || y==v.y) ) );
00107                         }
00108 
00110             float x;
00112             float y;
00113     };
00114 
00116 
00118     class Gel3dVec
00119     {
00120         public:
00122             Gel3dVec() : x(0.0f), y(0.0f), z(0.0f) {}
00123 
00125 
00126             Gel3dVec( const Gel3dVec &vec ) : x(vec.x), y(vec.y), z(vec.z) {}
00127 
00129 
00130             Gel3dVec( float n ) : x(n), y(n), z(n) {}
00131 
00133 
00136             Gel3dVec( float _x, float _y, float _z ) : x(_x), y(_y), z(_z) {}
00137 
00139 
00140             float getLength() const
00141             {
00142                 return sqrt(x*x + y*y + z*z);
00143             }
00144 
00146 
00147             float getSqrLength() const
00148             {
00149                 return x*x + y*y + z*z;
00150             }
00151 
00153 
00155             float getDistanceTo( const Gel3dVec &vec ) const
00156             {
00157                 return sqrtf((x - vec.x)*(x - vec.x) +
00158                              (y - vec.y)*(y - vec.y) +
00159                              (z - vec.z)*(z - vec.z));
00160             }
00161 
00163 
00165             float getDot( const Gel3dVec &vec ) const
00166             {
00167                 return x*vec.x + y*vec.y + z*vec.z;
00168             }
00169 
00171 
00173             Gel3dVec getCross( const Gel3dVec &vec ) const
00174             {
00175                 return Gel3dVec((y*vec.z) - (z*vec.y),
00176                                 (z*vec.x) - (x*vec.z),
00177                                 (x*vec.y) - (y*vec.x));
00178             }
00179 
00181             void normalize()
00182             {
00183                 float len = getLength();
00184                 x /= len;
00185                 y /= len;
00186                 z /= len;
00187             }
00188 
00189             Gel3dVec operator+  ( const Gel3dVec &v ) { return Gel3dVec( x+v.x, y+v.y, z+v.z ); }
00190             Gel3dVec operator-  ( const Gel3dVec &v ) { return Gel3dVec( x-v.x, y-v.y, z-v.z ); }
00191             Gel3dVec operator*  ( const Gel3dVec &v ) { return Gel3dVec( x*v.x, y*v.y, z*v.z ); }
00192             Gel3dVec operator/  ( const Gel3dVec &v ) { return Gel3dVec( x/v.x, y/v.y, z/v.z ); }
00193             Gel3dVec operator=  ( const Gel3dVec &v ) { x=v.x; y=v.y; z=v.z; return *this; }
00194             Gel3dVec operator+= ( const Gel3dVec &v ) { x+=v.x; y+=v.y; z+=v.z; return *this; }
00195             Gel3dVec operator-= ( const Gel3dVec &v ) { x-=v.x; y-=v.y; z-=v.z; return *this; }
00196             Gel3dVec operator*= ( const Gel3dVec &v ) { x*=v.x; y*=v.y; z*=v.z; return *this; }
00197             Gel3dVec operator/= ( const Gel3dVec &v ) { x/=v.x; y/=v.y; z/=v.z; return *this; }
00198 
00199             bool operator== ( const Gel3dVec &v ) const { return ( x==v.x && y==v.y && z==v.z ); }
00200             bool operator!= ( const Gel3dVec &v ) const { return ( x!=v.x || y!=v.y || z!=v.z ); }
00201 
00203             float x;
00205             float y;
00207             float z;
00208     };
00210 }
00211 
00212 #endif // __GELVECTOR_H__