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-2013 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 
251  // arithmetic updates
252  inline Vector2& operator += ( const Vector2& rkVector )
253  {
254  x += rkVector.x;
255  y += rkVector.y;
256 
257  return *this;
258  }
259 
260  inline Vector2& operator += ( const Real fScaler )
261  {
262  x += fScaler;
263  y += fScaler;
264 
265  return *this;
266  }
267 
268  inline Vector2& operator -= ( const Vector2& rkVector )
269  {
270  x -= rkVector.x;
271  y -= rkVector.y;
272 
273  return *this;
274  }
275 
276  inline Vector2& operator -= ( const Real fScaler )
277  {
278  x -= fScaler;
279  y -= fScaler;
280 
281  return *this;
282  }
283 
284  inline Vector2& operator *= ( const Real fScalar )
285  {
286  x *= fScalar;
287  y *= fScalar;
288 
289  return *this;
290  }
291 
292  inline Vector2& operator *= ( const Vector2& rkVector )
293  {
294  x *= rkVector.x;
295  y *= rkVector.y;
296 
297  return *this;
298  }
299 
300  inline Vector2& operator /= ( const Real fScalar )
301  {
302  assert( fScalar != 0.0 );
303 
304  Real fInv = 1.0f / fScalar;
305 
306  x *= fInv;
307  y *= fInv;
308 
309  return *this;
310  }
311 
312  inline Vector2& operator /= ( const Vector2& rkVector )
313  {
314  x /= rkVector.x;
315  y /= rkVector.y;
316 
317  return *this;
318  }
319 
327  inline Real length () const
328  {
329  return Math::Sqrt( x * x + y * y );
330  }
331 
342  inline Real squaredLength () const
343  {
344  return x * x + y * y;
345  }
346 
354  inline Real distance(const Vector2& rhs) const
355  {
356  return (*this - rhs).length();
357  }
358 
369  inline Real squaredDistance(const Vector2& rhs) const
370  {
371  return (*this - rhs).squaredLength();
372  }
373 
388  inline Real dotProduct(const Vector2& vec) const
389  {
390  return x * vec.x + y * vec.y;
391  }
392 
403  inline Real normalise()
404  {
405  Real fLength = Math::Sqrt( x * x + y * y);
406 
407  // Will also work for zero-sized vectors, but will change nothing
408  // We're not using epsilons because we don't need to.
409  // Read http://www.ogre3d.org/forums/viewtopic.php?f=4&t=61259
410  if ( fLength > Real(0.0f) )
411  {
412  Real fInvLength = 1.0f / fLength;
413  x *= fInvLength;
414  y *= fInvLength;
415  }
416 
417  return fLength;
418  }
419 
423  inline Vector2 midPoint( const Vector2& vec ) const
424  {
425  return Vector2(
426  ( x + vec.x ) * 0.5f,
427  ( y + vec.y ) * 0.5f );
428  }
429 
433  inline bool operator < ( const Vector2& rhs ) const
434  {
435  if( x < rhs.x && y < rhs.y )
436  return true;
437  return false;
438  }
439 
443  inline bool operator > ( const Vector2& rhs ) const
444  {
445  if( x > rhs.x && y > rhs.y )
446  return true;
447  return false;
448  }
449 
457  inline void makeFloor( const Vector2& cmp )
458  {
459  if( cmp.x < x ) x = cmp.x;
460  if( cmp.y < y ) y = cmp.y;
461  }
462 
470  inline void makeCeil( const Vector2& cmp )
471  {
472  if( cmp.x > x ) x = cmp.x;
473  if( cmp.y > y ) y = cmp.y;
474  }
475 
483  inline Vector2 perpendicular(void) const
484  {
485  return Vector2 (-y, x);
486  }
487 
491  inline Real crossProduct( const Vector2& rkVector ) const
492  {
493  return x * rkVector.y - y * rkVector.x;
494  }
495 
508  inline Vector2 randomDeviant(Radian angle) const
509  {
510  angle *= Math::RangeRandom(-1, 1);
511  Real cosa = Math::Cos(angle);
512  Real sina = Math::Sin(angle);
513  return Vector2(cosa * x - sina * y,
514  sina * x + cosa * y);
515  }
516 
518  inline bool isZeroLength(void) const
519  {
520  Real sqlen = (x * x) + (y * y);
521  return (sqlen < (1e-06 * 1e-06));
522 
523  }
524 
527  inline Vector2 normalisedCopy(void) const
528  {
529  Vector2 ret = *this;
530  ret.normalise();
531  return ret;
532  }
533 
537  inline Vector2 reflect(const Vector2& normal) const
538  {
539  return Vector2( *this - ( 2 * this->dotProduct(normal) * normal ) );
540  }
541 
543  inline bool isNaN() const
544  {
545  return Math::isNaN(x) || Math::isNaN(y);
546  }
547 
552  inline Ogre::Radian angleBetween(const Ogre::Vector2& other) const
553  {
554  Ogre::Real lenProduct = length() * other.length();
555  // Divide by zero check
556  if(lenProduct < 1e-6f)
557  lenProduct = 1e-6f;
558 
559  Ogre::Real f = dotProduct(other) / lenProduct;
560 
561  f = Ogre::Math::Clamp(f, (Ogre::Real)-1.0, (Ogre::Real)1.0);
562  return Ogre::Math::ACos(f);
563  }
564 
570  inline Ogre::Radian angleTo(const Ogre::Vector2& other) const
571  {
572  Ogre::Radian angle = angleBetween(other);
573 
574  if (crossProduct(other)<0)
575  angle = (Ogre::Radian)Ogre::Math::TWO_PI - angle;
576 
577  return angle;
578  }
579 
580  // special points
581  static const Vector2 ZERO;
582  static const Vector2 UNIT_X;
583  static const Vector2 UNIT_Y;
584  static const Vector2 NEGATIVE_UNIT_X;
585  static const Vector2 NEGATIVE_UNIT_Y;
586  static const Vector2 UNIT_SCALE;
587 
590  inline _OgreExport friend std::ostream& operator <<
591  ( std::ostream& o, const Vector2& v )
592  {
593  o << "Vector2(" << v.x << ", " << v.y << ")";
594  return o;
595  }
596  };
600 }
601 #endif

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Wed Oct 16 2013 14:35:46