My Project
Loading...
Searching...
No Matches
Maths::Vec2< T > Class Template Reference

A 2D vector class template for mathematical operations. More...

#include <MVec2.hpp>

Public Member Functions

 Vec2 (T m_x, T m_y)
 Ensure that T is an arithmetic type (e.g., float, int).
 Vec2 ()=default
 Default constructor (initializes to (0,0)).
 ~Vec2 ()=default
 Destructor.
x () const
 Returns the x-component.
y () const
 Returns the y-component.
void SetX (T newX)
 Sets the x-component to a new value.
void SetY (T newY)
 Sets the y-component to a new value.
void Set (T newX, T newY)
 Sets both components of the vector.
Vec2 Normalized () const
 Returns a normalized (unit length) vector.
SqrtMagnitude () const
 Returns the squared magnitude of the vector.
Magnitude () const
 Returns the magnitude (length) of the vector.
This (int i) const
 Returns the component at index i (0 -> x, 1 -> y).
bool Equal (const Vec2 &other) const
 Checks if this vector is equal to another vector.
std::string ToString () const
 Converts the vector to a string representation.
void Normalize ()
 Normalizes the vector in place (makes it a unit vector).
Vec2< T > ToVec2 () const
 Returns the vector as itself (no conversion).
Vec3< T > ToVec3 () const
 Converts this 2D vector to a 3D vector (z-component = 0).
Vec2 operator- (const Vec2 &other) const
 Subtracts another vector from this vector.
Vec2 operator+ (const Vec2 &other) const
 Adds another vector to this vector.
Vec2 operator* (const Vec2 &other) const
 Component-wise multiplication of two vectors.
Vec2 operator* (T scalar) const
 Multiplies this vector by a scalar.
Vec2 operator/ (const Vec2 &other) const
 Component-wise division of two vectors.
Vec2 operator/ (T scalar) const
 Divides this vector by a scalar.
bool operator== (const Vec2 &other) const
 Checks if this vector is equal to another vector.
bool operator!= (const Vec2 &other) const
 Checks if this vector is not equal to another vector.

Static Public Member Functions

static Vec2 Down ()
 Returns a vector pointing down (0, -1).
static Vec2 Up ()
 Returns a vector pointing up (0, 1).
static Vec2 Left ()
 Returns a vector pointing left (-1, 0).
static Vec2 Right ()
 Returns a vector pointing right (1, 0).
static Vec2 One ()
 Returns a vector with both components set to 1 (1, 1).
static Vec2 Zero ()
 Returns a zero vector (0, 0).
static Vec2 PositiveInfinity ()
 Returns a vector with both components set to positive infinity.
static Vec2 NegativeInfinity ()
 Returns a vector with both components set to negative infinity.
static Vec2 MoveTowards (const Vec2 &current, const Vec2 &target, T maxDistanceDelta)
 Moves the current vector towards the target vector by a maximum distance delta.
static Vec2 LerpUnclamped (const Vec2 &a, const Vec2 &b, T t)
 Linearly interpolates (unclamped) between two vectors.
static Vec2 ClampMagnitude (const Vec2 &a, T maxLength)
 Clamps the magnitude of a vector to a maximum length.
static Vec2 Perpendicular (const Vec2 &inDirection)
 Returns a vector perpendicular to the input vector.
static Vec2 Lerp (const Vec2 &a, const Vec2 &b, T t)
 Linearly interpolates (clamped) between two vectors.
static Vec2 Scale (const Vec2 &a, const Vec2 &b)
 Component-wise multiplication of two vectors.
static Vec2 Min (const Vec2 &a, const Vec2 &b)
 Returns the component-wise minimum of two vectors.
static Vec2 Max (const Vec2 &a, const Vec2 &b)
 Returns the component-wise maximum of two vectors.
static T SignedAngle (const Vec2 &from, const Vec2 &to)
 Calculates the signed angle (in radians) between two vectors.
static T Distance (const Vec2 &a, const Vec2 &b)
 Calculates the distance between two vectors.
static T Angle (const Vec2 &a, const Vec2 &b)
 Calculates the angle (in radians) between two vectors.
static T Dot (const Vec2 &a, const Vec2 &b)
 Calculates the dot product of two vectors.

Public Attributes

m_x
 X component of the vector.
m_y
 Y component of the vector.

Detailed Description

template<typename T = float>
class Maths::Vec2< T >

A 2D vector class template for mathematical operations.

Template Parameters
TThe numeric type (e.g., float, double, int).

Constructor & Destructor Documentation

◆ Vec2() [1/2]

template<typename T = float>
Maths::Vec2< T >::Vec2 ( T m_x,
T m_y )

Ensure that T is an arithmetic type (e.g., float, int).

Constructs a vector with the given x and y components.

Parameters
m_xThe x-component.
m_yThe y-component.

◆ Vec2() [2/2]

template<typename T = float>
Maths::Vec2< T >::Vec2 ( )
default

Default constructor (initializes to (0,0)).

◆ ~Vec2()

template<typename T = float>
Maths::Vec2< T >::~Vec2 ( )
default

Destructor.

Member Function Documentation

◆ Angle()

template<typename T = float>
T Maths::Vec2< T >::Angle ( const Vec2< T > & a,
const Vec2< T > & b )
static

Calculates the angle (in radians) between two vectors.

Parameters
aFirst vector.
bSecond vector.
Returns
The angle between the vectors.

◆ ClampMagnitude()

template<typename T = float>
Vec2 Maths::Vec2< T >::ClampMagnitude ( const Vec2< T > & a,
T maxLength )
static

Clamps the magnitude of a vector to a maximum length.

Parameters
aThe vector to clamp.
maxLengthThe maximum allowed length.
Returns
The clamped vector.

◆ Distance()

template<typename T = float>
T Maths::Vec2< T >::Distance ( const Vec2< T > & a,
const Vec2< T > & b )
static

Calculates the distance between two vectors.

Parameters
aFirst vector.
bSecond vector.
Returns
The distance between the vectors.

◆ Dot()

template<typename T = float>
T Maths::Vec2< T >::Dot ( const Vec2< T > & a,
const Vec2< T > & b )
static

Calculates the dot product of two vectors.

Parameters
aFirst vector.
bSecond vector.
Returns
The dot product.

◆ Down()

template<typename T = float>
Vec2 Maths::Vec2< T >::Down ( )
static

Returns a vector pointing down (0, -1).

Returns
The down vector.

◆ Equal()

template<typename T = float>
bool Maths::Vec2< T >::Equal ( const Vec2< T > & other) const

Checks if this vector is equal to another vector.

Parameters
otherThe vector to compare with.
Returns
True if equal, otherwise false.

◆ Left()

template<typename T = float>
Vec2 Maths::Vec2< T >::Left ( )
static

Returns a vector pointing left (-1, 0).

Returns
The left vector.

◆ Lerp()

template<typename T = float>
Vec2 Maths::Vec2< T >::Lerp ( const Vec2< T > & a,
const Vec2< T > & b,
T t )
static

Linearly interpolates (clamped) between two vectors.

Parameters
aThe start vector.
bThe end vector.
tInterpolation factor (clamped).
Returns
The interpolated vector.

◆ LerpUnclamped()

template<typename T = float>
Vec2 Maths::Vec2< T >::LerpUnclamped ( const Vec2< T > & a,
const Vec2< T > & b,
T t )
static

Linearly interpolates (unclamped) between two vectors.

Parameters
aThe start vector.
bThe end vector.
tInterpolation factor (unclamped).
Returns
The interpolated vector.

◆ Magnitude()

template<typename T = float>
T Maths::Vec2< T >::Magnitude ( ) const

Returns the magnitude (length) of the vector.

Returns
The magnitude.

◆ Max()

template<typename T = float>
Vec2 Maths::Vec2< T >::Max ( const Vec2< T > & a,
const Vec2< T > & b )
static

Returns the component-wise maximum of two vectors.

Parameters
aFirst vector.
bSecond vector.
Returns
The vector with the maximum components.

◆ Min()

template<typename T = float>
Vec2 Maths::Vec2< T >::Min ( const Vec2< T > & a,
const Vec2< T > & b )
static

Returns the component-wise minimum of two vectors.

Parameters
aFirst vector.
bSecond vector.
Returns
The vector with the minimum components.

◆ MoveTowards()

template<typename T = float>
Vec2 Maths::Vec2< T >::MoveTowards ( const Vec2< T > & current,
const Vec2< T > & target,
T maxDistanceDelta )
static

Moves the current vector towards the target vector by a maximum distance delta.

Parameters
currentThe current vector.
targetThe target vector.
maxDistanceDeltaThe maximum distance to move.
Returns
The resulting vector after moving towards the target.

◆ NegativeInfinity()

template<typename T = float>
Vec2 Maths::Vec2< T >::NegativeInfinity ( )
static

Returns a vector with both components set to negative infinity.

Returns
The negative infinity vector.

◆ Normalize()

template<typename T = float>
void Maths::Vec2< T >::Normalize ( )

Normalizes the vector in place (makes it a unit vector).

◆ Normalized()

template<typename T = float>
Vec2 Maths::Vec2< T >::Normalized ( ) const

Returns a normalized (unit length) vector.

Returns
The normalized vector.

◆ One()

template<typename T = float>
Vec2 Maths::Vec2< T >::One ( )
static

Returns a vector with both components set to 1 (1, 1).

Returns
The one vector.

◆ operator!=()

template<typename T = float>
bool Maths::Vec2< T >::operator!= ( const Vec2< T > & other) const

Checks if this vector is not equal to another vector.

Parameters
otherThe vector to compare with.
Returns
True if not equal, otherwise false.

◆ operator*() [1/2]

template<typename T = float>
Vec2 Maths::Vec2< T >::operator* ( const Vec2< T > & other) const

Component-wise multiplication of two vectors.

Parameters
otherThe vector to multiply with.
Returns
The resulting vector.

◆ operator*() [2/2]

template<typename T = float>
Vec2 Maths::Vec2< T >::operator* ( T scalar) const

Multiplies this vector by a scalar.

Parameters
scalarThe scalar to multiply with.
Returns
The resulting vector.

◆ operator+()

template<typename T = float>
Vec2 Maths::Vec2< T >::operator+ ( const Vec2< T > & other) const

Adds another vector to this vector.

Parameters
otherThe vector to add.
Returns
The resulting vector.

◆ operator-()

template<typename T = float>
Vec2 Maths::Vec2< T >::operator- ( const Vec2< T > & other) const

Subtracts another vector from this vector.

Parameters
otherThe vector to subtract.
Returns
The resulting vector.

◆ operator/() [1/2]

template<typename T = float>
Vec2 Maths::Vec2< T >::operator/ ( const Vec2< T > & other) const

Component-wise division of two vectors.

Parameters
otherThe vector to divide by.
Returns
The resulting vector.

◆ operator/() [2/2]

template<typename T = float>
Vec2 Maths::Vec2< T >::operator/ ( T scalar) const

Divides this vector by a scalar.

Parameters
scalarThe scalar to divide by.
Returns
The resulting vector.

◆ operator==()

template<typename T = float>
bool Maths::Vec2< T >::operator== ( const Vec2< T > & other) const

Checks if this vector is equal to another vector.

Parameters
otherThe vector to compare with.
Returns
True if equal, otherwise false.

◆ Perpendicular()

template<typename T = float>
Vec2 Maths::Vec2< T >::Perpendicular ( const Vec2< T > & inDirection)
static

Returns a vector perpendicular to the input vector.

Parameters
inDirectionThe original vector.
Returns
The perpendicular vector.

◆ PositiveInfinity()

template<typename T = float>
Vec2 Maths::Vec2< T >::PositiveInfinity ( )
static

Returns a vector with both components set to positive infinity.

Returns
The positive infinity vector.

◆ Right()

template<typename T = float>
Vec2 Maths::Vec2< T >::Right ( )
static

Returns a vector pointing right (1, 0).

Returns
The right vector.

◆ Scale()

template<typename T = float>
Vec2 Maths::Vec2< T >::Scale ( const Vec2< T > & a,
const Vec2< T > & b )
static

Component-wise multiplication of two vectors.

Parameters
aFirst vector.
bSecond vector.
Returns
The resulting vector after component-wise multiplication.

◆ Set()

template<typename T = float>
void Maths::Vec2< T >::Set ( T newX,
T newY )
inline

Sets both components of the vector.

Parameters
newXThe new x value.
newYThe new y value.

◆ SetX()

template<typename T = float>
void Maths::Vec2< T >::SetX ( T newX)
inline

Sets the x-component to a new value.

Parameters
newXThe new x value.

◆ SetY()

template<typename T = float>
void Maths::Vec2< T >::SetY ( T newY)
inline

Sets the y-component to a new value.

Parameters
newYThe new y value.

◆ SignedAngle()

template<typename T = float>
T Maths::Vec2< T >::SignedAngle ( const Vec2< T > & from,
const Vec2< T > & to )
static

Calculates the signed angle (in radians) between two vectors.

Parameters
fromThe first vector.
toThe second vector.
Returns
The signed angle between the vectors.

◆ SqrtMagnitude()

template<typename T = float>
T Maths::Vec2< T >::SqrtMagnitude ( ) const

Returns the squared magnitude of the vector.

Returns
The squared magnitude.

◆ This()

template<typename T = float>
T Maths::Vec2< T >::This ( int i) const

Returns the component at index i (0 -> x, 1 -> y).

Parameters
iThe index (0 or 1).
Returns
The component at index i.

◆ ToString()

template<typename T = float>
std::string Maths::Vec2< T >::ToString ( ) const

Converts the vector to a string representation.

Returns
A string representing the vector.

◆ ToVec2()

template<typename T = float>
Vec2< T > Maths::Vec2< T >::ToVec2 ( ) const

Returns the vector as itself (no conversion).

Returns
The same vector.

◆ ToVec3()

template<typename T = float>
Vec3< T > Maths::Vec2< T >::ToVec3 ( ) const

Converts this 2D vector to a 3D vector (z-component = 0).

Returns
The corresponding 3D vector.

◆ Up()

template<typename T = float>
Vec2 Maths::Vec2< T >::Up ( )
static

Returns a vector pointing up (0, 1).

Returns
The up vector.

◆ x()

template<typename T = float>
T Maths::Vec2< T >::x ( ) const
inline

Returns the x-component.

Returns
The x-component.

◆ y()

template<typename T = float>
T Maths::Vec2< T >::y ( ) const
inline

Returns the y-component.

Returns
The y-component.

◆ Zero()

template<typename T = float>
Vec2 Maths::Vec2< T >::Zero ( )
static

Returns a zero vector (0, 0).

Returns
The zero vector.

Member Data Documentation

◆ m_x

template<typename T = float>
T Maths::Vec2< T >::m_x

X component of the vector.

◆ m_y

template<typename T = float>
T Maths::Vec2< T >::m_y

Y component of the vector.


The documentation for this class was generated from the following file: