Infinity Engine v0.6.20
C++ API Documentation
Loading...
Searching...
No Matches
Infinity::Types::Math::t_Vector2< T > Struct Template Reference

Template structure representing a 2-component vector. More...

#include <Vector2.hpp>

Public Member Functions

constexpr t_Vector2 ()
 Default constructor.
 
constexpr t_Vector2 (const t_Vector2 &v)
 Copy constructor.
 
constexpr t_Vector2operator= (const t_Vector2 &)=default
 Copy assignment operator.
 
constexpr t_Vector2 (T x, T y)
 Constructs a vector from component values.
 
template<typename R >
constexpr t_Vector2 (const t_Vector2< R > &v)
 Explicit type-converting constructor.
 
Toperator[] (size_t i)
 Array subscript operator (mutable).
 
T operator[] (size_t i) const
 Array subscript operator (const).
 
void set (T in_x, T in_y)
 Sets both components simultaneously.
 
Tdata ()
 Gets a mutable pointer to the underlying data.
 
const Tdata () const
 Gets a const pointer to the underlying data.
 
template<typename R >
t_Vector2operator= (const t_Vector2< R > &rhs)
 Type-converting assignment operator.
 
t_Vector2operator+= (const t_Vector2 &rhs)
 Component-wise addition assignment.
 
t_Vector2operator-= (const t_Vector2 &rhs)
 Component-wise subtraction assignment.
 
t_Vector2operator*= (T rhs)
 Scalar multiplication assignment.
 
t_Vector2operator*= (const t_Vector2 &rhs)
 Component-wise multiplication assignment.
 
t_Vector2operator/= (T rhs)
 Scalar division assignment.
 
 operator bool () const noexcept
 Boolean conversion operator.
 
t_Vector2< Tnormalized () const
 Computes the normalized version of this vector.
 
t_Vector2< Tcross (const t_Vector2< T > &other) const
 Computes the 2D cross product with another vector.
 
float length () const
 Computes the length (magnitude) of this vector.
 
float magnitude () const
 Computes the magnitude of this vector.
 
float squaredMagnitude () const
 Computes the squared magnitude of this vector.
 
float distance (const t_Vector2< T > &other) const
 Computes the Euclidean distance to another vector.
 
float dot (const t_Vector2< T > &other) const
 Computes the dot product with another vector.
 
float angle (const t_Vector2< T > &other) const
 Computes the angle between this vector and another.
 
t_Vector2< Tabs () const
 Computes the component-wise absolute value.
 

Public Attributes

union { 
 
   T   value [2] 
 Array access to components. More...
 
   struct { 
 
      T   x 
 
      T   y 
 Cartesian coordinate interpretation. More...
 
   }  
 
   struct { 
 
      T   r 
 
      T   g 
 Color component interpretation. More...
 
   }  
 
   struct { 
 
      T   s 
 
      T   t 
 Texture coordinate interpretation. More...
 
   }  
 
};  
 Union providing multiple interpretations of the same data.
 

Detailed Description

template<typename T>
struct Infinity::Types::Math::t_Vector2< T >

Template structure representing a 2-component vector.

t_Vector2 provides a versatile 2D vector implementation with support for multiple interpretations of the same underlying data through union members:

  • Cartesian coordinates (x, y)
  • Color components (r, g)
  • Texture coordinates (s, t)
  • Array indexing (value[0], value[1])

This flexibility makes it suitable for various use cases in procedural generation:

  • 2D positions, directions, and offsets
  • Texture/UV coordinates
  • 2D velocity and acceleration vectors
  • 2-channel color data (e.g., RG channels)
  • 2D grid indices
  • Screen/pixel coordinates

The template parameter allows instantiation with different numeric types (float, double, int, etc.) to match precision and performance requirements. Common specializations are provided via type aliases (Vector2, Vector2i, etc.).

Vector operations include:

  • Arithmetic operations (+, -, *, /)
  • Vector math (dot product, cross product, normalization)
  • Magnitude and distance calculations
  • Angle computations
  • Component-wise operations

Example usage:

// Create vectors
Vector2 position(10.0f, 20.0f);
Vector2 velocity(-5.0f, 3.0f);
// Arithmetic operations
Vector2 scaled = velocity * 2.0f;
// Vector operations
float dist = position.distance(newPosition);
float dotProd = position.dot(velocity);
// Access via different names
float x = position.x;
float u = position.s; // Same as x, different interpretation
float red = position.r; // Same as x, color interpretation
// Array access
for (size_t i = 0; i < 2; ++i) {
position[i] *= 2.0f;
}
Template structure representing a 2-component vector.
Definition Vector2.hpp:75
t_Vector2< T > normalized() const
Computes the normalized version of this vector.
Definition Vector2.hpp:326
float distance(const t_Vector2< T > &other) const
Computes the Euclidean distance to another vector.
Definition Vector2.hpp:408
T x
Definition Vector2.hpp:90
Template Parameters
TThe numeric type for vector components (float, double, int, etc.).
See also
Vector2, Vector2f, Vector2d, Vector2i

Constructor & Destructor Documentation

◆ t_Vector2() [1/4]

template<typename T >
constexpr Infinity::Types::Math::t_Vector2< T >::t_Vector2 ( )
inlineconstexpr

Default constructor.

Initializes all components to zero (or default value of T).

◆ t_Vector2() [2/4]

template<typename T >
constexpr Infinity::Types::Math::t_Vector2< T >::t_Vector2 ( const t_Vector2< T > &  v)
inlineconstexpr

Copy constructor.

Parameters
vVector to copy from.

◆ t_Vector2() [3/4]

template<typename T >
constexpr Infinity::Types::Math::t_Vector2< T >::t_Vector2 ( T  x,
T  y 
)
inlineconstexpr

Constructs a vector from component values.

Parameters
xThe x component (or r, or s).
yThe y component (or g, or t).
Vector2 pos(10.0f, 20.0f);
Vector2i pixel(100, 200);

◆ t_Vector2() [4/4]

template<typename T >
template<typename R >
constexpr Infinity::Types::Math::t_Vector2< T >::t_Vector2 ( const t_Vector2< R > &  v)
inlineexplicitconstexpr

Explicit type-converting constructor.

Constructs a vector from another vector with a different component type, performing static_cast on each component.

Template Parameters
RThe source component type.
Parameters
vVector to convert from.
Vector2i intVec(10, 20);
Vector2 floatVec(intVec); // Converts int to float

Member Function Documentation

◆ abs()

template<typename T >
t_Vector2< T > Infinity::Types::Math::t_Vector2< T >::abs ( ) const
inline

Computes the component-wise absolute value.

Returns a vector where each component is the absolute value of the corresponding component in this vector.

Returns
Vector with absolute values of components.
Note
For unsigned types, returns a copy of the vector unchanged.
Vector2 v(-3.0f, -4.0f);
Vector2 absV = v.abs(); // Returns (3.0f, 4.0f)
t_Vector2< T > abs() const
Computes the component-wise absolute value.
Definition Vector2.hpp:473

◆ angle()

template<typename T >
float Infinity::Types::Math::t_Vector2< T >::angle ( const t_Vector2< T > &  other) const
inline

Computes the angle between this vector and another.

Returns the angle in radians between the two vectors.

Parameters
otherThe other vector.
Returns
Angle in radians (0 to π).
Note
Returns 0 if either vector has zero length.
Vector2 a(1, 0);
Vector2 b(0, 1);
float angle = a.angle(b); // Returns π/2 (90 degrees)
float angle(const t_Vector2< T > &other) const
Computes the angle between this vector and another.
Definition Vector2.hpp:450

◆ cross()

template<typename T >
t_Vector2< T > Infinity::Types::Math::t_Vector2< T >::cross ( const t_Vector2< T > &  other) const
inline

Computes the 2D cross product with another vector.

In 2D, the cross product returns a vector perpendicular to both inputs. The magnitude equals the area of the parallelogram formed by the vectors.

Parameters
otherThe other vector.
Returns
Cross product result.
Note
This returns a 2D vector, not a scalar like the 3D cross product z-component.

◆ data() [1/2]

template<typename T >
T * Infinity::Types::Math::t_Vector2< T >::data ( )
inline

Gets a mutable pointer to the underlying data.

Returns
Pointer to the first component.
Vector2 v(1, 2);
float* ptr = v.data();
ptr[0] = 5.0f; // Modifies x

◆ data() [2/2]

template<typename T >
const T * Infinity::Types::Math::t_Vector2< T >::data ( ) const
inline

Gets a const pointer to the underlying data.

Returns
Const pointer to the first component.

◆ distance()

template<typename T >
float Infinity::Types::Math::t_Vector2< T >::distance ( const t_Vector2< T > &  other) const
inline

Computes the Euclidean distance to another vector.

Parameters
otherThe other vector.
Returns
The distance between this vector and other.
Vector2 a(0, 0);
Vector2 b(3, 4);
float dist = a.distance(b); // Returns 5.0f

◆ dot()

template<typename T >
float Infinity::Types::Math::t_Vector2< T >::dot ( const t_Vector2< T > &  other) const
inline

Computes the dot product with another vector.

The dot product measures how much two vectors point in the same direction. Returns positive if vectors point in similar directions, negative if opposite, and zero if perpendicular.

Parameters
otherThe other vector.
Returns
The dot product (x₁·x₂ + y₁·y₂).
Vector2 a(1, 0);
Vector2 b(0, 1);
float dot = a.dot(b); // Returns 0 (perpendicular)
float dot(const t_Vector2< T > &other) const
Computes the dot product with another vector.
Definition Vector2.hpp:429

◆ length()

template<typename T >
float Infinity::Types::Math::t_Vector2< T >::length ( ) const
inline

Computes the length (magnitude) of this vector.

Returns
The Euclidean length of the vector.
Vector2 v(3.0f, 4.0f);
float len = v.length(); // Returns 5.0f

◆ magnitude()

template<typename T >
float Infinity::Types::Math::t_Vector2< T >::magnitude ( ) const
inline

Computes the magnitude of this vector.

Alias for length().

Returns
The Euclidean magnitude of the vector.

◆ normalized()

template<typename T >
t_Vector2< T > Infinity::Types::Math::t_Vector2< T >::normalized ( ) const
inline

Computes the normalized version of this vector.

Returns a unit vector pointing in the same direction. If the vector is zero-length, returns a zero vector.

Returns
Normalized vector (length 1) or zero vector if original is zero.
Vector2 v(3.0f, 4.0f); // Length is 5
Vector2 unit = v.normalized(); // (0.6, 0.8)

◆ operator bool()

template<typename T >
Infinity::Types::Math::t_Vector2< T >::operator bool ( ) const
inlineexplicitnoexcept

Boolean conversion operator.

Returns true if the vector is non-zero (at least one component is non-zero).

Returns
true if vector is non-zero, false if zero vector.
Vector2 v(0, 0);
if (v) {
// This won't execute
}

◆ operator*=() [1/2]

Component-wise multiplication assignment.

Parameters
rhsVector to multiply by component-wise.
Returns
Reference to this vector.

◆ operator*=() [2/2]

Scalar multiplication assignment.

Parameters
rhsScalar to multiply by.
Returns
Reference to this vector.

◆ operator+=()

Component-wise addition assignment.

Parameters
rhsVector to add.
Returns
Reference to this vector.

◆ operator-=()

Component-wise subtraction assignment.

Parameters
rhsVector to subtract.
Returns
Reference to this vector.

◆ operator/=()

Scalar division assignment.

For floating-point types, multiplies by reciprocal for better performance. For integer types, performs standard division.

Parameters
rhsScalar to divide by.
Returns
Reference to this vector.
Warning
Division by zero is undefined behavior.

◆ operator=() [1/2]

Copy assignment operator.

◆ operator=() [2/2]

template<typename T >
template<typename R >
t_Vector2 & Infinity::Types::Math::t_Vector2< T >::operator= ( const t_Vector2< R > &  rhs)
inline

Type-converting assignment operator.

Assigns from a vector with a different component type, performing static_cast on each component.

Template Parameters
RThe source component type.
Parameters
rhsVector to assign from.
Returns
Reference to this vector.

◆ operator[]() [1/2]

Array subscript operator (mutable).

Parameters
iComponent index (0 or 1).
Returns
Reference to the component at index i.

◆ operator[]() [2/2]

template<typename T >
T Infinity::Types::Math::t_Vector2< T >::operator[] ( size_t  i) const
inline

Array subscript operator (const).

Parameters
iComponent index (0 or 1).
Returns
Value of the component at index i.

◆ set()

template<typename T >
void Infinity::Types::Math::t_Vector2< T >::set ( T  in_x,
T  in_y 
)
inline

Sets both components simultaneously.

Parameters
in_xNew x component value.
in_yNew y component value.

◆ squaredMagnitude()

template<typename T >
float Infinity::Types::Math::t_Vector2< T >::squaredMagnitude ( ) const
inline

Computes the squared magnitude of this vector.

More efficient than length() when you only need to compare lengths or don't need the actual distance value, as it avoids the sqrt operation.

Returns
The squared length (x² + y²).
// Efficient distance comparison without sqrt
if (v1.squaredMagnitude() < v2.squaredMagnitude()) {
// v1 is shorter than v2
}

Member Data Documentation

◆ [union]

Union providing multiple interpretations of the same data.

Allows accessing the two components as:

  • value[2]: Array access
  • x, y: Cartesian coordinates
  • r, g: Color components (red, green)
  • s, t: Texture coordinates (UV mapping)

◆ g

Color component interpretation.

◆ r

◆ s

◆ t

Texture coordinate interpretation.

◆ value

template<typename T >
T Infinity::Types::Math::t_Vector2< T >::value[2]

Array access to components.

◆ x

template<typename T >
T Infinity::Types::Math::t_Vector2< T >::x

◆ y

Cartesian coordinate interpretation.