My Project
ParaVector2.h
1 #pragma once
2 
3 #include "ParaMath.h"
4 
5 namespace ParaEngine
6 {
7  class Matrix4;
8 
16  class Vector2
17  {
18  public:
19  float x, y;
20 
21  public:
22  inline Vector2()
23  {
24  }
25 
26  inline Vector2(const float fX, const float fY )
27  : x( fX ), y( fY )
28  {
29  }
30 
31  inline explicit Vector2( const float scaler )
32  : x( scaler), y( scaler )
33  {
34  }
35 
36  inline explicit Vector2( const float afCoordinate[2] )
37  : x( afCoordinate[0] ),
38  y( afCoordinate[1] )
39  {
40  }
41 
42  inline explicit Vector2( const int afCoordinate[2] )
43  {
44  x = (float)afCoordinate[0];
45  y = (float)afCoordinate[1];
46  }
47 
48  inline explicit Vector2( float* const r )
49  : x( r[0] ), y( r[1] )
50  {
51  }
52 
53  inline float operator [] ( const size_t i ) const
54  {
55  assert( i < 2 );
56 
57  return *(&x+i);
58  }
59 
60  inline float& operator [] ( const size_t i )
61  {
62  assert( i < 2 );
63 
64  return *(&x+i);
65  }
66 
68  inline float* ptr()
69  {
70  return &x;
71  }
73  inline const float* ptr() const
74  {
75  return &x;
76  }
77 
82  inline Vector2& operator = ( const Vector2& rkVector )
83  {
84  x = rkVector.x;
85  y = rkVector.y;
86 
87  return *this;
88  }
89 
90  inline Vector2& operator = ( const float fScalar)
91  {
92  x = fScalar;
93  y = fScalar;
94 
95  return *this;
96  }
97 
98  inline bool operator == ( const Vector2& rkVector ) const
99  {
100  return ( x == rkVector.x && y == rkVector.y );
101  }
102 
103  inline bool operator != ( const Vector2& rkVector ) const
104  {
105  return ( x != rkVector.x || y != rkVector.y );
106  }
107 
108  // arithmetic operations
109  inline Vector2 operator + ( const Vector2& rkVector ) const
110  {
111  return Vector2(
112  x + rkVector.x,
113  y + rkVector.y);
114  }
115 
116  inline Vector2 operator - ( const Vector2& rkVector ) const
117  {
118  return Vector2(
119  x - rkVector.x,
120  y - rkVector.y);
121  }
122 
123  inline Vector2 operator * ( const float fScalar ) const
124  {
125  return Vector2(
126  x * fScalar,
127  y * fScalar);
128  }
129 
130  inline Vector2 operator * ( const Vector2& rhs) const
131  {
132  return Vector2(
133  x * rhs.x,
134  y * rhs.y);
135  }
136 
137  inline Vector2 operator * (const Matrix4& mat) const;
138 
139  inline Vector2 operator / ( const float fScalar ) const
140  {
141  assert( fScalar != 0.0f );
142 
143  float fInv = 1.0f / fScalar;
144 
145  return Vector2(
146  x * fInv,
147  y * fInv);
148  }
149 
150  inline Vector2 operator / ( const Vector2& rhs) const
151  {
152  return Vector2(
153  x / rhs.x,
154  y / rhs.y);
155  }
156 
157  inline const Vector2& operator + () const
158  {
159  return *this;
160  }
161 
162  inline Vector2 operator - () const
163  {
164  return Vector2(-x, -y);
165  }
166 
167  // overloaded operators to help Vector2
168  inline friend Vector2 operator * ( const float fScalar, const Vector2& rkVector )
169  {
170  return Vector2(
171  fScalar * rkVector.x,
172  fScalar * rkVector.y);
173  }
174 
175  inline friend Vector2 operator / ( const float fScalar, const Vector2& rkVector )
176  {
177  return Vector2(
178  fScalar / rkVector.x,
179  fScalar / rkVector.y);
180  }
181 
182  inline friend Vector2 operator + (const Vector2& lhs, const float rhs)
183  {
184  return Vector2(
185  lhs.x + rhs,
186  lhs.y + rhs);
187  }
188 
189  inline friend Vector2 operator + (const float lhs, const Vector2& rhs)
190  {
191  return Vector2(
192  lhs + rhs.x,
193  lhs + rhs.y);
194  }
195 
196  inline friend Vector2 operator - (const Vector2& lhs, const float rhs)
197  {
198  return Vector2(
199  lhs.x - rhs,
200  lhs.y - rhs);
201  }
202 
203  inline friend Vector2 operator - (const float lhs, const Vector2& rhs)
204  {
205  return Vector2(
206  lhs - rhs.x,
207  lhs - rhs.y);
208  }
209  // arithmetic updates
210  inline Vector2& operator += ( const Vector2& rkVector )
211  {
212  x += rkVector.x;
213  y += rkVector.y;
214 
215  return *this;
216  }
217 
218  inline Vector2& operator += ( const float fScaler )
219  {
220  x += fScaler;
221  y += fScaler;
222 
223  return *this;
224  }
225 
226  inline Vector2& operator -= ( const Vector2& rkVector )
227  {
228  x -= rkVector.x;
229  y -= rkVector.y;
230 
231  return *this;
232  }
233 
234  inline Vector2& operator -= ( const float fScaler )
235  {
236  x -= fScaler;
237  y -= fScaler;
238 
239  return *this;
240  }
241 
242  inline Vector2& operator *= ( const float fScalar )
243  {
244  x *= fScalar;
245  y *= fScalar;
246 
247  return *this;
248  }
249 
250  inline Vector2& operator *= ( const Vector2& rkVector )
251  {
252  x *= rkVector.x;
253  y *= rkVector.y;
254 
255  return *this;
256  }
257 
258  inline Vector2& operator /= ( const float fScalar )
259  {
260  assert( fScalar != 0.0f );
261 
262  float fInv = 1.0f / fScalar;
263 
264  x *= fInv;
265  y *= fInv;
266 
267  return *this;
268  }
269 
270  inline Vector2& operator /= ( const Vector2& rkVector )
271  {
272  x /= rkVector.x;
273  y /= rkVector.y;
274 
275  return *this;
276  }
277 
285  inline float length () const
286  {
287  return Math::Sqrt( x * x + y * y );
288  }
289 
300  inline float squaredLength () const
301  {
302  return x * x + y * y;
303  }
304 
319  inline float dotProduct(const Vector2& vec) const
320  {
321  return x * vec.x + y * vec.y;
322  }
323 
333  inline float normalise()
334  {
335  float fLength = Math::Sqrt( x * x + y * y);
336 
337  // Will also work for zero-sized vectors, but will change nothing
338  if ( fLength > 1e-08 )
339  {
340  float fInvLength = 1.0f / fLength;
341  x *= fInvLength;
342  y *= fInvLength;
343  }
344 
345  return fLength;
346  }
347 
348 
349 
353  inline Vector2 midPoint( const Vector2& vec ) const
354  {
355  return Vector2(
356  ( x + vec.x ) * 0.5f,
357  ( y + vec.y ) * 0.5f );
358  }
359 
363  inline bool operator < ( const Vector2& rhs ) const
364  {
365  if( x < rhs.x && y < rhs.y )
366  return true;
367  return false;
368  }
369 
373  inline bool operator > ( const Vector2& rhs ) const
374  {
375  if( x > rhs.x && y > rhs.y )
376  return true;
377  return false;
378  }
379 
387  inline void makeFloor( const Vector2& cmp )
388  {
389  if( cmp.x < x ) x = cmp.x;
390  if( cmp.y < y ) y = cmp.y;
391  }
392 
400  inline void makeCeil( const Vector2& cmp )
401  {
402  if( cmp.x > x ) x = cmp.x;
403  if( cmp.y > y ) y = cmp.y;
404  }
405 
413  inline Vector2 perpendicular(void) const
414  {
415  return Vector2 (-y, x);
416  }
420  inline float crossProduct( const Vector2& rkVector ) const
421  {
422  return x * rkVector.y - y * rkVector.x;
423  }
444  float angle) const
445  {
446 
447  angle *= Math::UnitRandom() * Math::TWO_PI;
448  float cosa = cos(angle);
449  float sina = sin(angle);
450  return Vector2(cosa * x - sina * y,
451  sina * x + cosa * y);
452  }
453 
455  inline bool isZeroLength(void) const
456  {
457  float sqlen = (x * x) + (y * y);
458  return (sqlen < (1e-06 * 1e-06));
459 
460  }
461 
464  inline Vector2 normalisedCopy(void) const
465  {
466  Vector2 ret = *this;
467  ret.normalise();
468  return ret;
469  }
470 
474  inline Vector2 reflect(const Vector2& normal) const
475  {
476  return Vector2( *this - ( 2 * this->dotProduct(normal) * normal ) );
477  }
478 
479  // special points
480  static const Vector2 ZERO;
481  static const Vector2 UNIT_X;
482  static const Vector2 UNIT_Y;
483  static const Vector2 NEGATIVE_UNIT_X;
484  static const Vector2 NEGATIVE_UNIT_Y;
485  static const Vector2 UNIT_SCALE;
486 
489  inline friend std::ostream& operator <<
490  ( std::ostream& o, const Vector2& v )
491  {
492  o << "Vector2(" << v.x << ", " << v.y << ")";
493  return o;
494  }
495 
496  };
497 }
float normalise()
Normalises the vector.
Definition: ParaVector2.h:333
float crossProduct(const Vector2 &rkVector) const
Calculates the 2 dimensional cross-product of 2 vectors, which results in a single floating point val...
Definition: ParaVector2.h:420
bool operator<(const Vector2 &rhs) const
Returns true if the vector&#39;s scalar components are all greater that the ones of the vector it is comp...
Definition: ParaVector2.h:363
different physics engine has different winding order.
Definition: EventBinding.h:32
Vector2 normalisedCopy(void) const
As normalise, except that this vector is unaffected and the normalised vector is returned as a copy...
Definition: ParaVector2.h:464
bool operator>(const Vector2 &rhs) const
Returns true if the vector&#39;s scalar components are all smaller that the ones of the vector it is comp...
Definition: ParaVector2.h:373
float length() const
Returns the length (magnitude) of the vector.
Definition: ParaVector2.h:285
Standard 2-dimensional vector.
Definition: ParaVector2.h:16
Vector2 perpendicular(void) const
Generates a vector perpendicular to this vector (eg an &#39;up&#39; vector).
Definition: ParaVector2.h:413
bool isZeroLength(void) const
Returns true if this vector is zero length.
Definition: ParaVector2.h:455
void makeFloor(const Vector2 &cmp)
Sets this vector&#39;s components to the minimum of its own and the ones of the passed in vector...
Definition: ParaVector2.h:387
Class encapsulating a standard 4x4 homogeneous matrix.
Definition: ParaMatrix4.h:23
const float * ptr() const
Pointer accessor for direct copying.
Definition: ParaVector2.h:73
float dotProduct(const Vector2 &vec) const
Calculates the dot (scalar) product of this vector with another.
Definition: ParaVector2.h:319
Vector2 midPoint(const Vector2 &vec) const
Returns a vector at a point half way between this and the passed in vector.
Definition: ParaVector2.h:353
Vector2 reflect(const Vector2 &normal) const
Calculates a reflection vector to the plane with the given normal .
Definition: ParaVector2.h:474
Vector2 randomDeviant(float angle) const
Generates a new random vector which deviates from this vector by a given angle in a random direction...
Definition: ParaVector2.h:443
Vector2 & operator=(const Vector2 &rkVector)
Assigns the value of the other vector.
Definition: ParaVector2.h:82
float * ptr()
Pointer accessor for direct copying.
Definition: ParaVector2.h:68
float squaredLength() const
Returns the square of the length(magnitude) of the vector.
Definition: ParaVector2.h:300
void makeCeil(const Vector2 &cmp)
Sets this vector&#39;s components to the maximum of its own and the ones of the passed in vector...
Definition: ParaVector2.h:400