Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | 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-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(const Real fX, const Real fY )
00058             : x( fX ), y( fY )
00059         {
00060         }
00061 
00062         inline explicit Vector2( const Real scaler )
00063             : x( scaler), y( scaler )
00064         {
00065         }
00066 
00067         inline explicit Vector2( const Real afCoordinate[2] )
00068             : x( afCoordinate[0] ),
00069               y( afCoordinate[1] )
00070         {
00071         }
00072 
00073         inline explicit Vector2( const int afCoordinate[2] )
00074         {
00075             x = (Real)afCoordinate[0];
00076             y = (Real)afCoordinate[1];
00077         }
00078 
00079         inline explicit Vector2( Real* const r )
00080             : x( r[0] ), y( r[1] )
00081         {
00082         }
00083 
00084         inline Vector2( const Vector2& rkVector )
00085             : x( rkVector.x ), y( rkVector.y )
00086         {
00087         }
00088 
00089         inline Real operator [] ( const size_t i ) const
00090         {
00091             assert( i < 2 );
00092 
00093             return *(&x+i);
00094         }
00095 
00096         inline Real& operator [] ( const size_t i )
00097         {
00098             assert( i < 2 );
00099 
00100             return *(&x+i);
00101         }
00102 
00107         inline Vector2& operator = ( const Vector2& rkVector )
00108         {
00109             x = rkVector.x;
00110             y = rkVector.y;
00111 
00112             return *this;
00113         }
00114 
00115         inline Vector2& operator = ( const Real fScalar)
00116         {
00117             x = fScalar;
00118             y = fScalar;
00119 
00120             return *this;
00121         }
00122 
00123         inline bool operator == ( const Vector2& rkVector ) const
00124         {
00125             return ( x == rkVector.x && y == rkVector.y );
00126         }
00127 
00128         inline bool operator != ( const Vector2& rkVector ) const
00129         {
00130             return ( x != rkVector.x || y != rkVector.y  );
00131         }
00132 
00133         // arithmetic operations
00134         inline Vector2 operator + ( const Vector2& rkVector ) const
00135         {
00136             Vector2 kSum;
00137 
00138             kSum.x = x + rkVector.x;
00139             kSum.y = y + rkVector.y;
00140 
00141             return kSum;
00142         }
00143 
00144         inline Vector2 operator - ( const Vector2& rkVector ) const
00145         {
00146             Vector2 kDiff;
00147 
00148             kDiff.x = x - rkVector.x;
00149             kDiff.y = y - rkVector.y;
00150 
00151             return kDiff;
00152         }
00153 
00154         inline Vector2 operator * ( const Real fScalar ) const
00155         {
00156             Vector2 kProd;
00157 
00158             kProd.x = fScalar*x;
00159             kProd.y = fScalar*y;
00160 
00161             return kProd;
00162         }
00163 
00164         inline Vector2 operator * ( const Vector2& rhs) const
00165         {
00166             Vector2 kProd;
00167 
00168             kProd.x = rhs.x * x;
00169             kProd.y = rhs.y * y;
00170 
00171             return kProd;
00172         }
00173 
00174         inline Vector2 operator / ( const Real fScalar ) const
00175         {
00176             assert( fScalar != 0.0 );
00177 
00178             Vector2 kDiv;
00179 
00180             Real fInv = 1.0 / fScalar;
00181             kDiv.x = x * fInv;
00182             kDiv.y = y * fInv;
00183 
00184             return kDiv;
00185         }
00186 
00187         inline Vector2 operator - () const
00188         {
00189             Vector2 kNeg;
00190 
00191             kNeg.x = -x;
00192             kNeg.y = -y;
00193 
00194             return kNeg;
00195         }
00196 
00197         // overloaded operators to help Vector2
00198         inline friend Vector2 operator * ( const Real fScalar, const Vector2& rkVector )
00199         {
00200             Vector2 kProd;
00201 
00202             kProd.x = fScalar * rkVector.x;
00203             kProd.y = fScalar * rkVector.y;
00204 
00205             return kProd;
00206         }
00207 
00208         inline friend Vector2 operator + (const Vector2& lhs, const Real rhs)
00209         {
00210             Vector2 ret(rhs);
00211             return ret += lhs;
00212         }
00213 
00214         inline friend Vector2 operator + (const Real lhs, const Vector2& rhs)
00215         {
00216             Vector2 ret(lhs);
00217             return ret += rhs;
00218         }
00219 
00220         inline friend Vector2 operator - (const Vector2& lhs, const Real rhs)
00221         {
00222             return lhs - Vector2(rhs);
00223         }
00224 
00225         inline friend Vector2 operator - (const Real lhs, const Vector2& rhs)
00226         {
00227             Vector2 ret(lhs);
00228             return ret -= rhs;
00229         }
00230         // arithmetic updates
00231         inline Vector2& operator += ( const Vector2& rkVector )
00232         {
00233             x += rkVector.x;
00234             y += rkVector.y;
00235 
00236             return *this;
00237         }
00238 
00239         inline Vector2& operator += ( const Real fScaler )
00240         {
00241             x += fScaler;
00242             y += fScaler;
00243 
00244             return *this;
00245         }
00246 
00247         inline Vector2& operator -= ( const Vector2& rkVector )
00248         {
00249             x -= rkVector.x;
00250             y -= rkVector.y;
00251 
00252             return *this;
00253         }
00254 
00255         inline Vector2& operator -= ( const Real fScaler )
00256         {
00257             x -= fScaler;
00258             y -= fScaler;
00259 
00260             return *this;
00261         }
00262 
00263         inline Vector2& operator *= ( const Real fScalar )
00264         {
00265             x *= fScalar;
00266             y *= fScalar;
00267 
00268             return *this;
00269         }
00270 
00271         inline Vector2& operator /= ( const Real fScalar )
00272         {
00273             assert( fScalar != 0.0 );
00274 
00275             Real fInv = 1.0 / fScalar;
00276 
00277             x *= fInv;
00278             y *= fInv;
00279 
00280             return *this;
00281         }
00282 
00290         inline Real length () const
00291         {
00292             return Math::Sqrt( x * x + y * y );
00293         }
00294 
00305         inline Real squaredLength () const
00306         {
00307             return x * x + y * y;
00308         }
00309 
00324         inline Real dotProduct(const Vector2& vec) const
00325         {
00326             return x * vec.x + y * vec.y;
00327         }
00328 
00338         inline Real normalise()
00339         {
00340             Real fLength = Math::Sqrt( x * x + y * y);
00341 
00342             // Will also work for zero-sized vectors, but will change nothing
00343             if ( fLength > 1e-08 )
00344             {
00345                 Real fInvLength = 1.0 / fLength;
00346                 x *= fInvLength;
00347                 y *= fInvLength;
00348             }
00349 
00350             return fLength;
00351         }
00352 
00353 
00354 
00358         inline Vector2 midPoint( const Vector2& vec ) const
00359         {
00360             return Vector2(
00361                 ( x + vec.x ) * 0.5,
00362                 ( y + vec.y ) * 0.5 );
00363         }
00364 
00368         inline bool operator < ( const Vector2& rhs ) const
00369         {
00370             if( x < rhs.x && y < rhs.y )
00371                 return true;
00372             return false;
00373         }
00374 
00378         inline bool operator > ( const Vector2& rhs ) const
00379         {
00380             if( x > rhs.x && y > rhs.y )
00381                 return true;
00382             return false;
00383         }
00384 
00392         inline void makeFloor( const Vector2& cmp )
00393         {
00394             if( cmp.x < x ) x = cmp.x;
00395             if( cmp.y < y ) y = cmp.y;
00396         }
00397 
00405         inline void makeCeil( const Vector2& cmp )
00406         {
00407             if( cmp.x > x ) x = cmp.x;
00408             if( cmp.y > y ) y = cmp.y;
00409         }
00410 
00418         inline Vector2 perpendicular(void) const
00419         {
00420             return Vector2 (-y, x);
00421         }
00425         inline Real crossProduct( const Vector2& rkVector ) const
00426         {
00427             return x * rkVector.y - y * rkVector.x;
00428         }
00448         inline Vector2 randomDeviant(
00449             Real angle) const
00450         {
00451 
00452             angle *=  Math::UnitRandom() * Math::TWO_PI;
00453             Real cosa = cos(angle);
00454             Real sina = sin(angle);
00455             return  Vector2(cosa * x - sina * y,
00456                             sina * x + cosa * y);
00457         }
00458 
00460         inline bool isZeroLength(void) const
00461         {
00462             Real sqlen = (x * x) + (y * y);
00463             return (sqlen < (1e-06 * 1e-06));
00464 
00465         }
00466 
00469         inline Vector2 normalisedCopy(void) const
00470         {
00471             Vector2 ret = *this;
00472             ret.normalise();
00473             return ret;
00474         }
00475 
00479         inline Vector2 reflect(const Vector2& normal) const
00480         {
00481             return Vector2( *this - ( 2 * this->dotProduct(normal) * normal ) );
00482         }
00483 
00484         // special points
00485         static const Vector2 ZERO;
00486         static const Vector2 UNIT_X;
00487         static const Vector2 UNIT_Y;
00488         static const Vector2 NEGATIVE_UNIT_X;
00489         static const Vector2 NEGATIVE_UNIT_Y;
00490         static const Vector2 UNIT_SCALE;
00491 
00494         inline _OgreExport friend std::ostream& operator <<
00495             ( std::ostream& o, const Vector2& v )
00496         {
00497             o << "Vector2(" << v.x << ", " << v.y <<  ")";
00498             return o;
00499         }
00500 
00501     };
00502 
00503 }
00504 #endif

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