AaronCameron.net
No ads. No Profit. No Master, But Truth.
Not a Member? - Login or Create an Account
Tuesday the 22nd of May 2012 @ 04:11pm
Front Page Journal Projects Your Profile About
[]

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

cVector2.h

Go to the documentation of this file.
00001 /************************************************************************
00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library
00003 Copyright (C) 2002  Aaron Cameron
00004 
00005 This library is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Lesser General Public
00007 License as published by the Free Software Foundation; either
00008 version 2.1 of the License, or (at your option) any later version.
00009 
00010 This library 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 GNU
00013 Lesser General Public License for more details.
00014 
00015 You should have received a copy of the GNU Lesser General Public
00016 License along with this library; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00018 
00019 A copy of the GNU Lesser General Public License has been provided with
00020 this library in the file 'COPYING'.
00021 
00022 Contact information for the author of this library has been provided
00023 with this library in the file 'AUTHOR'.
00024 ************************************************************************/
00025 
00026 #ifndef _n2l4_cVector2_h
00027 #define _n2l4_cVector2_h
00028 
00029 #include <math.h>
00030 #include <GL/gl.h>
00031 
00032 /******************************************************************************/
00033 namespace n2l
00034 {
00035     template <class T1, class T2, class T3>
00036     class cVector3;
00037 
00042     template < class TComponent, class TComponentDiff, class TMagnitude>
00043     class cVector2
00044     {
00045     public:
00046         typedef cVector2<TComponent,TComponentDiff,TMagnitude> tThisVector;
00047         typedef TComponent      tComponent;
00048         typedef TComponentDiff  tComponentDiff;
00049         typedef TMagnitude      tMagnitude;
00050 
00051         typedef cVector2<TComponentDiff,TComponentDiff,TMagnitude> tVectorDiff;
00052 
00053         friend class cVector3<TComponent,TComponentDiff,TMagnitude>;
00054 
00055         // Constructors
00056         /**********************************************************************/
00057         cVector2()
00058         {
00059             mComp[0] = 0;
00060             mComp[1] = 0;
00061         }
00062 
00063         /**********************************************************************/
00064         cVector2( const tThisVector & iV )
00065         {
00066             mComp[0] = iV.mComp[0];
00067             mComp[1] = iV.mComp[1];
00068         }
00069 
00070         /**********************************************************************/
00071         cVector2(   const tComponent iX,
00072                     const tComponent iY )
00073         {
00074             mComp[0] = iX;
00075             mComp[1] = iY;
00076         }
00077 
00078         /**********************************************************************/
00079         template <typename TOtherVector>
00080         cVector2( const TOtherVector & iV )
00081         {
00082             mComp[0] = tComponent(iV.x());
00083             mComp[1] = tComponent(iV.y());
00084         }
00085 
00086         /**********************************************************************/
00087         ~cVector2() {}
00088 
00089 
00090         /**********************************************************************/
00091         // Assignments
00092         tThisVector & setX( const tComponent i_x ) { mComp[0] = i_x; return *this; }
00093         tThisVector & setY( const tComponent i_y ) { mComp[1] = i_y; return *this; }
00094 
00095         tComponent & x( const tComponent i_x ) { mComp[0] = i_x; return mComp[0]; }
00096         tComponent & y( const tComponent i_y ) { mComp[1] = i_y; return mComp[1]; }
00097 
00098         /**********************************************************************/
00099         tThisVector & operator =( const tThisVector & iV )
00100         {
00101             mComp[0] = iV.mComp[0];
00102             mComp[1] = iV.mComp[1];
00103             return *this;
00104         }
00105 
00106         /**********************************************************************/
00107         void set( const tComponent i_x, const tComponent i_y )
00108         {
00109             mComp[0] = i_x;  mComp[1] = i_y;
00110         }
00111 
00112         /**********************************************************************/
00113         // Views
00114         const tComponent getX() const { return mComp[0]; }
00115         const tComponent getY() const { return mComp[1]; }
00116 
00117         const tComponent & x() const { return mComp[0]; }
00118         const tComponent & y() const { return mComp[1]; }
00119 
00120         tComponent & x() { return mComp[0]; }
00121         tComponent & y() { return mComp[1]; }
00122 
00123         /**********************************************************************/
00124         void get( tComponent & o_x, tComponent & o_y ) const
00125         {
00126             o_x = mComp[0];  o_y = mComp[1];
00127         }
00128 
00129         /**********************************************************************/
00130         const tVectorDiff operator +( const tThisVector & i_v ) const
00131         {
00132             return tVectorDiff(mComp[0]+i_v.mComp[0],   mComp[1]+i_v.mComp[1]);
00133         }
00134 
00135 
00136         /**********************************************************************/
00137         const tVectorDiff operator -( const tThisVector & i_v ) const
00138         {
00139             return tVectorDiff(mComp[0]-i_v.mComp[0],   mComp[1]-i_v.mComp[1]);
00140         }
00141 
00142 
00143         /**********************************************************************/
00144         const tThisVector operator *( const tMagnitude i_mag ) const
00145         {
00146             return tThisVector(mComp[0]*i_mag,mComp[1]*i_mag);
00147         }
00148 
00149 
00150         /**********************************************************************/
00151         void operator *=( const tMagnitude i_mag )
00152         {
00153             mComp[0]*=i_mag;
00154             mComp[1]*=i_mag;
00155         }
00156 
00157 
00158         /**********************************************************************/
00159         const tThisVector operator /( const tMagnitude i_mag ) const
00160         {
00161             return tThisVector(mComp[0]/i_mag,mComp[1]/i_mag);
00162         }
00163 
00164 
00165         /**********************************************************************/
00166         const tThisVector operator *( const tThisVector & iV ) const
00167         {
00168             return tThisVector(mComp[0]*iV.mComp[0], mComp[1]*iV.mComp[1]);
00169         }
00170 
00171         /**********************************************************************/
00172         const tThisVector operator /( const tThisVector & iV ) const
00173         {
00174             return tThisVector(mComp[0]/iV.mComp[0], mComp[1]/iV.mComp[1]);
00175         }
00176 
00177         /**********************************************************************/
00178         const tMagnitude magnitude() const
00179         {
00180             return sqrt( mComp[0]*mComp[0] + mComp[1]*mComp[1] );
00181         }
00182 
00183         /**********************************************************************/
00184         const tMagnitude maxComp() const {
00185             return n2lMax(mComp[0],mComp[1]);
00186         }
00187         
00188         /**********************************************************************/
00189         // Show this vector how to become its difference
00190         operator tVectorDiff() const {
00191             return tVectorDiff(mComp[0],mComp[1]);
00192         }
00193 
00194         
00195         /**********************************************************************/
00196         template <typename TOtherVector>
00197         void operator -=( const TOtherVector & iOtherVector )
00198         {
00199             mComp[0] -= iOtherVector.x();
00200             mComp[1] -= iOtherVector.y();
00201         }
00202 
00203 
00204         /**********************************************************************/
00205         template <typename TOtherVector>
00206         void operator +=( const TOtherVector & iOtherVector )
00207         {
00208             mComp[0] += iOtherVector.x();
00209             mComp[1] += iOtherVector.y();
00210         }
00211 
00212 
00213         /**********************************************************************/
00214         template <typename TOtherVector>
00215         void operator *=( const TOtherVector & iOtherVector )
00216         {
00217             mComp[0] *= iOtherVector.x();
00218             mComp[1] *= iOtherVector.y();
00219         }
00220         
00221         /**********************************************************************/
00222         template <typename TOtherVector>
00223         const tBool operator ==( const TOtherVector & iOtherVector )
00224         {
00225             return (mComp[0]==iOtherVector.x() && mComp[1]==iOtherVector.y());
00226         }
00227 
00228         /**********************************************************************/
00229         /**********************************************************************/
00230         template <typename TOtherVector>
00231         const tBool operator !=( const TOtherVector & iOtherVector )
00232         {
00233             return (mComp[0]!=iOtherVector.x() || mComp[1]!=iOtherVector.y());
00234         }
00235 
00236         /**********************************************************************/
00237         const tString dump() const {
00238             tString str("tVector2(");
00239             str += asString(mComp[0]);
00240             str += ",";
00241             str += asString(mComp[1]);
00242             str += ")";
00243             return str;
00244         }
00245 
00246 
00247         /**********************************************************************/
00248         void glVertex2f() const
00249         {
00250             ::glVertex2fv( mComp );
00251         }
00252 
00253         /**********************************************************************/
00254         void glVertex3f() const
00255         {
00256 			::glVertex3f( mComp[0],mComp[1],0.0f );
00257         }
00258 
00259         /**********************************************************************/
00260         void glTexCoord2f() const
00261         {
00262             ::glTexCoord2fv( mComp );
00263         }
00264 
00265         /**********************************************************************/
00266         void glTranslatef() const
00267         {
00268 			::glTranslatef( mComp[0], mComp[1], 0.0f );
00269         }
00270 
00271     protected:
00272 
00273     private:
00274         tComponent mComp[2];
00275 
00276     }; // class
00277 
00278 } // namespace n2l
00279 
00280 
00281 
00282 #endif
©2012 Aaron Cameron