# OgreVector2.h

Go to the documentation of this file.

    00001 /*
    00002 -----------------------------------------------------------------------------
    00003 This source file is part of OGRE
    00004     (Object-oriented Graphics Rendering Engine)
    00005 For the latest info, see http://www.ogre3d.org/
    00006
    00007 Copyright (c) 2000-2005 The OGRE Team
    00008 Also see acknowledgements in Readme.html
    00009
    00010 This program is free software; you can redistribute it and/or modify it under
    00011 the terms of the GNU Lesser General Public License as published by the Free Software
    00012 Foundation; either version 2 of the License, or (at your option) any later
    00013 version.
    00014
    00015 This program is distributed in the hope that it will be useful, but WITHOUT
    00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
    00018
    00019 You should have received a copy of the GNU Lesser General Public License along with
    00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
    00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
    00022 http://www.gnu.org/copyleft/lesser.txt.
    00023 -----------------------------------------------------------------------------
    00024 */
    00025 #ifndef __Vector2_H__
    00026 #define __Vector2_H__
    00027
    00028
    00029 #include "OgrePrerequisites.h"
    00030 #include "OgreMath.h"
    00031
    00032 namespace Ogre
    00033 {
    00034
    00042     class _OgreExport Vector2
    00043     {
    00044     public:
    00045         union {
    00046             struct {
    00047                 Real x, y;
    00048             };
    00049             Real val[2];
    00050         };
    00051
    00052     public:
    00053         inline Vector2()
    00054         {
    00055         }
    00056
    00057         inline Vector2( Real fX, Real fY )
    00058             : x( fX ), y( fY )
    00059         {
    00060         }
    00061
    00062         inline Vector2( Real afCoordinate[2] )
    00063             : x( afCoordinate[0] ),
    00064               y( afCoordinate[1] )
    00065         {
    00066         }
    00067
    00068         inline Vector2( int afCoordinate[2] )
    00069         {
    00070             x = (Real)afCoordinate[0];
    00071             y = (Real)afCoordinate[1];
    00072         }
    00073
    00074         inline Vector2( const Real* const r )
    00075             : x( r[0] ), y( r[1] )
    00076         {
    00077         }
    00078
    00079         inline Vector2( const Vector2& rkVector )
    00080             : x( rkVector.x ), y( rkVector.y )
    00081         {
    00082         }
    00083
    00084         inline Real operator [] ( size_t i ) const
    00085         {
    00086             assert( i < 2 );
    00087
    00088             return *(&x+i);
    00089         }
    00090
    00091         inline Real& operator [] ( size_t i )
    00092         {
    00093             assert( i < 2 );
    00094
    00095             return *(&x+i);
    00096         }
    00097
    00102         inline Vector2& operator = ( const Vector2& rkVector )
    00103         {
    00104             x = rkVector.x;
    00105             y = rkVector.y;
    00106
    00107             return *this;
    00108         }
    00109
    00110         inline bool operator == ( const Vector2& rkVector ) const
    00111         {
    00112             return ( x == rkVector.x && y == rkVector.y );
    00113         }
    00114
    00115         inline bool operator != ( const Vector2& rkVector ) const
    00116         {
    00117             return ( x != rkVector.x || y != rkVector.y  );
    00118         }
    00119
    00120         // arithmetic operations
    00121         inline Vector2 operator + ( const Vector2& rkVector ) const
    00122         {
    00123             Vector2 kSum;
    00124
    00125             kSum.x = x + rkVector.x;
    00126             kSum.y = y + rkVector.y;
    00127
    00128             return kSum;
    00129         }
    00130
    00131         inline Vector2 operator - ( const Vector2& rkVector ) const
    00132         {
    00133             Vector2 kDiff;
    00134
    00135             kDiff.x = x - rkVector.x;
    00136             kDiff.y = y - rkVector.y;
    00137
    00138             return kDiff;
    00139         }
    00140
    00141         inline Vector2 operator * ( Real fScalar ) const
    00142         {
    00143             Vector2 kProd;
    00144
    00145             kProd.x = fScalar*x;
    00146             kProd.y = fScalar*y;
    00147
    00148             return kProd;
    00149         }
    00150
    00151         inline Vector2 operator * ( const Vector2& rhs) const
    00152         {
    00153             Vector2 kProd;
    00154
    00155             kProd.x = rhs.x * x;
    00156             kProd.y = rhs.y * y;
    00157
    00158             return kProd;
    00159         }
    00160
    00161         inline Vector2 operator / ( Real fScalar ) const
    00162         {
    00163             assert( fScalar != 0.0 );
    00164
    00165             Vector2 kDiv;
    00166
    00167             Real fInv = 1.0 / fScalar;
    00168             kDiv.x = x * fInv;
    00169             kDiv.y = y * fInv;
    00170
    00171             return kDiv;
    00172         }
    00173
    00174         inline Vector2 operator - () const
    00175         {
    00176             Vector2 kNeg;
    00177
    00178             kNeg.x = -x;
    00179             kNeg.y = -y;
    00180
    00181             return kNeg;
    00182         }
    00183
    00184         inline friend Vector2 operator * ( Real fScalar, const Vector2& rkVector )
    00185         {
    00186             Vector2 kProd;
    00187
    00188             kProd.x = fScalar * rkVector.x;
    00189             kProd.y = fScalar * rkVector.y;
    00190
    00191             return kProd;
    00192         }
    00193
    00194         // arithmetic updates
    00195         inline Vector2& operator += ( const Vector2& rkVector )
    00196         {
    00197             x += rkVector.x;
    00198             y += rkVector.y;
    00199
    00200             return *this;
    00201         }
    00202
    00203         inline Vector2& operator -= ( const Vector2& rkVector )
    00204         {
    00205             x -= rkVector.x;
    00206             y -= rkVector.y;
    00207
    00208             return *this;
    00209         }
    00210
    00211         inline Vector2& operator *= ( Real fScalar )
    00212         {
    00213             x *= fScalar;
    00214             y *= fScalar;
    00215
    00216             return *this;
    00217         }
    00218
    00219         inline Vector2& operator /= ( Real fScalar )
    00220         {
    00221             assert( fScalar != 0.0 );
    00222
    00223             Real fInv = 1.0 / fScalar;
    00224
    00225             x *= fInv;
    00226             y *= fInv;
    00227
    00228             return *this;
    00229         }
    00230
    00238         inline Real length () const
    00239         {
    00240             return Math::Sqrt( x * x + y * y );
    00241         }
    00242
    00253         inline Real squaredLength () const
    00254         {
    00255             return x * x + y * y;
    00256         }
    00257
    00272         inline Real dotProduct(const Vector2& vec) const
    00273         {
    00274             return x * vec.x + y * vec.y;
    00275         }
    00276
    00286         inline Real normalise()
    00287         {
    00288             Real fLength = Math::Sqrt( x * x + y * y);
    00289
    00290             // Will also work for zero-sized vectors, but will change nothing
    00291             if ( fLength > 1e-08 )
    00292             {
    00293                 Real fInvLength = 1.0 / fLength;
    00294                 x *= fInvLength;
    00295                 y *= fInvLength;
    00296             }
    00297
    00298             return fLength;
    00299         }
    00300
    00301
    00302
    00306         inline Vector2 midPoint( const Vector2& vec ) const
    00307         {
    00308             return Vector2(
    00309                 ( x + vec.x ) * 0.5,
    00310                 ( y + vec.y ) * 0.5 );
    00311         }
    00312
    00316         inline bool operator < ( const Vector2& rhs ) const
    00317         {
    00318             if( x < rhs.x && y < rhs.y )
    00319                 return true;
    00320             return false;
    00321         }
    00322
    00326         inline bool operator > ( const Vector2& rhs ) const
    00327         {
    00328             if( x > rhs.x && y > rhs.y )
    00329                 return true;
    00330             return false;
    00331         }
    00332
    00340         inline void makeFloor( const Vector2& cmp )
    00341         {
    00342             if( cmp.x < x ) x = cmp.x;
    00343             if( cmp.y < y ) y = cmp.y;
    00344         }
    00345
    00353         inline void makeCeil( const Vector2& cmp )
    00354         {
    00355             if( cmp.x > x ) x = cmp.x;
    00356             if( cmp.y > y ) y = cmp.y;
    00357         }
    00358
    00366         inline Vector2 perpendicular(void) const
    00367         {
    00368             return Vector2 (-y, x);
    00369         }
    00397         inline Vector2 crossProduct( const Vector2& rkVector ) const
    00398         {
    00399             return Vector2(-rkVector.y, rkVector.x);
    00400         }
    00420         inline Vector2 randomDeviant(
    00421             Real angle) const
    00422         {
    00423
    00424             angle *=  Math::UnitRandom() * Math::TWO_PI;
    00425             Real cosa = cos(angle);
    00426             Real sina = sin(angle);
    00427             return  Vector2(cosa * x - sina * y,
    00428                             sina * x + cosa * y);
    00429         }
    00430
    00432         inline bool isZeroLength(void) const
    00433         {
    00434             Real sqlen = (x * x) + (y * y);
    00435             return (sqlen < (1e-06 * 1e-06));
    00436
    00437         }
    00438
    00441         inline Vector2 normalisedCopy(void) const
    00442         {
    00443             Vector2 ret = *this;
    00444             ret.normalise();
    00445             return ret;
    00446         }
    00447
    00451         inline Vector2 reflect(const Vector2& normal) const
    00452         {
    00453             return Vector2( *this - ( 2 * this->dotProduct(normal) * normal ) );
    00454         }
    00455
    00456         // special points
    00457         static const Vector2 ZERO;
    00458         static const Vector2 UNIT_X;
    00459         static const Vector2 UNIT_Y;
    00460         static const Vector2 NEGATIVE_UNIT_X;
    00461         static const Vector2 NEGATIVE_UNIT_Y;
    00462         static const Vector2 UNIT_SCALE;
    00463
    00466         inline _OgreExport friend std::ostream& operator <<
    00467             ( std::ostream& o, const Vector2& v )
    00468         {
    00469             o << "Vector2(" << v.x << ", " << v.y <<  ")";
    00470             return o;
    00471         }
    00472     };
    00473
    00474 }
    00475 #endif

Copyright © 2000-2005 by The OGRE Team
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Feb 12 12:59:54 2006