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-2011 Torus Knot Software Ltd 00008 00009 Permission is hereby granted, free of charge, to any person obtaining a copy 00010 of this software and associated documentation files (the "Software"), to deal 00011 in the Software without restriction, including without limitation the rights 00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00013 copies of the Software, and to permit persons to whom the Software is 00014 furnished to do so, subject to the following conditions: 00015 00016 The above copyright notice and this permission notice shall be included in 00017 all copies or substantial portions of the Software. 00018 00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00025 THE SOFTWARE. 00026 ----------------------------------------------------------------------------- 00027 */ 00028 #ifndef __Vector2_H__ 00029 #define __Vector2_H__ 00030 00031 00032 #include "OgrePrerequisites.h" 00033 #include "OgreMath.h" 00034 00035 namespace Ogre 00036 { 00037 00051 class _OgreExport Vector2 00052 { 00053 public: 00054 Real x, y; 00055 00056 public: 00057 inline Vector2() 00058 { 00059 } 00060 00061 inline Vector2(const Real fX, const Real fY ) 00062 : x( fX ), y( fY ) 00063 { 00064 } 00065 00066 inline explicit Vector2( const Real scaler ) 00067 : x( scaler), y( scaler ) 00068 { 00069 } 00070 00071 inline explicit Vector2( const Real afCoordinate[2] ) 00072 : x( afCoordinate[0] ), 00073 y( afCoordinate[1] ) 00074 { 00075 } 00076 00077 inline explicit Vector2( const int afCoordinate[2] ) 00078 { 00079 x = (Real)afCoordinate[0]; 00080 y = (Real)afCoordinate[1]; 00081 } 00082 00083 inline explicit Vector2( Real* const r ) 00084 : x( r[0] ), y( r[1] ) 00085 { 00086 } 00087 00090 inline void swap(Vector2& other) 00091 { 00092 std::swap(x, other.x); 00093 std::swap(y, other.y); 00094 } 00095 00096 inline Real operator [] ( const size_t i ) const 00097 { 00098 assert( i < 2 ); 00099 00100 return *(&x+i); 00101 } 00102 00103 inline Real& operator [] ( const size_t i ) 00104 { 00105 assert( i < 2 ); 00106 00107 return *(&x+i); 00108 } 00109 00111 inline Real* ptr() 00112 { 00113 return &x; 00114 } 00116 inline const Real* ptr() const 00117 { 00118 return &x; 00119 } 00120 00125 inline Vector2& operator = ( const Vector2& rkVector ) 00126 { 00127 x = rkVector.x; 00128 y = rkVector.y; 00129 00130 return *this; 00131 } 00132 00133 inline Vector2& operator = ( const Real fScalar) 00134 { 00135 x = fScalar; 00136 y = fScalar; 00137 00138 return *this; 00139 } 00140 00141 inline bool operator == ( const Vector2& rkVector ) const 00142 { 00143 return ( x == rkVector.x && y == rkVector.y ); 00144 } 00145 00146 inline bool operator != ( const Vector2& rkVector ) const 00147 { 00148 return ( x != rkVector.x || y != rkVector.y ); 00149 } 00150 00151 // arithmetic operations 00152 inline Vector2 operator + ( const Vector2& rkVector ) const 00153 { 00154 return Vector2( 00155 x + rkVector.x, 00156 y + rkVector.y); 00157 } 00158 00159 inline Vector2 operator - ( const Vector2& rkVector ) const 00160 { 00161 return Vector2( 00162 x - rkVector.x, 00163 y - rkVector.y); 00164 } 00165 00166 inline Vector2 operator * ( const Real fScalar ) const 00167 { 00168 return Vector2( 00169 x * fScalar, 00170 y * fScalar); 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 Vector2 operator / ( const Real fScalar ) const 00181 { 00182 assert( fScalar != 0.0 ); 00183 00184 Real fInv = 1.0f / fScalar; 00185 00186 return Vector2( 00187 x * fInv, 00188 y * fInv); 00189 } 00190 00191 inline Vector2 operator / ( const Vector2& rhs) const 00192 { 00193 return Vector2( 00194 x / rhs.x, 00195 y / rhs.y); 00196 } 00197 00198 inline const Vector2& operator + () const 00199 { 00200 return *this; 00201 } 00202 00203 inline Vector2 operator - () const 00204 { 00205 return Vector2(-x, -y); 00206 } 00207 00208 // overloaded operators to help Vector2 00209 inline friend Vector2 operator * ( const Real fScalar, const Vector2& rkVector ) 00210 { 00211 return Vector2( 00212 fScalar * rkVector.x, 00213 fScalar * rkVector.y); 00214 } 00215 00216 inline friend Vector2 operator / ( const Real fScalar, const Vector2& rkVector ) 00217 { 00218 return Vector2( 00219 fScalar / rkVector.x, 00220 fScalar / rkVector.y); 00221 } 00222 00223 inline friend Vector2 operator + (const Vector2& lhs, const Real rhs) 00224 { 00225 return Vector2( 00226 lhs.x + rhs, 00227 lhs.y + rhs); 00228 } 00229 00230 inline friend Vector2 operator + (const Real lhs, const Vector2& rhs) 00231 { 00232 return Vector2( 00233 lhs + rhs.x, 00234 lhs + rhs.y); 00235 } 00236 00237 inline friend Vector2 operator - (const Vector2& lhs, const Real rhs) 00238 { 00239 return Vector2( 00240 lhs.x - rhs, 00241 lhs.y - rhs); 00242 } 00243 00244 inline friend Vector2 operator - (const Real lhs, const Vector2& rhs) 00245 { 00246 return Vector2( 00247 lhs - rhs.x, 00248 lhs - rhs.y); 00249 } 00250 // arithmetic updates 00251 inline Vector2& operator += ( const Vector2& rkVector ) 00252 { 00253 x += rkVector.x; 00254 y += rkVector.y; 00255 00256 return *this; 00257 } 00258 00259 inline Vector2& operator += ( const Real fScaler ) 00260 { 00261 x += fScaler; 00262 y += fScaler; 00263 00264 return *this; 00265 } 00266 00267 inline Vector2& operator -= ( const Vector2& rkVector ) 00268 { 00269 x -= rkVector.x; 00270 y -= rkVector.y; 00271 00272 return *this; 00273 } 00274 00275 inline Vector2& operator -= ( const Real fScaler ) 00276 { 00277 x -= fScaler; 00278 y -= fScaler; 00279 00280 return *this; 00281 } 00282 00283 inline Vector2& operator *= ( const Real fScalar ) 00284 { 00285 x *= fScalar; 00286 y *= fScalar; 00287 00288 return *this; 00289 } 00290 00291 inline Vector2& operator *= ( const Vector2& rkVector ) 00292 { 00293 x *= rkVector.x; 00294 y *= rkVector.y; 00295 00296 return *this; 00297 } 00298 00299 inline Vector2& operator /= ( const Real fScalar ) 00300 { 00301 assert( fScalar != 0.0 ); 00302 00303 Real fInv = 1.0f / fScalar; 00304 00305 x *= fInv; 00306 y *= fInv; 00307 00308 return *this; 00309 } 00310 00311 inline Vector2& operator /= ( const Vector2& rkVector ) 00312 { 00313 x /= rkVector.x; 00314 y /= rkVector.y; 00315 00316 return *this; 00317 } 00318 00326 inline Real length () const 00327 { 00328 return Math::Sqrt( x * x + y * y ); 00329 } 00330 00341 inline Real squaredLength () const 00342 { 00343 return x * x + y * y; 00344 } 00352 inline Real distance(const Vector2& rhs) const 00353 { 00354 return (*this - rhs).length(); 00355 } 00356 00367 inline Real squaredDistance(const Vector2& rhs) const 00368 { 00369 return (*this - rhs).squaredLength(); 00370 } 00371 00386 inline Real dotProduct(const Vector2& vec) const 00387 { 00388 return x * vec.x + y * vec.y; 00389 } 00390 00400 inline Real normalise() 00401 { 00402 Real fLength = Math::Sqrt( x * x + y * y); 00403 00404 // Will also work for zero-sized vectors, but will change nothing 00405 if ( fLength > 1e-08 ) 00406 { 00407 Real fInvLength = 1.0f / fLength; 00408 x *= fInvLength; 00409 y *= fInvLength; 00410 } 00411 00412 return fLength; 00413 } 00414 00415 00416 00420 inline Vector2 midPoint( const Vector2& vec ) const 00421 { 00422 return Vector2( 00423 ( x + vec.x ) * 0.5f, 00424 ( y + vec.y ) * 0.5f ); 00425 } 00426 00430 inline bool operator < ( const Vector2& rhs ) const 00431 { 00432 if( x < rhs.x && y < rhs.y ) 00433 return true; 00434 return false; 00435 } 00436 00440 inline bool operator > ( const Vector2& rhs ) const 00441 { 00442 if( x > rhs.x && y > rhs.y ) 00443 return true; 00444 return false; 00445 } 00446 00454 inline void makeFloor( const Vector2& cmp ) 00455 { 00456 if( cmp.x < x ) x = cmp.x; 00457 if( cmp.y < y ) y = cmp.y; 00458 } 00459 00467 inline void makeCeil( const Vector2& cmp ) 00468 { 00469 if( cmp.x > x ) x = cmp.x; 00470 if( cmp.y > y ) y = cmp.y; 00471 } 00472 00480 inline Vector2 perpendicular(void) const 00481 { 00482 return Vector2 (-y, x); 00483 } 00487 inline Real crossProduct( const Vector2& rkVector ) const 00488 { 00489 return x * rkVector.y - y * rkVector.x; 00490 } 00510 inline Vector2 randomDeviant( 00511 Real angle) const 00512 { 00513 00514 angle *= Math::UnitRandom() * Math::TWO_PI; 00515 Real cosa = cos(angle); 00516 Real sina = sin(angle); 00517 return Vector2(cosa * x - sina * y, 00518 sina * x + cosa * y); 00519 } 00520 00522 inline bool isZeroLength(void) const 00523 { 00524 Real sqlen = (x * x) + (y * y); 00525 return (sqlen < (1e-06 * 1e-06)); 00526 00527 } 00528 00531 inline Vector2 normalisedCopy(void) const 00532 { 00533 Vector2 ret = *this; 00534 ret.normalise(); 00535 return ret; 00536 } 00537 00541 inline Vector2 reflect(const Vector2& normal) const 00542 { 00543 return Vector2( *this - ( 2 * this->dotProduct(normal) * normal ) ); 00544 } 00546 inline bool isNaN() const 00547 { 00548 return Math::isNaN(x) || Math::isNaN(y); 00549 } 00550 00551 // special points 00552 static const Vector2 ZERO; 00553 static const Vector2 UNIT_X; 00554 static const Vector2 UNIT_Y; 00555 static const Vector2 NEGATIVE_UNIT_X; 00556 static const Vector2 NEGATIVE_UNIT_Y; 00557 static const Vector2 UNIT_SCALE; 00558 00561 inline _OgreExport friend std::ostream& operator << 00562 ( std::ostream& o, const Vector2& v ) 00563 { 00564 o << "Vector2(" << v.x << ", " << v.y << ")"; 00565 return o; 00566 } 00567 00568 }; 00572 } 00573 #endif
Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Sat Jan 14 2012 18:40:44