- Main Page
- Namespaces
- Classes
- Files
- Directories
- Related Pages

# 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-2006 Torus Knot Software Ltd
    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 You may alternatively use this source under the terms of a specific version of
    00025 the OGRE Unrestricted License provided you have obtained such a license from
    00026 Torus Knot Software Ltd.
    00027 -----------------------------------------------------------------------------
    00028 */
    00029 #ifndef __Vector2_H__
    00030 #define __Vector2_H__
    00031
    00032
    00033 #include "OgrePrerequisites.h"
    00034 #include "OgreMath.h"
    00035
    00036 namespace Ogre
    00037 {
    00038
    00046     class _OgreExport Vector2
    00047     {
    00048     public:
    00049         Real x, y;
    00050
    00051     public:
    00052         inline Vector2()
    00053         {
    00054         }
    00055
    00056         inline Vector2(const Real fX, const Real fY )
    00057             : x( fX ), y( fY )
    00058         {
    00059         }
    00060
    00061         inline explicit Vector2( const Real scaler )
    00062             : x( scaler), y( scaler )
    00063         {
    00064         }
    00065
    00066         inline explicit Vector2( const Real afCoordinate[2] )
    00067             : x( afCoordinate[0] ),
    00068               y( afCoordinate[1] )
    00069         {
    00070         }
    00071
    00072         inline explicit Vector2( const int afCoordinate[2] )
    00073         {
    00074             x = (Real)afCoordinate[0];
    00075             y = (Real)afCoordinate[1];
    00076         }
    00077
    00078         inline explicit Vector2( Real* const r )
    00079             : x( r[0] ), y( r[1] )
    00080         {
    00081         }
    00082
    00083         inline Vector2( const Vector2& rkVector )
    00084             : x( rkVector.x ), y( rkVector.y )
    00085         {
    00086         }
    00087
    00088         inline Real operator [] ( const size_t i ) const
    00089         {
    00090             assert( i < 2 );
    00091
    00092             return *(&x+i);
    00093         }
    00094
    00095         inline Real& operator [] ( const size_t i )
    00096         {
    00097             assert( i < 2 );
    00098
    00099             return *(&x+i);
    00100         }
    00101
    00103         inline Real* ptr()
    00104         {
    00105             return &x;
    00106         }
    00108         inline const Real* ptr() const
    00109         {
    00110             return &x;
    00111         }
    00112
    00117         inline Vector2& operator = ( const Vector2& rkVector )
    00118         {
    00119             x = rkVector.x;
    00120             y = rkVector.y;
    00121
    00122             return *this;
    00123         }
    00124
    00125         inline Vector2& operator = ( const Real fScalar)
    00126         {
    00127             x = fScalar;
    00128             y = fScalar;
    00129
    00130             return *this;
    00131         }
    00132
    00133         inline bool operator == ( const Vector2& rkVector ) const
    00134         {
    00135             return ( x == rkVector.x && y == rkVector.y );
    00136         }
    00137
    00138         inline bool operator != ( const Vector2& rkVector ) const
    00139         {
    00140             return ( x != rkVector.x || y != rkVector.y  );
    00141         }
    00142
    00143         // arithmetic operations
    00144         inline Vector2 operator + ( const Vector2& rkVector ) const
    00145         {
    00146             return Vector2(
    00147                 x + rkVector.x,
    00148                 y + rkVector.y);
    00149         }
    00150
    00151         inline Vector2 operator - ( const Vector2& rkVector ) const
    00152         {
    00153             return Vector2(
    00154                 x - rkVector.x,
    00155                 y - rkVector.y);
    00156         }
    00157
    00158         inline Vector2 operator * ( const Real fScalar ) const
    00159         {
    00160             return Vector2(
    00161                 x * fScalar,
    00162                 y * fScalar);
    00163         }
    00164
    00165         inline Vector2 operator * ( const Vector2& rhs) const
    00166         {
    00167             return Vector2(
    00168                 x * rhs.x,
    00169                 y * rhs.y);
    00170         }
    00171
    00172         inline Vector2 operator / ( const Real fScalar ) const
    00173         {
    00174             assert( fScalar != 0.0 );
    00175
    00176             Real fInv = 1.0 / fScalar;
    00177
    00178             return Vector2(
    00179                 x * fInv,
    00180                 y * fInv);
    00181         }
    00182
    00183         inline Vector2 operator / ( const Vector2& rhs) const
    00184         {
    00185             return Vector2(
    00186                 x / rhs.x,
    00187                 y / rhs.y);
    00188         }
    00189
    00190         inline const Vector2& operator + () const
    00191         {
    00192             return *this;
    00193         }
    00194
    00195         inline Vector2 operator - () const
    00196         {
    00197             return Vector2(-x, -y);
    00198         }
    00199
    00200         // overloaded operators to help Vector2
    00201         inline friend Vector2 operator * ( const Real fScalar, const Vector2& rkVector )
    00202         {
    00203             return Vector2(
    00204                 fScalar * rkVector.x,
    00205                 fScalar * rkVector.y);
    00206         }
    00207
    00208         inline friend Vector2 operator / ( const Real fScalar, const Vector2& rkVector )
    00209         {
    00210             return Vector2(
    00211                 fScalar / rkVector.x,
    00212                 fScalar / rkVector.y);
    00213         }
    00214
    00215         inline friend Vector2 operator + (const Vector2& lhs, const Real rhs)
    00216         {
    00217             return Vector2(
    00218                 lhs.x + rhs,
    00219                 lhs.y + rhs);
    00220         }
    00221
    00222         inline friend Vector2 operator + (const Real lhs, const Vector2& rhs)
    00223         {
    00224             return Vector2(
    00225                 lhs + rhs.x,
    00226                 lhs + rhs.y);
    00227         }
    00228
    00229         inline friend Vector2 operator - (const Vector2& lhs, const Real rhs)
    00230         {
    00231             return Vector2(
    00232                 lhs.x - rhs,
    00233                 lhs.y - rhs);
    00234         }
    00235
    00236         inline friend Vector2 operator - (const Real lhs, const Vector2& rhs)
    00237         {
    00238             return Vector2(
    00239                 lhs - rhs.x,
    00240                 lhs - rhs.y);
    00241         }
    00242         // arithmetic updates
    00243         inline Vector2& operator += ( const Vector2& rkVector )
    00244         {
    00245             x += rkVector.x;
    00246             y += rkVector.y;
    00247
    00248             return *this;
    00249         }
    00250
    00251         inline Vector2& operator += ( const Real fScaler )
    00252         {
    00253             x += fScaler;
    00254             y += fScaler;
    00255
    00256             return *this;
    00257         }
    00258
    00259         inline Vector2& operator -= ( const Vector2& rkVector )
    00260         {
    00261             x -= rkVector.x;
    00262             y -= rkVector.y;
    00263
    00264             return *this;
    00265         }
    00266
    00267         inline Vector2& operator -= ( const Real fScaler )
    00268         {
    00269             x -= fScaler;
    00270             y -= fScaler;
    00271
    00272             return *this;
    00273         }
    00274
    00275         inline Vector2& operator *= ( const Real fScalar )
    00276         {
    00277             x *= fScalar;
    00278             y *= fScalar;
    00279
    00280             return *this;
    00281         }
    00282
    00283         inline Vector2& operator *= ( const Vector2& rkVector )
    00284         {
    00285             x *= rkVector.x;
    00286             y *= rkVector.y;
    00287
    00288             return *this;
    00289         }
    00290
    00291         inline Vector2& operator /= ( const Real fScalar )
    00292         {
    00293             assert( fScalar != 0.0 );
    00294
    00295             Real fInv = 1.0 / fScalar;
    00296
    00297             x *= fInv;
    00298             y *= fInv;
    00299
    00300             return *this;
    00301         }
    00302
    00303         inline Vector2& operator /= ( const Vector2& rkVector )
    00304         {
    00305             x /= rkVector.x;
    00306             y /= rkVector.y;
    00307
    00308             return *this;
    00309         }
    00310
    00318         inline Real length () const
    00319         {
    00320             return Math::Sqrt( x * x + y * y );
    00321         }
    00322
    00333         inline Real squaredLength () const
    00334         {
    00335             return x * x + y * y;
    00336         }
    00337
    00352         inline Real dotProduct(const Vector2& vec) const
    00353         {
    00354             return x * vec.x + y * vec.y;
    00355         }
    00356
    00366         inline Real normalise()
    00367         {
    00368             Real fLength = Math::Sqrt( x * x + y * y);
    00369
    00370             // Will also work for zero-sized vectors, but will change nothing
    00371             if ( fLength > 1e-08 )
    00372             {
    00373                 Real fInvLength = 1.0 / fLength;
    00374                 x *= fInvLength;
    00375                 y *= fInvLength;
    00376             }
    00377
    00378             return fLength;
    00379         }
    00380
    00381
    00382
    00386         inline Vector2 midPoint( const Vector2& vec ) const
    00387         {
    00388             return Vector2(
    00389                 ( x + vec.x ) * 0.5,
    00390                 ( y + vec.y ) * 0.5 );
    00391         }
    00392
    00396         inline bool operator < ( const Vector2& rhs ) const
    00397         {
    00398             if( x < rhs.x && y < rhs.y )
    00399                 return true;
    00400             return false;
    00401         }
    00402
    00406         inline bool operator > ( const Vector2& rhs ) const
    00407         {
    00408             if( x > rhs.x && y > rhs.y )
    00409                 return true;
    00410             return false;
    00411         }
    00412
    00420         inline void makeFloor( const Vector2& cmp )
    00421         {
    00422             if( cmp.x < x ) x = cmp.x;
    00423             if( cmp.y < y ) y = cmp.y;
    00424         }
    00425
    00433         inline void makeCeil( const Vector2& cmp )
    00434         {
    00435             if( cmp.x > x ) x = cmp.x;
    00436             if( cmp.y > y ) y = cmp.y;
    00437         }
    00438
    00446         inline Vector2 perpendicular(void) const
    00447         {
    00448             return Vector2 (-y, x);
    00449         }
    00453         inline Real crossProduct( const Vector2& rkVector ) const
    00454         {
    00455             return x * rkVector.y - y * rkVector.x;
    00456         }
    00476         inline Vector2 randomDeviant(
    00477             Real angle) const
    00478         {
    00479
    00480             angle *=  Math::UnitRandom() * Math::TWO_PI;
    00481             Real cosa = cos(angle);
    00482             Real sina = sin(angle);
    00483             return  Vector2(cosa * x - sina * y,
    00484                             sina * x + cosa * y);
    00485         }
    00486
    00488         inline bool isZeroLength(void) const
    00489         {
    00490             Real sqlen = (x * x) + (y * y);
    00491             return (sqlen < (1e-06 * 1e-06));
    00492
    00493         }
    00494
    00497         inline Vector2 normalisedCopy(void) const
    00498         {
    00499             Vector2 ret = *this;
    00500             ret.normalise();
    00501             return ret;
    00502         }
    00503
    00507         inline Vector2 reflect(const Vector2& normal) const
    00508         {
    00509             return Vector2( *this - ( 2 * this->dotProduct(normal) * normal ) );
    00510         }
    00511
    00512         // special points
    00513         static const Vector2 ZERO;
    00514         static const Vector2 UNIT_X;
    00515         static const Vector2 UNIT_Y;
    00516         static const Vector2 NEGATIVE_UNIT_X;
    00517         static const Vector2 NEGATIVE_UNIT_Y;
    00518         static const Vector2 UNIT_SCALE;
    00519
    00522         inline _OgreExport friend std::ostream& operator <<
    00523             ( std::ostream& o, const Vector2& v )
    00524         {
    00525             o << "Vector2(" << v.x << ", " << v.y <<  ")";
    00526             return o;
    00527         }
    00528
    00529     };
    00530
    00531 }
    00532 #endif

Copyright © 2000-2005 by The OGRE Team
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Mon Jun 16 12:48:57 2008