K3dVector2.h

Go to the documentation of this file.
00001 
00012 /***************************************************************************
00013  *   Copyright (C) 2007 by Jan Koci   *
00014  *   honza.koci@email.cz   *
00015  *   http://kengine.sourceforge.net/tutorial/
00016  *                                                                         *
00017  *   This program is free software; you can redistribute it and/or modify  *
00018  *   it under the terms of the GNU General Public License as published by  *
00019  *   the Free Software Foundation; either version 2 of the License, or     *
00020  *   (at your option) any later version.                                   *
00021  *                                                                         *
00022  *   This program is distributed in the hope that it will be useful,       *
00023  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00024  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00025  *   GNU General Public License for more details.                          *
00026  *                                                                         *
00027  *   You should have received a copy of the GNU General Public License     *
00028  *   along with this program; if not, write to the                         *
00029  *   Free Software Foundation, Inc.,                                       *
00030  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00031  ***************************************************************************/
00032 
00033 
00034 #pragma once
00035 
00036 #include <iostream>     
00037 #include <cmath>
00038 
00039 #include "../System/K3dString.h"
00040 
00041 
00042 
00044         // 2D vector class
00046 
00047         class K3dVector2
00048         {
00049                 //      2d vector x,y
00050                 float m_afVector[2];
00051                 int m_iId;                              
00052         public:
00053 
00054 
00055                 //      Reset constructor (0, 0) 
00056                 K3dVector2();   
00057                 //      Constructor set vector from array
00058                 K3dVector2( const float vector[2] );
00059                 //      Constructor set vector from x, y, z values
00060                 K3dVector2( float x, float y);
00062                 ~K3dVector2();
00063 
00065                 // Coordinate access
00067 
00068                 // Get vector array 
00069                 float *GetVector();
00071                 int &GetId()
00072                 {
00073                         return m_iId;
00074                 }
00075                 // Overloaded operator[] allows set vector arrays  
00076                 float &operator[]( int index) ;
00077                 float operator[]( int index) const;
00078                 // Get scalar by index
00079                 float GetScalar(const int index);
00080                 // Set value by index
00081                 void SetScalar(const float fScalar, const int index);
00082                 // Set vector from another vector
00083                 void Set(  K3dVector2 vec );
00084                 // Set vector from      array values
00085                 void Set(  float vector[2] );
00086                 // Set vector from      (x, y) values
00087                 void Set( float x, float y);
00088                 // Set X value
00089                 void SetX(const float fX);
00090                 // Set Y value
00091                 void SetY(const float fY);
00092                 float &GetX()
00093                 {
00094                         return m_afVector[0];
00095                 }
00096                 float &GetY()
00097                 {
00098                         return m_afVector[1];
00099                 }
00100 
00102                 // Assignment operator
00104                 K3dVector2& operator= (const K3dVector2& rkV);
00105 
00107                 // Comparison operators
00109                 bool operator== (const K3dVector2& rkV);
00110                 bool operator!= (const K3dVector2& rkV);
00111                 bool operator<  (const K3dVector2& rkV);
00112                 bool operator<= (const K3dVector2& rkV);
00113                 bool operator>  (const K3dVector2& rkV);
00114                 bool operator>= (const K3dVector2& rkV);
00115                 // Compare arrays for comparison operators
00116                 int CompareArrays(const K3dVector2& rkV);
00117 
00118 
00120                 // Arithmetic operations
00122 
00123                 // Overloaded operator+ 
00124                 K3dVector2 operator+(const K3dVector2& rkV );
00125                 // Overloaded operator+ allows add vector and float value
00126                 K3dVector2 operator+( float fScalar );
00127                 // Overloaded operator- allows subtract two vectors
00128                 K3dVector2 operator-(K3dVector2 &rkVector );
00129                 // Overloaded operator- allows add vector and float value
00130                 K3dVector2 operator-( float fScalar );
00131                 K3dVector2 operator- ();
00132                 // Overloaded operator* allows multiply two vectors
00133                 K3dVector2 operator*( K3dVector2 &rkVector );
00134                 // Overloaded operator* allows multiply vector and float value
00135                 K3dVector2 operator*( float fScalar );
00136                 // Overloaded operator* allows divide two vectors
00137                 K3dVector2 operator/( K3dVector2 &rkVector);
00138                 // Overloaded operator* allows divide two vectors
00139                 K3dVector2 operator/( float fScalar );
00140 
00141 
00143                 // Arithmetic updates
00145 
00146                 // Overloaded operator+=
00147                 K3dVector2& operator+=(const K3dVector2 &rkVector); 
00148                 // Overloaded scalar operator+=
00149                 K3dVector2& operator+=(float fScalar );
00150                 // Overloaded operator-=
00151                 K3dVector2& operator-=(const K3dVector2 &rkVector); 
00152                 // Overloaded scalar operator-=
00153                 K3dVector2& operator-=(float fScalar );
00154                 // Overloaded operator*= allows multiply two vectors 
00155                 K3dVector2 operator*=( const K3dVector2 &rkVector );
00156                 // Overloaded operator*= allows multiply vector and float value
00157                 K3dVector2 operator*=( float fScalar );
00158                 // Overloaded operator/= allows divide two vectors 
00159                 K3dVector2 operator/=( const K3dVector2 &rkVector );
00160                 // Overloaded operator/= allows divide vector and float value
00161                 K3dVector2 operator/=( float fScalar );
00162 
00163 
00164 
00166                 // Functions
00168                 // Calc square of vector
00169                 K3dVector2 Sqr();
00170                 // Add new vector to original vector
00171                 void Add( const K3dVector2& vec );
00172                 // Add value to scalar by index
00173                 void AddScalar( const float fScalar, const int index );
00174                 // Subtract value from scalar by index
00175                 void SubtractScalar( const float fScalar, const int index );
00176                 //      Reset vector to (0, 0) coordinates.
00177                 void Reset();
00178                 // Calculate vector length. 
00179                 float Length();
00180                 // Calc square vector length
00181                 float SquaredLength ();
00182                 //      Normalize vector
00183                 void Normalize();
00184                 // Calculate vector between two vectors (vec_1 - vec_2)
00185                 void VectorBetween(K3dVector2 &_rkVec_1,K3dVector2 &_rkVec_2);
00186                 // Add two vectors
00187                 void AddVector(K3dVector2 &_rkVec_1, K3dVector2 &_rkVec_2);
00188                 // Divide vector by divider d�itele
00189                 void DivideVector(K3dVector2 &_rkVec, float divider);
00190                 // Translate vector by direction vector
00191                 void Translate(const K3dVector2 &_rkVertex, const K3dVector2 &_rkDirectVec, const float distance);
00192                 // Invert vector
00193                 void Invert();
00194                 // Calculate dot product between two vectors
00195                 float Dot( const K3dVector2 &_rkV);
00196                 // Change vector to angle for rotation by vector
00197                 void Angle();   
00198 
00199         };
00200 
00201         
00202 
00203         // Comparison ==
00204         inline bool K3dVector2::operator== (const K3dVector2& rkV) 
00205         {
00206                 return memcmp(m_afVector, rkV.m_afVector, 2*sizeof(float)) == 0;
00207         }
00208 
00209         
00210 
00211         inline bool K3dVector2::operator!= (const K3dVector2& rkV) 
00212         {
00213                 return memcmp(m_afVector, rkV.m_afVector, 2*sizeof(float)) != 0;
00214         }
00215 
00216         
00217 
00218         inline bool K3dVector2::operator<  (const K3dVector2& rkV) 
00219         {
00220                 return CompareArrays(rkV) < 0;
00221         }
00222 
00223         
00224 
00225         inline bool K3dVector2::operator<=  (const K3dVector2& rkV) 
00226         {
00227                 return CompareArrays(rkV) <= 0;
00228         }
00229 
00230         
00231 
00232         inline bool K3dVector2::operator>  (const K3dVector2& rkV) 
00233         {
00234                 return CompareArrays(rkV) > 0;
00235         }
00236 
00237         
00238 
00239         inline bool K3dVector2::operator>=  (const K3dVector2& rkV) 
00240         {
00241                 return CompareArrays(rkV) >= 0;
00242         }
00243 
00244         
00245 
00246         // Overloaded operator[] allows set vector arrays  
00247         inline float &K3dVector2::operator[]( int i )   
00248         { 
00249                 if(!(0 <= i) && (i < 2) )
00250                 {
00251                         cerr << "Error index -- Index must be from 0 to 1" << i <<endl;
00252                         return m_afVector[0];
00253                 }
00254                 return m_afVector[i]; 
00255         }
00256 
00257         inline float K3dVector2::operator[]( int i ) const
00258         { 
00259                 if(!(0 <= i) && (i < 2) )
00260                 {
00261                         cerr << "Error index -- Index must be from 0 to 1" << i <<endl;
00262                         return m_afVector[0];
00263                 }
00264                 return m_afVector[i]; 
00265         }
00266 
00267         
00268 
00269         // Get vector pointer 
00270         inline float *K3dVector2::GetVector() 
00271         { 
00272                 return m_afVector; 
00273         }
00274 
00275         
00276 
00277         // Get scalar by index
00278         inline float K3dVector2::GetScalar(const int i)
00279         {
00280                 if(!(0 <= i) && (i < 2) ) 
00281                 {
00282                         cerr << "Error index -- Index must be from 0 to 1" << i <<endl;
00283                         return (float)0;
00284                         //CDebug::InfoMsg("K3dVector2::GetScalar() -- error index", i);
00285                 }
00286                 return m_afVector[i];
00287         }
00288 
00289         
00290 
00291         // Set scalar by index
00292         inline void K3dVector2::SetScalar(const float fScalar, const int i)
00293         {
00294                 if(!(0 <= i) && (i < 2) ) 
00295                 {
00296                         cerr << "Error index -- Index must be from 0 to 1" << i <<endl;
00297                         return;
00298                         //CDebug::InfoMsg("K3dVector2::SetScalar() -- error index", i);
00299                 }
00300                 m_afVector[i] = fScalar;
00301         }
00302 
00303         
00304 
00305         // Translate vector by direction vector
00306         inline void K3dVector2::Translate(const K3dVector2 &_rkVertex, const K3dVector2 &_rkDirectVec, const float distance)
00307         {
00308                 m_afVector[0] = _rkVertex.m_afVector[0] + (_rkDirectVec.m_afVector[0] * distance);
00309                 m_afVector[1] = _rkVertex.m_afVector[1] + (_rkDirectVec.m_afVector[1] * distance);
00310         }
00311 
00312         
00313 
00314         // Invert vector
00315         inline void K3dVector2::Invert()
00316         {
00317                 m_afVector[0] = -m_afVector[0];
00318                 m_afVector[1] = -m_afVector[1];
00319         }
00320 
00321         
00322 
00323 
00324         // Calculate dot product between two vectors
00325         inline float K3dVector2::Dot(const K3dVector2 &_rkVector)
00326         {
00327                 float fX,fY, fResult;
00328                 fX = m_afVector[0] * _rkVector.m_afVector[0];
00329                 fY = m_afVector[1] * _rkVector.m_afVector[1];
00330                 fResult = fX + fY;
00331                 return fResult;
00332         }
00333 
00334         
00335 
00336         //      Reset vector to (0, 0,) coordinates
00337         inline void K3dVector2::Reset()
00338         {
00339                 m_afVector[0] = m_afVector[1] = 0;
00340         }
00341 
00342         
00343 
00344         // Set vector from another vector
00345         inline void K3dVector2::Set(  K3dVector2 vec )
00346         {
00347                 m_afVector[0] = vec.m_afVector[0];
00348                 m_afVector[1] = vec.m_afVector[1];
00349         }
00350 
00351         
00352 
00353         // Set x,y,z coordinates from array 
00354         inline void K3dVector2::Set(float vector[2] )
00355         {
00356                 m_afVector[0] = vector[0];
00357                 m_afVector[1] = vector[1];
00358         }
00359 
00360         
00361 
00362         // Set vector from      (x, y, z) values
00363         inline void K3dVector2::Set( float x, float y )
00364         {
00365                 m_afVector[0] = x;
00366                 m_afVector[1] = y;
00367         }
00368 
00369         
00370 
00371         // Set X value
00372         inline void K3dVector2::SetX(const float fX)
00373         {
00374                 m_afVector[0] = fX;
00375         }
00376         
00377         // Set Y value
00378         inline void K3dVector2::SetY(const float fY)
00379         {
00380                 m_afVector[1] = fY;
00381         }
00382         
00383 
00384         //      Add new vector to original vector
00385         inline void K3dVector2::Add( const K3dVector2& vec )
00386         {
00387                 m_afVector[0] += vec.m_afVector[0];
00388                 m_afVector[1] += vec.m_afVector[1];
00389         }
00390 
00391         
00392 
00393         // Add value to scalar by index
00394         inline void K3dVector2::AddScalar( const float fScalar, const int i )
00395         {
00396                 if(!(0 <= i) && (i < 2) ) 
00397                 {
00398                         cerr << "Error index -- Index must be from 0 to 1" << i <<endl;
00399                         return;
00400                 }
00401                 m_afVector[i] += fScalar;
00402         }
00403 
00404         
00405 
00406         // Subtract value from scalar by index
00407         inline void K3dVector2::SubtractScalar( const float fScalar, const int i )
00408         {
00409                 if(!(0 <= i) && (i < 2) ) 
00410                 {
00411                         cerr << "Error index -- Index must be from 0 to 1" << i <<endl;
00412                         return;
00413                 }
00414                 m_afVector[i] -= fScalar;
00415         }
00416 
00417         
00418 
00419 
00420         //      Normalize vector
00421         inline void K3dVector2::Normalize()
00422         {
00423 
00424                 float fLength0, fLength1;
00425 
00426                 // Calculate vector lenght
00427                 fLength0 = Length();    
00428 
00429                 if (fLength0)
00430                 {
00431                         fLength1 = 1.0f/fLength0;
00432                         m_afVector[0] *= fLength1;
00433                         m_afVector[1] *= fLength1;
00434                 }
00435         }
00436 
00437         
00438 
00439         // Calculate vector length
00440         inline float K3dVector2::Length()
00441         {
00442                 return (float) sqrt( m_afVector[0] * m_afVector[0] + m_afVector[1] * m_afVector[1]  );
00443         }
00444 
00445         
00446 
00447         // Calc square of vector
00448         inline K3dVector2 K3dVector2::Sqr() 
00449         {
00450                 K3dVector2 kV;
00451 
00452                 kV = *this;
00453 
00454                 kV *= kV;
00455 
00456                 return kV;
00457         }
00458 
00459         
00460 
00461 
00462         // Calculate vector between two vectors (vec_1 - vec_2)
00463         inline void K3dVector2::VectorBetween(K3dVector2 &_rkVec_1,K3dVector2 &_rkVec_2)
00464         {
00465                 m_afVector[0] = _rkVec_1[0] - _rkVec_2[0];                      
00466                 m_afVector[1] = _rkVec_1[1] - _rkVec_2[1];                                              
00467         }
00468 
00469         
00470 
00471         // Add two vectors
00472         inline void K3dVector2::AddVector(K3dVector2 &_rkVec_1, K3dVector2 &_rkVec_2)
00473         {
00474                 m_afVector[0] = _rkVec_1[0] + _rkVec_2[0];                      
00475                 m_afVector[1] = _rkVec_1[1] + _rkVec_2[1];                      
00476         }
00477 
00478         
00479 
00480         // Divide vector by divider 
00481         inline void K3dVector2::DivideVector(K3dVector2 &_rkVec, float divider)
00482         {
00483                 if(divider!=0)
00484                 {
00485                         m_afVector[0] = _rkVec[0] / divider;                    
00486                         m_afVector[1] = _rkVec[1] / divider;                            
00487                 }
00488                 else
00489                 {
00490                         cerr << "inline void K3dVector2::DivideVector() -- Error - Divide by zero" << endl;
00491                 }
00492         }
00493 
00494         
00495 
00496         // Change vector to angle for rotation by vector
00497         inline void K3dVector2::Angle()
00498         {
00499                 // Slave vector
00500                 K3dVector2 kV;
00501                 // Length of vector
00502                 float fLength;
00503 
00504 
00505                 // Get length of actual vector
00506                 fLength = this->Length();
00507 
00508 
00509 
00510                 if(fLength)
00511                 {
00512                         // Calculate length for multiplication instead dividing
00513                         fLength = 1/fLength;
00514                         // Calculate X angle
00515                         kV.m_afVector[0] = acos(this->m_afVector[0] * fLength);
00516                         // Calculate Y angle
00517                         kV.m_afVector[1] = acos(this->m_afVector[1] * fLength);// *  kV.m_afVector[0];
00518                         *this = kV;
00519                 }
00520         }
00521 

Generated on Thu Aug 16 23:53:27 2007 for K3dEngine by  doxygen 1.5.0