OGRE  1.7
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OgreVector2.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4  (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2011 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #ifndef __Vector2_H__
29 #define __Vector2_H__
30 
31 
32 #include "OgrePrerequisites.h"
33 #include "OgreMath.h"
34 
35 namespace Ogre
36 {
37 
52  {
53  public:
54  Real x, y;
55 
56  public:
57  inline Vector2()
58  {
59  }
60 
61  inline Vector2(const Real fX, const Real fY )
62  : x( fX ), y( fY )
63  {
64  }
65 
66  inline explicit Vector2( const Real scaler )
67  : x( scaler), y( scaler )
68  {
69  }
70 
71  inline explicit Vector2( const Real afCoordinate[2] )
72  : x( afCoordinate[0] ),
73  y( afCoordinate[1] )
74  {
75  }
76 
77  inline explicit Vector2( const int afCoordinate[2] )
78  {
79  x = (Real)afCoordinate[0];
80  y = (Real)afCoordinate[1];
81  }
82 
83  inline explicit Vector2( Real* const r )
84  : x( r[0] ), y( r[1] )
85  {
86  }
87 
90  inline void swap(Vector2& other)
91  {
92  std::swap(x, other.x);
93  std::swap(y, other.y);
94  }
95 
96  inline Real operator [] ( const size_t i ) const
97  {
98  assert( i < 2 );
99 
100  return *(&x+i);
101  }
102 
103  inline Real& operator [] ( const size_t i )
104  {
105  assert( i < 2 );
106 
107  return *(&x+i);
108  }
109 
111  inline Real* ptr()
112  {
113  return &x;
114  }
116  inline const Real* ptr() const
117  {
118  return &x;
119  }
120 
125  inline Vector2& operator = ( const Vector2& rkVector )
126  {
127  x = rkVector.x;
128  y = rkVector.y;
129 
130  return *this;
131  }
132 
133  inline Vector2& operator = ( const Real fScalar)
134  {
135  x = fScalar;
136  y = fScalar;
137 
138  return *this;
139  }
140 
141  inline bool operator == ( const Vector2& rkVector ) const
142  {
143  return ( x == rkVector.x && y == rkVector.y );
144  }
145 
146  inline bool operator != ( const Vector2& rkVector ) const
147  {
148  return ( x != rkVector.x || y != rkVector.y );
149  }
150 
151  // arithmetic operations
152  inline Vector2 operator + ( const Vector2& rkVector ) const
153  {
154  return Vector2(
155  x + rkVector.x,
156  y + rkVector.y);
157  }
158 
159  inline Vector2 operator - ( const Vector2& rkVector ) const
160  {
161  return Vector2(
162  x - rkVector.x,
163  y - rkVector.y);
164  }
165 
166  inline Vector2 operator * ( const Real fScalar ) const
167  {
168  return Vector2(
169  x * fScalar,
170  y * fScalar);
171  }
172 
173  inline Vector2 operator * ( const Vector2& rhs) const
174  {
175  return Vector2(
176  x * rhs.x,
177  y * rhs.y);
178  }
179 
180  inline Vector2 operator / ( const Real fScalar ) const
181  {
182  assert( fScalar != 0.0 );
183 
184  Real fInv = 1.0f / fScalar;
185 
186  return Vector2(
187  x * fInv,
188  y * fInv);
189  }
190 
191  inline Vector2 operator / ( const Vector2& rhs) const
192  {
193  return Vector2(
194  x / rhs.x,
195  y / rhs.y);
196  }
197 
198  inline const Vector2& operator + () const
199  {
200  return *this;
201  }
202 
203  inline Vector2 operator - () const
204  {
205  return Vector2(-x, -y);
206  }
207 
208  // overloaded operators to help Vector2
209  inline friend Vector2 operator * ( const Real fScalar, const Vector2& rkVector )
210  {
211  return Vector2(
212  fScalar * rkVector.x,
213  fScalar * rkVector.y);
214  }
215 
216  inline friend Vector2 operator / ( const Real fScalar, const Vector2& rkVector )
217  {
218  return Vector2(
219  fScalar / rkVector.x,
220  fScalar / rkVector.y);
221  }
222 
223  inline friend Vector2 operator + (const Vector2& lhs, const Real rhs)
224  {
225  return Vector2(
226  lhs.x + rhs,
227  lhs.y + rhs);
228  }
229 
230  inline friend Vector2 operator + (const Real lhs, const Vector2& rhs)
231  {
232  return Vector2(
233  lhs + rhs.x,
234  lhs + rhs.y);
235  }
236 
237  inline friend Vector2 operator - (const Vector2& lhs, const Real rhs)
238  {
239  return Vector2(
240  lhs.x - rhs,
241  lhs.y - rhs);
242  }
243 
244  inline friend Vector2 operator - (const Real lhs, const Vector2& rhs)
245  {
246  return Vector2(
247  lhs - rhs.x,
248  lhs - rhs.y);
249  }
250  // arithmetic updates
251  inline Vector2& operator += ( const Vector2& rkVector )
252  {
253  x += rkVector.x;
254  y += rkVector.y;
255 
256  return *this;
257  }
258 
259  inline Vector2& operator += ( const Real fScaler )
260  {
261  x += fScaler;
262  y += fScaler;
263 
264  return *this;
265  }
266 
267  inline Vector2& operator -= ( const Vector2& rkVector )
268  {
269  x -= rkVector.x;
270  y -= rkVector.y;
271 
272  return *this;
273  }
274 
275  inline Vector2& operator -= ( const Real fScaler )
276  {
277  x -= fScaler;
278  y -= fScaler;
279 
280  return *this;
281  }
282 
283  inline Vector2& operator *= ( const Real fScalar )
284  {
285  x *= fScalar;
286  y *= fScalar;
287 
288  return *this;
289  }
290 
291  inline Vector2& operator *= ( const Vector2& rkVector )
292  {
293  x *= rkVector.x;
294  y *= rkVector.y;
295 
296  return *this;
297  }
298 
299  inline Vector2& operator /= ( const Real fScalar )
300  {
301  assert( fScalar != 0.0 );
302 
303  Real fInv = 1.0f / fScalar;
304 
305  x *= fInv;
306  y *= fInv;
307 
308  return *this;
309  }
310 
311  inline Vector2& operator /= ( const Vector2& rkVector )
312  {
313  x /= rkVector.x;
314  y /= rkVector.y;
315 
316  return *this;
317  }
318 
326  inline Real length () const
327  {
328  return Math::Sqrt( x * x + y * y );
329  }
330 
341  inline Real squaredLength () const
342  {
343  return x * x + y * y;
344  }
352  inline Real distance(const Vector2& rhs) const
353  {
354  return (*this - rhs).length();
355  }
356 
367  inline Real squaredDistance(const Vector2& rhs) const
368  {
369  return (*this - rhs).squaredLength();
370  }
371 
386  inline Real dotProduct(const Vector2& vec) const
387  {
388  return x * vec.x + y * vec.y;
389  }
390 
400  inline Real normalise()
401  {
402  Real fLength = Math::Sqrt( x * x + y * y);
403 
404  // Will also work for zero-sized vectors, but will change nothing
405  if ( fLength > 1e-08 )
406  {
407  Real fInvLength = 1.0f / fLength;
408  x *= fInvLength;
409  y *= fInvLength;
410  }
411 
412  return fLength;
413  }
414 
415 
416 
420  inline Vector2 midPoint( const Vector2& vec ) const
421  {
422  return Vector2(
423  ( x + vec.x ) * 0.5f,
424  ( y + vec.y ) * 0.5f );
425  }
426 
430  inline bool operator < ( const Vector2& rhs ) const
431  {
432  if( x < rhs.x && y < rhs.y )
433  return true;
434  return false;
435  }
436 
440  inline bool operator > ( const Vector2& rhs ) const
441  {
442  if( x > rhs.x && y > rhs.y )
443  return true;
444  return false;
445  }
446 
454  inline void makeFloor( const Vector2& cmp )
455  {
456  if( cmp.x < x ) x = cmp.x;
457  if( cmp.y < y ) y = cmp.y;
458  }
459 
467  inline void makeCeil( const Vector2& cmp )
468  {
469  if( cmp.x > x ) x = cmp.x;
470  if( cmp.y > y ) y = cmp.y;
471  }
472 
480  inline Vector2 perpendicular(void) const
481  {
482  return Vector2 (-y, x);
483  }
487  inline Real crossProduct( const Vector2& rkVector ) const
488  {
489  return x * rkVector.y - y * rkVector.x;
490  }
511  Real angle) const
512  {
513 
514  angle *= Math::UnitRandom() * Math::TWO_PI;
515  Real cosa = cos(angle);
516  Real sina = sin(angle);
517  return Vector2(cosa * x - sina * y,
518  sina * x + cosa * y);
519  }
520 
522  inline bool isZeroLength(void) const
523  {
524  Real sqlen = (x * x) + (y * y);
525  return (sqlen < (1e-06 * 1e-06));
526 
527  }
528 
531  inline Vector2 normalisedCopy(void) const
532  {
533  Vector2 ret = *this;
534  ret.normalise();
535  return ret;
536  }
537 
541  inline Vector2 reflect(const Vector2& normal) const
542  {
543  return Vector2( *this - ( 2 * this->dotProduct(normal) * normal ) );
544  }
546  inline bool isNaN() const
547  {
548  return Math::isNaN(x) || Math::isNaN(y);
549  }
550 
551  // special points
552  static const Vector2 ZERO;
553  static const Vector2 UNIT_X;
554  static const Vector2 UNIT_Y;
555  static const Vector2 NEGATIVE_UNIT_X;
556  static const Vector2 NEGATIVE_UNIT_Y;
557  static const Vector2 UNIT_SCALE;
558 
561  inline _OgreExport friend std::ostream& operator <<
562  ( std::ostream& o, const Vector2& v )
563  {
564  o << "Vector2(" << v.x << ", " << v.y << ")";
565  return o;
566  }
567 
568  };
572 }
573 #endif
Real squaredDistance(const Vector2 &rhs) const
Returns the square of the distance to another vector.
Definition: OgreVector2.h:367
Real length() const
Returns the length (magnitude) of the vector.
Definition: OgreVector2.h:326
Real dotProduct(const Vector2 &vec) const
Calculates the dot (scalar) product of this vector with another.
Definition: OgreVector2.h:386
static Real UnitRandom()
float Real
Software floating point type.
#define _OgreExport
Definition: OgrePlatform.h:203
static const Vector2 UNIT_Y
Definition: OgreVector2.h:554
Vector2(const Real fX, const Real fY)
Definition: OgreVector2.h:61
bool operator<(SharedPtr< T > const &a, SharedPtr< U > const &b)
void swap(Vector2 &other)
Exchange the contents of this vector with another.
Definition: OgreVector2.h:90
static const Vector2 NEGATIVE_UNIT_X
Definition: OgreVector2.h:555
static const Real TWO_PI
Definition: OgreMath.h:593
static const Vector2 UNIT_X
Definition: OgreVector2.h:553
Vector2(const Real afCoordinate[2])
Definition: OgreVector2.h:71
Real crossProduct(const Vector2 &rkVector) const
Calculates the 2 dimensional cross-product of 2 vectors, which results in a single floating point val...
Definition: OgreVector2.h:487
Vector2 reflect(const Vector2 &normal) const
Calculates a reflection vector to the plane with the given normal .
Definition: OgreVector2.h:541
Radian operator*(Real a, const Radian &b)
Definition: OgreMath.h:633
Radian operator/(Real a, const Radian &b)
Definition: OgreMath.h:638
Vector2(const Real scaler)
Definition: OgreVector2.h:66
Vector2 midPoint(const Vector2 &vec) const
Returns a vector at a point half way between this and the passed in vector.
Definition: OgreVector2.h:420
Vector2 randomDeviant(Real angle) const
Generates a new random vector which deviates from this vector by a given angle in a random direction...
Definition: OgreVector2.h:510
static const Vector2 ZERO
Definition: OgreVector2.h:552
bool isNaN() const
Check whether this vector contains valid values.
Definition: OgreVector2.h:546
static Real Sqrt(Real fValue)
Definition: OgreMath.h:323
Real distance(const Vector2 &rhs) const
Returns the distance to another vector.
Definition: OgreVector2.h:352
Standard 2-dimensional vector.
Definition: OgreVector2.h:51
static const Vector2 UNIT_SCALE
Definition: OgreVector2.h:557
Real normalise()
Normalises the vector.
Definition: OgreVector2.h:400
static const Vector2 NEGATIVE_UNIT_Y
Definition: OgreVector2.h:556
void makeFloor(const Vector2 &cmp)
Sets this vector's components to the minimum of its own and the ones of the passed in vector...
Definition: OgreVector2.h:454
Vector2(const int afCoordinate[2])
Definition: OgreVector2.h:77
bool isZeroLength(void) const
Returns true if this vector is zero length.
Definition: OgreVector2.h:522
const Real * ptr() const
Pointer accessor for direct copying.
Definition: OgreVector2.h:116
Vector2 perpendicular(void) const
Generates a vector perpendicular to this vector (eg an 'up' vector).
Definition: OgreVector2.h:480
static bool isNaN(Real f)
Definition: OgreMath.h:247
Vector2(Real *const r)
Definition: OgreVector2.h:83
void makeCeil(const Vector2 &cmp)
Sets this vector's components to the maximum of its own and the ones of the passed in vector...
Definition: OgreVector2.h:467
Real squaredLength() const
Returns the square of the length(magnitude) of the vector.
Definition: OgreVector2.h:341
Real * ptr()
Pointer accessor for direct copying.
Definition: OgreVector2.h:111
Vector2 normalisedCopy(void) const
As normalise, except that this vector is unaffected and the normalised vector is returned as a copy...
Definition: OgreVector2.h:531
bool operator==(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator, (ISO C++)
bool operator!=(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator, (ISO C++)