SVector2.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "SVector.h"
5 #include "Utilities.h"
6 
7 
10 template <typename T>
11 struct vec2
12 {
13 
14  T X;
15  T Y;
16 
18  // Constructors //
20 
22  vec2()
23  : X(0), Y(0)
24  {}
25 
27  vec2(T const in)
28  : X(in), Y(in)
29  {}
30 
32  vec2(T const x, T const y)
33  : X(x), Y(y)
34  {}
35 
37  template <typename U>
38  vec2(glm::tvec2<U, glm::defaultp> const & v)
39  : X(v.x), Y(v.y)
40  {}
41 
43  template <typename U>
44  vec2(vec2<U> const & vec)
45  : X((T) vec.X), Y((T) vec.Y)
46  {}
47 
48 
50  // Index Operators //
52 
54  T operator[] (int i) const
55  {
56  switch (i)
57  {
58  case 0:
59  return X;
60  case 1:
61  return Y;
62  default:
63  return 0;
64  }
65  }
66 
68  T & operator[] (int i)
69  {
70  switch (i)
71  {
72  case 0:
73  return X;
74  case 1:
75  return Y;
76  default:
77  {
78  static T Dummy;
79  return Dummy = 0;
80  }
81  }
82  }
83 
84 
86  // Assignment //
88 
90  template <typename U>
91  vec2<T> & operator = (vec2<U> const & vec)
92  {
93  set(vec);
94  return *this;
95  }
96 
98  void reset()
99  {
100  X = Y = 0;
101  }
102 
104  void set(T const in)
105  {
106  X = Y = in;
107  }
108 
110  void set(T const in[])
111  {
112  X = in[0];
113  Y = in[1];
114  }
115 
117  template <typename U>
118  void set(vec2<U> const & other)
119  {
120  X = (T) other.X;
121  Y = (T) other.Y;
122  }
123 
124 
126  // Length Operations //
128 
130  T Length() const
131  {
132  return Sqrt<T>(X*X + Y*Y);
133  }
134 
136  friend T Length(vec2<T> const & vec)
137  {
138  return vec.Length();
139  }
140 
142  T LengthSq() const
143  {
144  return X*X + Y*Y;
145  }
146 
148  friend T LengthSq(vec2<T> const & vec)
149  {
150  return vec.LengthSq();
151  }
152 
153 
155  // Dot Product //
157 
158  T Dot(vec2<T> const & other) const
159  {
160  return X * other.X + Y * other.Y;
161  }
162 
163  friend T Dot(vec2<T> const & lhs, vec2<T> const & rhs)
164  {
165  return lhs.Dot(rhs);
166  }
167 
168 
170  // Distance //
172 
173  T GetDistanceFrom(vec2<T> const & v) const
174  {
175  return (*this - v).Length();
176  }
177 
178  friend T Distance(vec2<T> const & lhs, vec2<T> const & rhs)
179  {
180  return (lhs - rhs).Length();
181  }
182 
183  T const GetDistanceSqFrom(vec2<T> const & v) const
184  {
185  return (* this - v).LengthSq();
186  }
187 
188  friend T DistanceSq(vec2<T> const & lhs, vec2<T> const & rhs)
189  {
190  return (lhs - rhs).LengthSq();
191  }
192 
193 
195  // Interpolation //
197 
198  template <typename Real>
199  vec2<T> GetInterpolated(vec2<T> const & v, Real const d) const
200  {
201  vec2<T> ret;
202  Real inv = (Real) 1 - d;
203  ret.X = (T) ((Real) v.X * inv + (Real) X * d);
204  ret.Y = (T) ((Real) v.Y * inv + (Real) Y * d);
205  return ret;
206  }
207 
208  template <typename Real>
209  friend vec2<T> Interpolate(vec2<T> const & lhs, vec2<T> const & rhs, Real const d)
210  {
211  return lhs.GetInterpolated(rhs, d);
212  }
213 
214 
216  // Normalization //
218 
220  {
221  vec2<T> ret = *this;
222  ret.Normalize();
223  return ret;
224  }
225 
228  {
229  T const len = Length();
230 
231  X /= len;
232  Y /= len;
233 
234  return *this;
235  }
236 
237  friend vec2<T> Normalize(vec2<T> const & v)
238  {
239  vec2<T> ret = v;
240  ret.Normalize();
241  return ret;
242  }
243 
244 
246  // Basic Arithmetic //
248 
249  vec2<T> operator + (vec2<T> const & v) const
250  {
251  vec2<T> ret;
252  ret.X = X + v.X;
253  ret.Y = Y + v.Y;
254  return ret;
255  }
256 
257  vec2<T> operator - (vec2<T> const & v) const
258  {
259  vec2<T> ret;
260  ret.X = X - v.X;
261  ret.Y = Y - v.Y;
262  return ret;
263  }
264 
265  vec2<T> operator * (vec2<T> const & v) const
266  {
267  vec2<T> ret;
268  ret.X = X * v.X;
269  ret.Y = Y * v.Y;
270  return ret;
271  }
272 
273  vec2<T> operator / (vec2<T> const & v) const
274  {
275  vec2<T> ret;
276  ret.X = X / v.X;
277  ret.Y = Y / v.Y;
278  return ret;
279  }
280 
281 
283  // Scalar Arithmetic //
285 
286  vec2<T> operator + (T const s) const
287  {
288  vec2<T> ret;
289  ret.X = X + s;
290  ret.Y = Y + s;
291  return ret;
292  }
293 
294  vec2<T> operator - (T const s) const
295  {
296  vec2<T> ret;
297  ret.X = X - s;
298  ret.Y = Y - s;
299  return ret;
300  }
301 
302  vec2<T> operator * (T const s) const
303  {
304  vec2<T> ret;
305  ret.X = X * s;
306  ret.Y = Y * s;
307  return ret;
308  }
309 
310  vec2<T> operator / (T const s) const
311  {
312  vec2<T> ret;
313  ret.X = X / s;
314  ret.Y = Y / s;
315  return ret;
316  }
317 
318  friend vec2<T> operator + (T const lhs, vec2<T> const & rhs)
319  {
320  vec2<T> ret;
321  ret.X = lhs + rhs.X;
322  ret.Y = lhs + rhs.Y;
323  return ret;
324  }
325 
326  friend vec2<T> operator - (T const lhs, vec2<T> const & rhs)
327  {
328  vec2<T> ret;
329  ret.X = lhs - rhs.X;
330  ret.Y = lhs - rhs.Y;
331  return ret;
332  }
333 
334  friend vec2<T> operator * (T const lhs, vec2<T> const & rhs)
335  {
336  vec2<T> ret;
337  ret.X = lhs * rhs.X;
338  ret.Y = lhs * rhs.Y;
339  return ret;
340  }
341 
342  friend vec2<T> operator / (T const lhs, vec2<T> const & rhs)
343  {
344  vec2<T> ret;
345  ret.X = lhs / rhs.X;
346  ret.Y = lhs / rhs.Y;
347  return ret;
348  }
349 
350 
352  // Basic Arithmetic Assignment //
354 
356  {
357  X += v.X;
358  Y += v.Y;
359 
360  return * this;
361  }
362 
364  {
365  X -= v.X;
366  Y -= v.Y;
367 
368  return * this;
369  }
370 
372  {
373  X *= v.X;
374  Y *= v.Y;
375 
376  return * this;
377  }
378 
380  {
381  X /= v.X;
382  Y /= v.Y;
383 
384  return * this;
385  }
386 
387 
389  // Scalar Arithmetic Assignment //
391 
392  vec2<T> & operator += (T const s)
393  {
394  X += s;
395  Y += s;
396 
397  return * this;
398  }
399 
400  vec2<T> & operator -= (T const s)
401  {
402  X -= s;
403  Y -= s;
404 
405  return * this;
406  }
407 
408  vec2<T> & operator *= (T const s)
409  {
410  X *= s;
411  Y *= s;
412 
413  return * this;
414  }
415 
416  vec2<T> & operator /= (T const s)
417  {
418  X /= s;
419  Y /= s;
420 
421  return * this;
422  }
423 
425  // Negation //
427 
428  vec2<T> const operator - () const
429  {
430  vec2<T> ret;
431  ret.X = -X;
432  ret.Y = -Y;
433  return ret;
434  }
435 
436 
438  // Inequality operator (for sorting) //
440 
441  bool operator < (vec2<T> const & v) const
442  {
443  return
444  (X < v.X) ||
445  (X == v.X && Y < v.Y);
446  }
447 
448 
450  // Equality operators //
452 
453  bool operator == (vec2<T> const & v) const
454  {
455  return Equals(v);
456  }
457 
458  bool operator != (vec2<T> const & v) const
459  {
460  return ! Equals(v);
461  }
462 
463  bool Equals(vec2<T> const & v, T const Epsilon = RoundingError<T>::Value()) const
464  {
465  return
466  ::Equals(X, v.X, Epsilon) &&
467  ::Equals(Y, v.Y, Epsilon);
468  }
469 
470 
472  // Extrema //
474 
475  friend vec2<T> ComponentMinimum(vec2<T> const & lhs, vec2<T> const & rhs)
476  {
477  vec2<T> ret;
478  ret.X = Minimum(lhs.X, rhs.X);
479  ret.Y = Minimum(lhs.Y, rhs.Y);
480  return ret;
481  }
482 
483  friend vec2<T> ComponentMaximum(vec2<T> const & lhs, vec2<T> const & rhs)
484  {
485  vec2<T> ret;
486  ret.X = Maximum(lhs.X, rhs.X);
487  ret.Y = Maximum(lhs.Y, rhs.Y);
488  return ret;
489  }
490 
491 
493  // GLM //
495 
496  glm::vec2 const ToGLM() const
497  {
498  return glm::vec2(X, Y);
499  }
500 
501  static vec2<T> FromGLM(glm::vec2 const & Vec)
502  {
503  return vec2<T>(Vec.x, Vec.y);
504  }
505 
506 
508  // Swizzle //
510 
511  vec2<T> YX() const
512  {
513  return vec2<T>(Y, X);
514  }
515 
516  vec2<T> XX() const
517  {
518  return vec2<T>(X, X);
519  }
520 
521  vec2<T> YY() const
522  {
523  return vec2<T>(Y, Y);
524  }
525 
526 
528  // Formatting //
530 
531  friend std::ostream & operator << (std::ostream & stream, vec2<T> const & vec)
532  {
533  return stream << vec.X << " " << vec.Y;
534  }
535 
536 
538  // Rotation //
540 
541  vec2<T> Rotate(T const radians) const
542  {
543  vec2<T> Ret;
544  T C = Cos(radians);
545  T S = Sin(radians);
546  Ret.X = X * C - Y * S;
547  Ret.Y = X * S + Y * C;
548  return Ret;
549  }
550 
551  T GetAngle() const
552  {
553  return ArcTan(Y, X);
554  }
555 
556 };
557 
558 typedef vec2<f32> vec2f;
559 typedef vec2<f64> vec2d;
560 typedef vec2<s32> vec2i;
561 typedef vec2<u32> vec2u;
vec2< T > & Normalize()
Normalize this vector.
Definition: SVector2.h:227
vec2< T > operator+(vec2< T > const &v) const
Definition: SVector2.h:249
T Sin(T const value)
Definition: Utilities.h:30
bool operator!=(vec2< T > const &v) const
Definition: SVector2.h:458
vec2< f32 > vec2f
Definition: SVector2.h:558
vec2< T > operator/(vec2< T > const &v) const
Definition: SVector2.h:273
T LengthSq() const
Squared-length of vector (computationally fast)
Definition: SVector2.h:142
vec2< T > XX() const
Definition: SVector2.h:516
T operator[](int i) const
Note: Out-of-bounds access returns a dummy value without error.
Definition: SVector2.h:54
vec2(glm::tvec2< U, glm::defaultp > const &v)
GLM constructor.
Definition: SVector2.h:38
friend T Distance(vec2< T > const &lhs, vec2< T > const &rhs)
Definition: SVector2.h:178
2D vector
Definition: SVector2.h:11
T X
Definition: SVector2.h:14
vec2< T > Rotate(T const radians) const
Definition: SVector2.h:541
friend vec2< T > ComponentMaximum(vec2< T > const &lhs, vec2< T > const &rhs)
Definition: SVector2.h:483
vec2< u32 > vec2u
Definition: SVector2.h:561
vec2(T const x, T const y)
Explicit constructor.
Definition: SVector2.h:32
T Y
Definition: SVector2.h:15
vec2()
Default constructor.
Definition: SVector2.h:22
friend T DistanceSq(vec2< T > const &lhs, vec2< T > const &rhs)
Definition: SVector2.h:188
friend vec2< T > Normalize(vec2< T > const &v)
Definition: SVector2.h:237
vec2< T > YX() const
Definition: SVector2.h:511
friend vec2< T > ComponentMinimum(vec2< T > const &lhs, vec2< T > const &rhs)
Definition: SVector2.h:475
vec2< T > & operator+=(vec2< T > const &v)
Definition: SVector2.h:355
T Dot(vec2< T > const &other) const
Definition: SVector2.h:158
friend T LengthSq(vec2< T > const &vec)
Squared-length of vector (computationally fast)
Definition: SVector2.h:148
vec2< T > & operator/=(vec2< T > const &v)
Definition: SVector2.h:379
T Cos(T const value)
Definition: Utilities.h:37
glm::vec2 const ToGLM() const
Definition: SVector2.h:496
bool Equals(vec2< T > const &v, T const Epsilon=RoundingError< T >::Value()) const
Definition: SVector2.h:463
vec2< T > GetInterpolated(vec2< T > const &v, Real const d) const
Definition: SVector2.h:199
vec2< T > & operator=(vec2< U > const &vec)
Generic vector assignment operator.
Definition: SVector2.h:91
friend T Length(vec2< T > const &vec)
Length of vector.
Definition: SVector2.h:136
static vec2< T > FromGLM(glm::vec2 const &Vec)
Definition: SVector2.h:501
friend vec2< T > Interpolate(vec2< T > const &lhs, vec2< T > const &rhs, Real const d)
Definition: SVector2.h:209
T ArcTan(T const value)
Definition: Utilities.h:51
T Length() const
Length of vector.
Definition: SVector2.h:130
vec2< T > & operator-=(vec2< T > const &v)
Definition: SVector2.h:363
T const GetDistanceSqFrom(vec2< T > const &v) const
Definition: SVector2.h:183
vec2< T > YY() const
Definition: SVector2.h:521
vec2< T > & operator*=(vec2< T > const &v)
Definition: SVector2.h:371
vec2< T > operator*(vec2< T > const &v) const
Definition: SVector2.h:265
vec2(vec2< U > const &vec)
Generic vec2 constructor.
Definition: SVector2.h:44
vec2< f64 > vec2d
Definition: SVector2.h:559
friend T Dot(vec2< T > const &lhs, vec2< T > const &rhs)
Definition: SVector2.h:163
bool operator==(vec2< T > const &v) const
Definition: SVector2.h:453
vec2(T const in)
Scalar constructor.
Definition: SVector2.h:27
void reset()
Sets all values to 0.
Definition: SVector2.h:98
vec2< s32 > vec2i
Definition: SVector2.h:560
T GetAngle() const
Definition: SVector2.h:551
vec2< T > const operator-() const
Definition: SVector2.h:428
Helper template to enable template-dependant epsilon values.
Definition: ionComparison.h:30
vec2< T > GetNormalized() const
Definition: SVector2.h:219
T GetDistanceFrom(vec2< T > const &v) const
Definition: SVector2.h:173