# 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 Real operator [] ( const size_t i ) const
    00084         {
    00085             assert( i < 2 );
    00086
    00087             return *(&x+i);
    00088         }
    00089
    00090         inline Real& operator [] ( const size_t i )
    00091         {
    00092             assert( i < 2 );
    00093
    00094             return *(&x+i);
    00095         }
    00096
    00098         inline Real* ptr()
    00099         {
    00100             return &x;
    00101         }
    00103         inline const Real* ptr() const
    00104         {
    00105             return &x;
    00106         }
    00107
    00112         inline Vector2& operator = ( const Vector2& rkVector )
    00113         {
    00114             x = rkVector.x;
    00115             y = rkVector.y;
    00116
    00117             return *this;
    00118         }
    00119
    00120         inline Vector2& operator = ( const Real fScalar)
    00121         {
    00122             x = fScalar;
    00123             y = fScalar;
    00124
    00125             return *this;
    00126         }
    00127
    00128         inline bool operator == ( const Vector2& rkVector ) const
    00129         {
    00130             return ( x == rkVector.x && y == rkVector.y );
    00131         }
    00132
    00133         inline bool operator != ( const Vector2& rkVector ) const
    00134         {
    00135             return ( x != rkVector.x || y != rkVector.y  );
    00136         }
    00137
    00138         // arithmetic operations
    00139         inline Vector2 operator + ( const Vector2& rkVector ) const
    00140         {
    00141             return Vector2(
    00142                 x + rkVector.x,
    00143                 y + rkVector.y);
    00144         }
    00145
    00146         inline Vector2 operator - ( const Vector2& rkVector ) const
    00147         {
    00148             return Vector2(
    00149                 x - rkVector.x,
    00150                 y - rkVector.y);
    00151         }
    00152
    00153         inline Vector2 operator * ( const Real fScalar ) const
    00154         {
    00155             return Vector2(
    00156                 x * fScalar,
    00157                 y * fScalar);
    00158         }
    00159
    00160         inline Vector2 operator * ( const Vector2& rhs) const
    00161         {
    00162             return Vector2(
    00163                 x * rhs.x,
    00164                 y * rhs.y);
    00165         }
    00166
    00167         inline Vector2 operator / ( const Real fScalar ) const
    00168         {
    00169             assert( fScalar != 0.0 );
    00170
    00171             Real fInv = 1.0 / fScalar;
    00172
    00173             return Vector2(
    00174                 x * fInv,
    00175                 y * fInv);
    00176         }
    00177
    00178         inline Vector2 operator / ( const Vector2& rhs) const
    00179         {
    00180             return Vector2(
    00181                 x / rhs.x,
    00182                 y / rhs.y);
    00183         }
    00184
    00185         inline const Vector2& operator + () const
    00186         {
    00187             return *this;
    00188         }
    00189
    00190         inline Vector2 operator - () const
    00191         {
    00192             return Vector2(-x, -y);
    00193         }
    00194
    00195         // overloaded operators to help Vector2
    00196         inline friend Vector2 operator * ( const Real fScalar, const Vector2& rkVector )
    00197         {
    00198             return Vector2(
    00199                 fScalar * rkVector.x,
    00200                 fScalar * rkVector.y);
    00201         }
    00202
    00203         inline friend Vector2 operator / ( const Real fScalar, const Vector2& rkVector )
    00204         {
    00205             return Vector2(
    00206                 fScalar / rkVector.x,
    00207                 fScalar / rkVector.y);
    00208         }
    00209
    00210         inline friend Vector2 operator + (const Vector2& lhs, const Real rhs)
    00211         {
    00212             return Vector2(
    00213                 lhs.x + rhs,
    00214                 lhs.y + rhs);
    00215         }
    00216
    00217         inline friend Vector2 operator + (const Real lhs, const Vector2& rhs)
    00218         {
    00219             return Vector2(
    00220                 lhs + rhs.x,
    00221                 lhs + rhs.y);
    00222         }
    00223
    00224         inline friend Vector2 operator - (const Vector2& lhs, const Real rhs)
    00225         {
    00226             return Vector2(
    00227                 lhs.x - rhs,
    00228                 lhs.y - rhs);
    00229         }
    00230
    00231         inline friend Vector2 operator - (const Real lhs, const Vector2& rhs)
    00232         {
    00233             return Vector2(
    00234                 lhs - rhs.x,
    00235                 lhs - rhs.y);
    00236         }
    00237         // arithmetic updates
    00238         inline Vector2& operator += ( const Vector2& rkVector )
    00239         {
    00240             x += rkVector.x;
    00241             y += rkVector.y;
    00242
    00243             return *this;
    00244         }
    00245
    00246         inline Vector2& operator += ( const Real fScaler )
    00247         {
    00248             x += fScaler;
    00249             y += fScaler;
    00250
    00251             return *this;
    00252         }
    00253
    00254         inline Vector2& operator -= ( const Vector2& rkVector )
    00255         {
    00256             x -= rkVector.x;
    00257             y -= rkVector.y;
    00258
    00259             return *this;
    00260         }
    00261
    00262         inline Vector2& operator -= ( const Real fScaler )
    00263         {
    00264             x -= fScaler;
    00265             y -= fScaler;
    00266
    00267             return *this;
    00268         }
    00269
    00270         inline Vector2& operator *= ( const Real fScalar )
    00271         {
    00272             x *= fScalar;
    00273             y *= fScalar;
    00274
    00275             return *this;
    00276         }
    00277
    00278         inline Vector2& operator *= ( const Vector2& rkVector )
    00279         {
    00280             x *= rkVector.x;
    00281             y *= rkVector.y;
    00282
    00283             return *this;
    00284         }
    00285
    00286         inline Vector2& operator /= ( const Real fScalar )
    00287         {
    00288             assert( fScalar != 0.0 );
    00289
    00290             Real fInv = 1.0 / fScalar;
    00291
    00292             x *= fInv;
    00293             y *= fInv;
    00294
    00295             return *this;
    00296         }
    00297
    00298         inline Vector2& operator /= ( const Vector2& rkVector )
    00299         {
    00300             x /= rkVector.x;
    00301             y /= rkVector.y;
    00302
    00303             return *this;
    00304         }
    00305
    00313         inline Real length () const
    00314         {
    00315             return Math::Sqrt( x * x + y * y );
    00316         }
    00317
    00328         inline Real squaredLength () const
    00329         {
    00330             return x * x + y * y;
    00331         }
    00332
    00347         inline Real dotProduct(const Vector2& vec) const
    00348         {
    00349             return x * vec.x + y * vec.y;
    00350         }
    00351
    00361         inline Real normalise()
    00362         {
    00363             Real fLength = Math::Sqrt( x * x + y * y);
    00364
    00365             // Will also work for zero-sized vectors, but will change nothing
    00366             if ( fLength > 1e-08 )
    00367             {
    00368                 Real fInvLength = 1.0 / fLength;
    00369                 x *= fInvLength;
    00370                 y *= fInvLength;
    00371             }
    00372
    00373             return fLength;
    00374         }
    00375
    00376
    00377
    00381         inline Vector2 midPoint( const Vector2& vec ) const
    00382         {
    00383             return Vector2(
    00384                 ( x + vec.x ) * 0.5,
    00385                 ( y + vec.y ) * 0.5 );
    00386         }
    00387
    00391         inline bool operator < ( const Vector2& rhs ) const
    00392         {
    00393             if( x < rhs.x && y < rhs.y )
    00394                 return true;
    00395             return false;
    00396         }
    00397
    00401         inline bool operator > ( const Vector2& rhs ) const
    00402         {
    00403             if( x > rhs.x && y > rhs.y )
    00404                 return true;
    00405             return false;
    00406         }
    00407
    00415         inline void makeFloor( const Vector2& cmp )
    00416         {
    00417             if( cmp.x < x ) x = cmp.x;
    00418             if( cmp.y < y ) y = cmp.y;
    00419         }
    00420
    00428         inline void makeCeil( const Vector2& cmp )
    00429         {
    00430             if( cmp.x > x ) x = cmp.x;
    00431             if( cmp.y > y ) y = cmp.y;
    00432         }
    00433
    00441         inline Vector2 perpendicular(void) const
    00442         {
    00443             return Vector2 (-y, x);
    00444         }
    00448         inline Real crossProduct( const Vector2& rkVector ) const
    00449         {
    00450             return x * rkVector.y - y * rkVector.x;
    00451         }
    00471         inline Vector2 randomDeviant(
    00472             Real angle) const
    00473         {
    00474
    00475             angle *=  Math::UnitRandom() * Math::TWO_PI;
    00476             Real cosa = cos(angle);
    00477             Real sina = sin(angle);
    00478             return  Vector2(cosa * x - sina * y,
    00479                             sina * x + cosa * y);
    00480         }
    00481
    00483         inline bool isZeroLength(void) const
    00484         {
    00485             Real sqlen = (x * x) + (y * y);
    00486             return (sqlen < (1e-06 * 1e-06));
    00487
    00488         }
    00489
    00492         inline Vector2 normalisedCopy(void) const
    00493         {
    00494             Vector2 ret = *this;
    00495             ret.normalise();
    00496             return ret;
    00497         }
    00498
    00502         inline Vector2 reflect(const Vector2& normal) const
    00503         {
    00504             return Vector2( *this - ( 2 * this->dotProduct(normal) * normal ) );
    00505         }
    00506
    00507         // special points
    00508         static const Vector2 ZERO;
    00509         static const Vector2 UNIT_X;
    00510         static const Vector2 UNIT_Y;
    00511         static const Vector2 NEGATIVE_UNIT_X;
    00512         static const Vector2 NEGATIVE_UNIT_Y;
    00513         static const Vector2 UNIT_SCALE;
    00514
    00517         inline _OgreExport friend std::ostream& operator <<
    00518             ( std::ostream& o, const Vector2& v )
    00519         {
    00520             o << "Vector2(" << v.x << ", " << v.y <<  ")";
    00521             return o;
    00522         }
    00523
    00524     };
    00525
    00526 }
    00527 #endif

Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Jul 26 11:32:21 2009