Leonetienne/Eule
Homemade math library, mainly targetted towards computer graphics
Public Member Functions | Public Attributes | Static Public Attributes | Friends | List of all members
Eule::Vector2< T > Class Template Reference

Representation of a 2d vector. More...

#include <Vector2.h>

Public Member Functions

 Vector2 ()
 
 Vector2 (T _x, T _y)
 
 Vector2 (const Vector2< T > &other)=default
 
 Vector2 (Vector2< T > &&other) noexcept=default
 
double DotProduct (const Vector2< T > &other) const
 Will compute the dot product to another Vector2. More...
 
double CrossProduct (const Vector2< T > &other) const
 Will compute the cross product to another Vector2. More...
 
double SqrMagnitude () const
 Will compute the square magnitude. More...
 
double Magnitude () const
 Will compute the magnitude. More...
 
Vector2< double > Normalize () const
 Will return the normalization of this vector. More...
 
void NormalizeSelf ()
 Will normalize this vector. More...
 
Vector2< T > VectorScale (const Vector2< T > &scalar) const
 Will scale self.n by scalar.n. More...
 
void LerpSelf (const Vector2< T > &other, double t)
 Will lerp itself towards other by t. More...
 
Vector2< double > Lerp (const Vector2< T > &other, double t) const
 Will return a lerp result between this and another vector. More...
 
bool Similar (const Vector2< T > &other, double epsilon=0.00001) const
 Will compare if two vectors are similar to a certain epsilon value. More...
 
Vector2< int > ToInt () const
 Will convert this vector to a Vector2i. More...
 
Vector2< double > ToDouble () const
 Will convert this vector to a Vector2d. More...
 
T & operator[] (std::size_t idx)
 
const T & operator[] (std::size_t idx) const
 
Vector2< T > operator+ (const Vector2< T > &other) const
 
void operator+= (const Vector2< T > &other)
 
Vector2< T > operator- (const Vector2< T > &other) const
 
void operator-= (const Vector2< T > &other)
 
Vector2< T > operator* (const T scale) const
 
void operator*= (const T scale)
 
Vector2< T > operator/ (const T scale) const
 
void operator/= (const T scale)
 
Vector2< T > operator- () const
 
 operator Vector3< T > () const
 
 operator Vector4< T > () const
 Conversion method. More...
 
void operator= (const Vector2< T > &other)
 Conversion method. More...
 
void operator= (Vector2< T > &&other) noexcept
 
bool operator== (const Vector2< T > &other) const
 
bool operator!= (const Vector2< T > &other) const
 

Public Attributes

x
 
y
 

Static Public Attributes

static const Vector2< double > up
 
static const Vector2< double > down
 
static const Vector2< double > right
 
static const Vector2< double > left
 
static const Vector2< double > one
 
static const Vector2< double > zero
 

Friends

std::ostream & operator<< (std::ostream &os, const Vector2< T > &v)
 
std::wostream & operator<< (std::wostream &os, const Vector2< T > &v)
 

Detailed Description

template<typename T>
class Eule::Vector2< T >

Representation of a 2d vector.

Contains a lot of utility methods.

Definition at line 14 of file Vector2.h.

Constructor & Destructor Documentation

◆ Vector2() [1/4]

template<typename T >
Eule::Vector2< T >::Vector2 ( )
inline

Definition at line 17 of file Vector2.h.

17 : x{ 0 }, y{ 0 } {}

◆ Vector2() [2/4]

template<typename T >
Eule::Vector2< T >::Vector2 ( _x,
_y 
)
inline

Definition at line 18 of file Vector2.h.

18 : x{ _x }, y{ _y } {}

◆ Vector2() [3/4]

template<typename T >
Eule::Vector2< T >::Vector2 ( const Vector2< T > &  other)
default

◆ Vector2() [4/4]

template<typename T >
Eule::Vector2< T >::Vector2 ( Vector2< T > &&  other)
defaultnoexcept

Member Function Documentation

◆ CrossProduct()

template<typename T >
double Vector2::CrossProduct ( const Vector2< T > &  other) const

Will compute the cross product to another Vector2.

Definition at line 65 of file Vector2.cpp.

66 {
67  int iCross = (x * other.y) -
68  (y * other.x);
69 
70  return (double)iCross;
71 }

◆ DotProduct()

template<typename T >
double Vector2::DotProduct ( const Vector2< T > &  other) const

Will compute the dot product to another Vector2.

Definition at line 47 of file Vector2.cpp.

48 {
49  int iDot = (x * other.x) +
50  (y * other.y);
51 
52  return (double)iDot;
53 }

◆ Lerp()

template<typename T >
Vector2< double > Vector2::Lerp ( const Vector2< T > &  other,
double  t 
) const

Will return a lerp result between this and another vector.

Definition at line 256 of file Vector2.cpp.

257 {
258  Vector2d copy(this->ToDouble());
259  copy.LerpSelf(other.ToDouble(), t);
260 
261  return copy;
262 }

◆ LerpSelf()

template<typename T >
void Vector2::LerpSelf ( const Vector2< T > &  other,
double  t 
)

Will lerp itself towards other by t.

Definition at line 238 of file Vector2.cpp.

239 {
240  const double it = 1.0 - t; // Inverse t
241 
242  x = (int)(it * (double)x + t * (double)other.x);
243  y = (int)(it * (double)y + t * (double)other.y);
244 
245  return;
246 }

◆ Magnitude()

template<typename T >
double Vector2::Magnitude

Will compute the magnitude.

Definition at line 90 of file Vector2.cpp.

91 {
92  return sqrt(SqrMagnitude());
93 }

◆ Normalize()

template<typename T >
Vector2< double > Vector2::Normalize

Will return the normalization of this vector.

Definition at line 137 of file Vector2.cpp.

138 {
139  Vector2<double> norm(x, y);
140  norm.NormalizeSelf();
141 
142  return norm;
143 }

◆ NormalizeSelf()

template<typename T >
void Vector2::NormalizeSelf ( )

Will normalize this vector.

Definition at line 187 of file Vector2.cpp.

188 {
189  std::cerr << "Stop normalizing int-vectors!!" << std::endl;
190  x = 0;
191  y = 0;
192 
193  return;
194 }

◆ operator Vector3< T >()

template<typename T >
Vector2::operator Vector3< T >

Definition at line 674 of file Vector2.cpp.

675 {
676  return Vector3<T>(x, y, 0);
677 }

◆ operator Vector4< T >()

template<typename T >
Vector2::operator Vector4< T >

Conversion method.

Definition at line 680 of file Vector2.cpp.

681 {
682  return Vector4<T>(x, y, 0, 0);
683 }

◆ operator!=()

template<typename T >
bool Vector2::operator!= ( const Vector2< T > &  other) const

Definition at line 656 of file Vector2.cpp.

657 {
658  return !operator==(other);
659 }

◆ operator*()

template<typename T >
Vector2< T > Vector2::operator* ( const T  scale) const

Definition at line 503 of file Vector2.cpp.

504 {
505  return Vector2<T>(
506  x * scale,
507  y * scale
508  );
509 }

◆ operator*=()

template<typename T >
void Vector2::operator*= ( const T  scale)

Definition at line 542 of file Vector2.cpp.

543 {
544  x *= scale;
545  y *= scale;
546  return;
547 }

◆ operator+()

template<typename T >
Vector2< T > Vector2::operator+ ( const Vector2< T > &  other) const

Definition at line 346 of file Vector2.cpp.

347 {
348  return Vector2<T>(
349  x + other.x,
350  y + other.y
351  );
352 }

◆ operator+=()

template<typename T >
void Vector2::operator+= ( const Vector2< T > &  other)

Definition at line 385 of file Vector2.cpp.

386 {
387  x += other.x;
388  y += other.y;
389  return;
390 }

◆ operator-() [1/2]

template<typename T >
Vector2< T > Vector2::operator-

Definition at line 662 of file Vector2.cpp.

663 {
664  return Vector2<T>(
665  -x,
666  -y
667  );
668 }

◆ operator-() [2/2]

template<typename T >
Vector2< T > Vector2::operator- ( const Vector2< T > &  other) const

Definition at line 424 of file Vector2.cpp.

425 {
426  return Vector2<T>(
427  x - other.x,
428  y - other.y
429  );
430 }

◆ operator-=()

template<typename T >
void Vector2::operator-= ( const Vector2< T > &  other)

Definition at line 463 of file Vector2.cpp.

464 {
465  x -= other.x;
466  y -= other.y;
467  return;
468 }

◆ operator/()

template<typename T >
Vector2< T > Vector2::operator/ ( const T  scale) const

Definition at line 582 of file Vector2.cpp.

583 {
584  return Vector2<T>(
585  x / scale,
586  y / scale
587  );
588 }

◆ operator/=()

template<typename T >
void Vector2::operator/= ( const T  scale)

Definition at line 620 of file Vector2.cpp.

621 {
622  x /= scale;
623  y /= scale;
624  return;
625 }

◆ operator=() [1/2]

template<typename T >
void Vector2::operator= ( const Vector2< T > &  other)

Conversion method.

Definition at line 630 of file Vector2.cpp.

631 {
632  x = other.x;
633  y = other.y;
634 
635  return;
636 }

◆ operator=() [2/2]

template<typename T >
void Vector2::operator= ( Vector2< T > &&  other)
noexcept

Definition at line 639 of file Vector2.cpp.

640 {
641  x = std::move(other.x);
642  y = std::move(other.y);
643 
644  return;
645 }

◆ operator==()

template<typename T >
bool Vector2::operator== ( const Vector2< T > &  other) const

Definition at line 648 of file Vector2.cpp.

649 {
650  return
651  (x == other.x) &&
652  (y == other.y);
653 }

◆ operator[]() [1/2]

template<typename T >
T & Vector2::operator[] ( std::size_t  idx)

Definition at line 267 of file Vector2.cpp.

268 {
269  switch (idx)
270  {
271  case 0:
272  return x;
273  case 1:
274  return y;
275  default:
276  throw std::out_of_range("Array descriptor on Vector2<T> out of range!");
277  }
278 }

◆ operator[]() [2/2]

template<typename T >
const T & Vector2::operator[] ( std::size_t  idx) const

Definition at line 281 of file Vector2.cpp.

282 {
283  switch (idx)
284  {
285  case 0:
286  return x;
287  case 1:
288  return y;
289  default:
290  throw std::out_of_range("Array descriptor on Vector2<T> out of range!");
291  }
292 }

◆ Similar()

template<typename T >
bool Vector2::Similar ( const Vector2< T > &  other,
double  epsilon = 0.00001 
) const

Will compare if two vectors are similar to a certain epsilon value.

Definition at line 295 of file Vector2.cpp.

296 {
297  return
298  (::Math::Similar(x, other.x, epsilon)) &&
299  (::Math::Similar(y, other.y, epsilon))
300  ;
301 }

◆ SqrMagnitude()

template<typename T >
double Vector2::SqrMagnitude ( ) const

Will compute the square magnitude.

Definition at line 83 of file Vector2.cpp.

84 {
85  int iSqrMag = x*x + y*y;
86  return (double)iSqrMag;
87 }

◆ ToDouble()

template<typename T >
Vector2< double > Vector2::ToDouble

Will convert this vector to a Vector2d.

Definition at line 310 of file Vector2.cpp.

311 {
312  return Vector2<double>((double)x, (double)y);
313 }

◆ ToInt()

template<typename T >
Vector2< int > Vector2::ToInt

Will convert this vector to a Vector2i.

Definition at line 304 of file Vector2.cpp.

305 {
306  return Vector2<int>((int)x, (int)y);
307 }

◆ VectorScale()

template<typename T >
Vector2< int > Vector2::VectorScale ( const Vector2< T > &  scalar) const

Will scale self.n by scalar.n.

Definition at line 127 of file Vector2.cpp.

128 {
129  return Vector2<int>(
130  x * scalar.x,
131  y * scalar.y
132  );
133 }

Friends And Related Function Documentation

◆ operator<< [1/2]

template<typename T >
std::ostream& operator<< ( std::ostream &  os,
const Vector2< T > &  v 
)
friend

Definition at line 80 of file Vector2.h.

81  {
82  return os << "[x: " << v.x << " y: " << v.y << "]";
83  }

◆ operator<< [2/2]

template<typename T >
std::wostream& operator<< ( std::wostream &  os,
const Vector2< T > &  v 
)
friend

Definition at line 84 of file Vector2.h.

85  {
86  return os << L"[x: " << v.x << L" y: " << v.y << L"]";
87  }

Member Data Documentation

◆ down

template<typename T >
const Vector2< double > Vector2::down
static

Definition at line 94 of file Vector2.h.

◆ left

template<typename T >
const Vector2< double > Vector2::left
static

Definition at line 96 of file Vector2.h.

◆ one

template<typename T >
const Vector2< double > Vector2::one
static

Definition at line 97 of file Vector2.h.

◆ right

template<typename T >
const Vector2< double > Vector2::right
static

Definition at line 95 of file Vector2.h.

◆ up

template<typename T >
const Vector2< double > Vector2::up
static

Definition at line 93 of file Vector2.h.

◆ x

template<typename T >
T Eule::Vector2< T >::x

Definition at line 89 of file Vector2.h.

◆ y

template<typename T >
T Eule::Vector2< T >::y

Definition at line 90 of file Vector2.h.

◆ zero

template<typename T >
const Vector2< double > Vector2::zero
static

Definition at line 98 of file Vector2.h.


The documentation for this class was generated from the following files:
Eule::Vector3
Representation of a 3d vector.
Definition: Matrix4x4.h:9
Eule::Vector2::SqrMagnitude
double SqrMagnitude() const
Will compute the square magnitude.
Definition: Vector2.cpp:83
Eule::Vector2::ToDouble
Vector2< double > ToDouble() const
Will convert this vector to a Vector2d.
Definition: Vector2.cpp:310
Eule::Vector2< double >
Eule::Vector2::x
T x
Definition: Vector2.h:89
Eule::Vector2::y
T y
Definition: Vector2.h:90
Eule::Math::Similar
static constexpr bool Similar(const double a, const double b, const double epsilon=0.00001)
Compares two double values with a given accuracy.
Eule::Vector2::operator==
bool operator==(const Vector2< T > &other) const
Definition: Vector2.cpp:648
Eule::Vector4
Representation of a 4d vector.
Definition: Vector2.h:8