ToyGine2 26.2.0
Game Engine for retro consoles
Loading...
Searching...
No Matches
toy::math::Vector2< T > Class Template Reference

2D vector with floating-point or fixed-point components for positions, movement, and physics. More...

Collaboration diagram for toy::math::Vector2< T >:

Public Member Functions

constexpr Vector2 () noexcept
 Default constructor.
constexpr Vector2 (const T &x, const T &y) noexcept
 Constructs a vector with the given components.
constexpr Vector2 (const T *values) noexcept
 Constructs a vector from an array.
constexpr T * c_arr () noexcept
 Pointer to the component array (x, then y).
constexpr const T * c_arr () const noexcept
 Read-only pointer to the component array (x, then y).
constexpr Vector2operator+= (const Vector2 &vector) noexcept
 Adds vector to this vector.
constexpr Vector2operator-= (const Vector2 &vector) noexcept
 Subtracts vector from this vector.
constexpr Vector2operator*= (const T &scalar) noexcept
 Multiplies both components by scalar.
constexpr Vector2operator/= (const T &scalar) noexcept
 Divides both components by scalar.
constexpr T sqrMagnitude () const noexcept
 Squared Euclidean length (x² + y²).
constexpr void setZero () noexcept
 Sets x and y to zero.
constexpr bool isZero () const noexcept
 Returns whether both components are zero.
constexpr bool isEqual (const Vector2 &vector, T absEpsilon=8 *numeric_limits< T >::epsilon(), T relEpsilon=64 *numeric_limits< T >::epsilon()) const noexcept
 Equality within absolute and relative epsilon.

Public Attributes

x
 X component of the vector.
y
 Y component of the vector.

Detailed Description

template<Vector2Component T>
class toy::math::Vector2< T >

2D vector with floating-point or fixed-point components for positions, movement, and physics.

Represents a 2D vector with x and y components. Suited for world/screen coordinates, velocity, forces, and transformations where sub-pixel or fractional precision is needed. For integer pixel coordinates use toy::math::Point.

Key Features

  • Precision: Component type T is toy::math::Vector2Component (float, double, or fixed-point).
  • Constexpr: Most operations are constexpr.
  • Exception safety: All operations are noexcept.
  • Small size: Two components; stack-friendly.

Usage Example

#include "math.hpp"
toy::math::Vector2 pos(100.5f, 200.3f);
toy::math::Vector2 velocity(50.0f, -25.0f);
auto deltaTime = 0.5f;
pos += velocity * deltaTime;
toy::math::Vector2 force(10.0f, 0.0f);
auto mass = 2.0f;
auto acceleration = force / mass;
2D vector with floating-point or fixed-point components for positions, movement, and physics.
Definition vector2.hpp:96
Umbrella header for the math module.

Performance Characteristics

  • Construction, assignment, arithmetic, comparison: O(1).
  • Memory: 2 × sizeof(T).

Safety Guarantees

  • Contracts: Division by zero is asserted in debug.
  • Exception safety: noexcept; no exceptions thrown.
Note
For pixel-aligned UI coordinates use toy::math::Point.
See also
toy::math::Point

Constructor & Destructor Documentation

◆ Vector2() [1/3]

template<Vector2Component T>
toy::math::Vector2< T >::Vector2 ( )
constexprnoexcept

Default constructor.

◆ Vector2() [2/3]

template<Vector2Component T>
toy::math::Vector2< T >::Vector2 ( const T & x,
const T & y )
constexprnoexcept

Constructs a vector with the given components.

Parameters
xX component.
yY component.

◆ Vector2() [3/3]

template<Vector2Component T>
toy::math::Vector2< T >::Vector2 ( const T * values)
explicitconstexprnoexcept

Constructs a vector from an array.

Parameters
valuesPointer to at least two elements; values[0] is x, values[1] is y.
Precondition
values != nullptr; array has at least two elements.
Postcondition
x == values[0], y == values[1].

Member Function Documentation

◆ c_arr() [1/2]

template<Vector2Component T>
const T * toy::math::Vector2< T >::c_arr ( ) const
nodiscardconstexprnoexcept

Read-only pointer to the component array (x, then y).

Returns
Pointer to x; y follows contiguously.

◆ c_arr() [2/2]

template<Vector2Component T>
T * toy::math::Vector2< T >::c_arr ( )
nodiscardconstexprnoexcept

Pointer to the component array (x, then y).

Returns
Pointer to x; y follows contiguously.

◆ isEqual()

template<Vector2Component T>
bool toy::math::Vector2< T >::isEqual ( const Vector2< T > & vector,
T absEpsilon = 8 * numeric_limits<T>::epsilon(),
T relEpsilon = 64 * numeric_limits<T>::epsilon() ) const
nodiscardconstexprnoexcept

Equality within absolute and relative epsilon.

Parameters
vectorOther vector.
absEpsilonMaximum absolute difference per component (default: 8× numeric_limits<T>::epsilon()).
relEpsilonMaximum relative difference (default: 64× numeric_limits<T>::epsilon()).
Returns
true if both components are equal within tolerances, false otherwise.
Precondition
absEpsilon0 and relEpsilon0.

◆ isZero()

template<Vector2Component T>
bool toy::math::Vector2< T >::isZero ( ) const
nodiscardconstexprnoexcept

Returns whether both components are zero.

Returns
true if x and y are zero, false otherwise.

◆ operator*=()

template<Vector2Component T>
Vector2< T > & toy::math::Vector2< T >::operator*= ( const T & scalar)
constexprnoexcept

Multiplies both components by scalar.

Parameters
scalarScale factor.
Returns
Reference to *this.

◆ operator+=()

template<Vector2Component T>
Vector2< T > & toy::math::Vector2< T >::operator+= ( const Vector2< T > & vector)
constexprnoexcept

Adds vector to this vector.

Parameters
vectorVector to add.
Returns
Reference to *this.
Postcondition
Components are the sum of the previous values and vector.

◆ operator-=()

template<Vector2Component T>
Vector2< T > & toy::math::Vector2< T >::operator-= ( const Vector2< T > & vector)
constexprnoexcept

Subtracts vector from this vector.

Parameters
vectorVector to subtract.
Returns
Reference to *this.
Postcondition
Components are the difference of the previous values and vector.

◆ operator/=()

template<Vector2Component T>
Vector2< T > & toy::math::Vector2< T >::operator/= ( const T & scalar)
constexprnoexcept

Divides both components by scalar.

Parameters
scalarDivisor.
Returns
Reference to *this.
Precondition
scalar != 0.

◆ setZero()

template<Vector2Component T>
void toy::math::Vector2< T >::setZero ( )
constexprnoexcept

Sets x and y to zero.

Postcondition
Vector is at the origin.

◆ sqrMagnitude()

template<Vector2Component T>
T toy::math::Vector2< T >::sqrMagnitude ( ) const
nodiscardconstexprnoexcept

Squared Euclidean length (x² + y²).

Returns
Squared magnitude; avoids sqrt for comparisons.

Member Data Documentation

◆ x

template<Vector2Component T>
T toy::math::Vector2< T >::x

X component of the vector.

◆ y

template<Vector2Component T>
T toy::math::Vector2< T >::y

Y component of the vector.


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