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 return Vector2( 00137 x + rkVector.x, 00138 y + rkVector.y); 00139 } 00140 00141 inline Vector2 operator - ( const Vector2& rkVector ) const 00142 { 00143 return Vector2( 00144 x - rkVector.x, 00145 y - rkVector.y); 00146 } 00147 00148 inline Vector2 operator * ( const Real fScalar ) const 00149 { 00150 return Vector2( 00151 x * fScalar, 00152 y * fScalar); 00153 } 00154 00155 inline Vector2 operator * ( const Vector2& rhs) const 00156 { 00157 return Vector2( 00158 x * rhs.x, 00159 y * rhs.y); 00160 } 00161 00162 inline Vector2 operator / ( const Real fScalar ) const 00163 { 00164 assert( fScalar != 0.0 ); 00165 00166 Real fInv = 1.0 / fScalar; 00167 00168 return Vector2( 00169 x * fInv, 00170 y * fInv); 00171 } 00172 00173 inline Vector2 operator / ( const Vector2& rhs) const 00174 { 00175 return Vector2( 00176 x / rhs.x, 00177 y / rhs.y); 00178 } 00179 00180 inline const Vector2& operator + () const 00181 { 00182 return *this; 00183 } 00184 00185 inline Vector2 operator - () const 00186 { 00187 return Vector2(-x, -y); 00188 } 00189 00190 // overloaded operators to help Vector2 00191 inline friend Vector2 operator * ( const Real fScalar, const Vector2& rkVector ) 00192 { 00193 return Vector2( 00194 fScalar * rkVector.x, 00195 fScalar * rkVector.y); 00196 } 00197 00198 inline friend Vector2 operator / ( const Real fScalar, const Vector2& rkVector ) 00199 { 00200 return Vector2( 00201 fScalar / rkVector.x, 00202 fScalar / rkVector.y); 00203 } 00204 00205 inline friend Vector2 operator + (const Vector2& lhs, const Real rhs) 00206 { 00207 return Vector2( 00208 lhs.x + rhs, 00209 lhs.y + rhs); 00210 } 00211 00212 inline friend Vector2 operator + (const Real lhs, const Vector2& rhs) 00213 { 00214 return Vector2( 00215 lhs + rhs.x, 00216 lhs + rhs.y); 00217 } 00218 00219 inline friend Vector2 operator - (const Vector2& lhs, const Real rhs) 00220 { 00221 return Vector2( 00222 lhs.x - rhs, 00223 lhs.y - rhs); 00224 } 00225 00226 inline friend Vector2 operator - (const Real lhs, const Vector2& rhs) 00227 { 00228 return Vector2( 00229 lhs - rhs.x, 00230 lhs - rhs.y); 00231 } 00232 // arithmetic updates 00233 inline Vector2& operator += ( const Vector2& rkVector ) 00234 { 00235 x += rkVector.x; 00236 y += rkVector.y; 00237 00238 return *this; 00239 } 00240 00241 inline Vector2& operator += ( const Real fScaler ) 00242 { 00243 x += fScaler; 00244 y += fScaler; 00245 00246 return *this; 00247 } 00248 00249 inline Vector2& operator -= ( const Vector2& rkVector ) 00250 { 00251 x -= rkVector.x; 00252 y -= rkVector.y; 00253 00254 return *this; 00255 } 00256 00257 inline Vector2& operator -= ( const Real fScaler ) 00258 { 00259 x -= fScaler; 00260 y -= fScaler; 00261 00262 return *this; 00263 } 00264 00265 inline Vector2& operator *= ( const Real fScalar ) 00266 { 00267 x *= fScalar; 00268 y *= fScalar; 00269 00270 return *this; 00271 } 00272 00273 inline Vector2& operator *= ( const Vector2& rkVector ) 00274 { 00275 x *= rkVector.x; 00276 y *= rkVector.y; 00277 00278 return *this; 00279 } 00280 00281 inline Vector2& operator /= ( const Real fScalar ) 00282 { 00283 assert( fScalar != 0.0 ); 00284 00285 Real fInv = 1.0 / fScalar; 00286 00287 x *= fInv; 00288 y *= fInv; 00289 00290 return *this; 00291 } 00292 00293 inline Vector2& operator /= ( const Vector2& rkVector ) 00294 { 00295 x /= rkVector.x; 00296 y /= rkVector.y; 00297 00298 return *this; 00299 } 00300 00308 inline Real length () const 00309 { 00310 return Math::Sqrt( x * x + y * y ); 00311 } 00312 00323 inline Real squaredLength () const 00324 { 00325 return x * x + y * y; 00326 } 00327 00342 inline Real dotProduct(const Vector2& vec) const 00343 { 00344 return x * vec.x + y * vec.y; 00345 } 00346 00356 inline Real normalise() 00357 { 00358 Real fLength = Math::Sqrt( x * x + y * y); 00359 00360 // Will also work for zero-sized vectors, but will change nothing 00361 if ( fLength > 1e-08 ) 00362 { 00363 Real fInvLength = 1.0 / fLength; 00364 x *= fInvLength; 00365 y *= fInvLength; 00366 } 00367 00368 return fLength; 00369 } 00370 00371 00372 00376 inline Vector2 midPoint( const Vector2& vec ) const 00377 { 00378 return Vector2( 00379 ( x + vec.x ) * 0.5, 00380 ( y + vec.y ) * 0.5 ); 00381 } 00382 00386 inline bool operator < ( const Vector2& rhs ) const 00387 { 00388 if( x < rhs.x && y < rhs.y ) 00389 return true; 00390 return false; 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 00410 inline void makeFloor( const Vector2& cmp ) 00411 { 00412 if( cmp.x < x ) x = cmp.x; 00413 if( cmp.y < y ) y = cmp.y; 00414 } 00415 00423 inline void makeCeil( const Vector2& cmp ) 00424 { 00425 if( cmp.x > x ) x = cmp.x; 00426 if( cmp.y > y ) y = cmp.y; 00427 } 00428 00436 inline Vector2 perpendicular(void) const 00437 { 00438 return Vector2 (-y, x); 00439 } 00443 inline Real crossProduct( const Vector2& rkVector ) const 00444 { 00445 return x * rkVector.y - y * rkVector.x; 00446 } 00466 inline Vector2 randomDeviant( 00467 Real angle) const 00468 { 00469 00470 angle *= Math::UnitRandom() * Math::TWO_PI; 00471 Real cosa = cos(angle); 00472 Real sina = sin(angle); 00473 return Vector2(cosa * x - sina * y, 00474 sina * x + cosa * y); 00475 } 00476 00478 inline bool isZeroLength(void) const 00479 { 00480 Real sqlen = (x * x) + (y * y); 00481 return (sqlen < (1e-06 * 1e-06)); 00482 00483 } 00484 00487 inline Vector2 normalisedCopy(void) const 00488 { 00489 Vector2 ret = *this; 00490 ret.normalise(); 00491 return ret; 00492 } 00493 00497 inline Vector2 reflect(const Vector2& normal) const 00498 { 00499 return Vector2( *this - ( 2 * this->dotProduct(normal) * normal ) ); 00500 } 00501 00502 // special points 00503 static const Vector2 ZERO; 00504 static const Vector2 UNIT_X; 00505 static const Vector2 UNIT_Y; 00506 static const Vector2 NEGATIVE_UNIT_X; 00507 static const Vector2 NEGATIVE_UNIT_Y; 00508 static const Vector2 UNIT_SCALE; 00509 00512 inline _OgreExport friend std::ostream& operator << 00513 ( std::ostream& o, const Vector2& v ) 00514 { 00515 o << "Vector2(" << v.x << ", " << v.y << ")"; 00516 return o; 00517 } 00518 00519 }; 00520 00521 } 00522 #endif
Copyright © 2000-2005 by The OGRE Team
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Jul 23 10:05:41 2006