Sparkle 0.0.1
Loading...
Searching...
No Matches
spk::IVector2< TType > Struct Template Reference

2D value type holding two components with basic arithmetic helpers. More...

#include <spk_vector2.hpp>

Collaboration diagram for spk::IVector2< TType >:
Collaboration graph

Public Types

using value_type = TType
 Component type stored by the vector.

Public Member Functions

constexpr IVector2 ()=default
 Builds a zero vector.
template<typename TX, typename TY>
requires (std::is_constructible_v<TType, TX> && std::is_constructible_v<TType, TY>)
constexpr IVector2 (const TX &p_x, const TY &p_y)
 Builds a vector from two components.
template<typename TOther>
requires std::is_constructible_v<TType, TOther>
constexpr IVector2 (const IVector2< TOther > &p_other)
 Converting constructor from another component type.
template<typename TOther>
requires std::is_constructible_v<TType, TOther>
constexpr IVector2 (const TOther &p_value)
 Fills both components with the same value.
std::string toString () const
 Converts the vector to a string.
std::wstring toWstring () const
 Converts the vector to a wide string.
bool operator== (const IVector2 &p_other) const
 Checks component-wise equality using SafeComparand.
bool operator!= (const IVector2 &p_other) const
 Negation of equality.
IVector2 operator+ (const IVector2 &p_other) const
 Component-wise addition.
IVector2 operator- (const IVector2 &p_other) const
 Component-wise subtraction.
IVector2 operator* (const IVector2 &p_other) const
 Component-wise multiplication.
IVector2 operator/ (const IVector2 &p_other) const
 Component-wise division.
IVector2 operator+ (const TType &p_scalar) const
 Adds a scalar to each component.
IVector2 operator- (const TType &p_scalar) const
 Subtracts a scalar from each component.
IVector2 operator* (const TType &p_scalar) const
 Multiplies each component by a scalar.
IVector2 operator/ (const TType &p_scalar) const
 Divides each component by a scalar.
IVector2 inverse () const
 Return the inverse vector.
IVector2 operator- () const
 Unary minus.
IVector2operator+= (const IVector2 &p_other)
 In-place component-wise addition.
IVector2operator-= (const IVector2 &p_other)
 In-place component-wise subtraction.
IVector2operator*= (const IVector2 &p_other)
 In-place component-wise multiplication.
IVector2operator/= (const IVector2 &p_other)
 In-place component-wise division.
IVector2operator+= (const TType &p_scalar)
 In-place scalar addition.
IVector2operator-= (const TType &p_scalar)
 In-place scalar subtraction.
IVector2operator*= (const TType &p_scalar)
 In-place scalar multiplication.
IVector2operator/= (const TType &p_scalar)
 In-place scalar division.
bool isZero () const
 Checks if both components are zero.
float squaredLength () const
 Computes squared vector length.
IVector2< float > normalize () const
 Return the vector once normalized by its length.
float length () const
 Computes Euclidean length.
float squaredDistance (const IVector2 &p_other) const
 Computes squared distance to another vector.
float distance (const IVector2 &p_other) const
 Computes Euclidean distance to another vector.
float dot (const IVector2 &p_other) const
 Computes dot product with another vector.
float cross (const IVector2 &p_other) const
 Computes 2D cross product (x1*y2 - y1*x2).
IVector2 perpendicular () const
 Returns a perpendicular vector (rotated 90 degrees).
IVector2 clamped (const IVector2 &p_boundMinimum, const IVector2 &p_boundMaximum) const
 Returns a clamped copy of this vector.
template<typename TOther>
requires std::is_constructible_v<TOther, TType>
constexpr operator IVector2< TOther > () const
 Explicitly converts to another IVector2 component type.

Static Public Member Functions

static IVector2 lerp (const IVector2 &p_from, const IVector2 &p_to, const float &p_alpha)
 Linearly interpolates between two vectors.
static IVector2 min (const IVector2 &p_a, const IVector2 &p_b)
 Component-wise minimum of two vectors.
template<typename... TOthers>
requires (sizeof...(TOthers) > 0 && (std::is_same_v<TOthers, IVector2> && ...))
static IVector2 min (const IVector2 &p_a, const IVector2 &p_b, const TOthers &...p_rest)
 Component-wise minimum of multiple vectors.
static IVector2 min (std::initializer_list< IVector2 > p_values)
 Component-wise minimum across an initializer list.
static IVector2 max (const IVector2 &p_a, const IVector2 &p_b)
 Component-wise maximum of two vectors.
template<typename... TOthers>
requires (sizeof...(TOthers) > 0 && (std::is_same_v<TOthers, IVector2> && ...))
static IVector2 max (const IVector2 &p_a, const IVector2 &p_b, const TOthers &...p_rest)
 Component-wise maximum of multiple vectors.
static IVector2 max (std::initializer_list< IVector2 > p_values)
 Component-wise maximum across an initializer list.
static IVector2 clamp (const IVector2 &p_value, const IVector2 &p_boundMinimum, const IVector2 &p_boundMaximum)
 Clamps each component of a vector between two bounds.
static bool isBetween (const IVector2 &p_value, const IVector2 &p_valueA, const IVector2 &p_valueB)
 Checks whether a vector lies within inclusive bounds.

Public Attributes

TType x {}
 X component.
TType y {}
 Y component.

Static Public Attributes

static const IVector2 Zero
 Predefined zero vector.
static const IVector2 One
 Predefined unit vector (1, 1).

Friends

std::ostream & operator<< (std::ostream &p_stream, const IVector2 &p_value)
 Streams the vector to an output stream.
std::wostream & operator<< (std::wostream &p_stream, const IVector2 &p_value)
 Streams the vector to a wide output stream.
IVector2 operator+ (const TType &p_scalar, const IVector2 &p_vector)
 Adds a scalar to a vector (scalar on the left side).
IVector2 operator- (const TType &p_scalar, const IVector2 &p_vector)
 Subtracts vector components from a scalar (scalar on the left side).
IVector2 operator* (const TType &p_scalar, const IVector2 &p_vector)
 Multiplies a scalar by a vector (scalar on the left side).
IVector2 operator/ (const TType &p_scalar, const IVector2 &p_vector)
 Divides a scalar by each vector component (scalar on the left side).

Detailed Description

template<typename TType>
struct spk::IVector2< TType >

2D value type holding two components with basic arithmetic helpers.

Template Parameters
TTypearithmetic component type; must be constructible from input values and support +, -, * and / and self assignation operator, in addition to comparisons.
See also
Vector2, Vector2Int, Vector2UInt
spk::Vector2 position{2.0f, 4.0f};
spk::Vector2 velocity{1.0f, -0.5f};
const auto next = position + velocity;
if (spk::Vector2::isBetween(next, {0.f, 0.f}, {10.f, 10.f}))
{
// Inside bounds
}
static bool isBetween(const IVector2 &p_value, const IVector2 &p_valueA, const IVector2 &p_valueB)
Definition spk_vector2.hpp:726

Constructor & Destructor Documentation

◆ IVector2() [1/3]

template<typename TType>
template<typename TX, typename TY>
requires (std::is_constructible_v<TType, TX> && std::is_constructible_v<TType, TY>)
spk::IVector2< TType >::IVector2 ( const TX & p_x,
const TY & p_y )
inlineconstexpr

Builds a vector from two components.

Parameters
p_xX value.
p_yY value.

◆ IVector2() [2/3]

template<typename TType>
template<typename TOther>
requires std::is_constructible_v<TType, TOther>
spk::IVector2< TType >::IVector2 ( const IVector2< TOther > & p_other)
inlineexplicitconstexpr

Converting constructor from another component type.

Template Parameters
TOthersource component type; must be constructible into TType.
Parameters
p_otherSource vector to copy and cast from.

◆ IVector2() [3/3]

template<typename TType>
template<typename TOther>
requires std::is_constructible_v<TType, TOther>
spk::IVector2< TType >::IVector2 ( const TOther & p_value)
inlineexplicitconstexpr

Fills both components with the same value.

Parameters
p_valueComponent value to duplicate.

Member Function Documentation

◆ clamp()

template<typename TType>
IVector2 spk::IVector2< TType >::clamp ( const IVector2< TType > & p_value,
const IVector2< TType > & p_boundMinimum,
const IVector2< TType > & p_boundMaximum )
inlinestatic

Clamps each component of a vector between two bounds.

Parameters
p_valueValue to clamp.
p_boundMinimumMinimal value to clamp onto.
p_boundMaximumMaximam value to clamp onto.
Returns
Vector with each component clamped between the provided bounds.
See also
clamped, min, max
const auto value = spk::Vector2{5.f, -2.f};
const auto bounded = spk::Vector2::clamp(value, {-1.f, 0.f}, {2.f, 1.f}); // (2, 0)
static IVector2 clamp(const IVector2 &p_value, const IVector2 &p_boundMinimum, const IVector2 &p_boundMaximum)
Definition spk_vector2.hpp:691
Here is the caller graph for this function:

◆ clamped()

template<typename TType>
IVector2 spk::IVector2< TType >::clamped ( const IVector2< TType > & p_boundMinimum,
const IVector2< TType > & p_boundMaximum ) const
inline

Returns a clamped copy of this vector.

Parameters
p_boundMinimumMinimal value to clamp onto.
p_boundMaximumMaximam value to clamp onto.
Returns
Clamped vector.
See also
clamp

◆ cross()

template<typename TType>
float spk::IVector2< TType >::cross ( const IVector2< TType > & p_other) const
inline

Computes 2D cross product (x1*y2 - y1*x2).

Parameters
p_otherTarget vector.
Returns
Scalar cross product (z-component magnitude).
See also
dot

◆ distance()

template<typename TType>
float spk::IVector2< TType >::distance ( const IVector2< TType > & p_other) const
inline

Computes Euclidean distance to another vector.

Parameters
p_otherTarget vector.
Returns
Distance between the two points.
See also
squaredDistance

◆ dot()

template<typename TType>
float spk::IVector2< TType >::dot ( const IVector2< TType > & p_other) const
inline

Computes dot product with another vector.

Parameters
p_otherTarget vector.
Returns
Scalar dot product.
See also
cross

◆ inverse()

template<typename TType>
IVector2 spk::IVector2< TType >::inverse ( ) const
inline

Return the inverse vector.

Returns
Vector with all components negated.
Here is the caller graph for this function:

◆ isBetween()

template<typename TType>
bool spk::IVector2< TType >::isBetween ( const IVector2< TType > & p_value,
const IVector2< TType > & p_valueA,
const IVector2< TType > & p_valueB )
inlinestatic

Checks whether a vector lies within inclusive bounds.

Parameters
p_valueValue to test.
p_valueALower bound of the check.
p_valueBHigher bound of the check.
Returns
true when both components are between the corresponding bounds.
Note
The behavior of this function is undefined if p_valueA isn't stricly lower than p_valueB on one of its attributes
See also
clamp

◆ isZero()

template<typename TType>
bool spk::IVector2< TType >::isZero ( ) const
inline

Checks if both components are zero.

Returns
true when x and y are zero.
Here is the caller graph for this function:

◆ length()

template<typename TType>
float spk::IVector2< TType >::length ( ) const
inline

Computes Euclidean length.

Returns
Magnitude of the vector.
See also
squaredLength
Here is the caller graph for this function:

◆ lerp()

template<typename TType>
IVector2 spk::IVector2< TType >::lerp ( const IVector2< TType > & p_from,
const IVector2< TType > & p_to,
const float & p_alpha )
inlinestatic

Linearly interpolates between two vectors.

Parameters
p_fromStart vector.
p_toEnd vector.
p_alphaInterpolation factor (0 -> start, 1 -> end); not clamped.
Returns
Interpolated vector.
See also
clamp, min, max
const auto start = spk::Vector2{0.f, 0.f};
const auto end = spk::Vector2{10.f, 20.f};
auto mid = spk::Vector2::lerp(start, end, 0.5f); // (5, 10)
static IVector2 lerp(const IVector2 &p_from, const IVector2 &p_to, const float &p_alpha)
Definition spk_vector2.hpp:566

◆ max() [1/3]

template<typename TType>
IVector2 spk::IVector2< TType >::max ( const IVector2< TType > & p_a,
const IVector2< TType > & p_b )
inlinestatic

Component-wise maximum of two vectors.

Parameters
p_aFirst vector.
p_bSecond vector.
Returns
Vector with maximum for each component.
See also
min
Here is the caller graph for this function:

◆ max() [2/3]

template<typename TType>
template<typename... TOthers>
requires (sizeof...(TOthers) > 0 && (std::is_same_v<TOthers, IVector2> && ...))
IVector2 spk::IVector2< TType >::max ( const IVector2< TType > & p_a,
const IVector2< TType > & p_b,
const TOthers &... p_rest )
inlinestatic

Component-wise maximum of multiple vectors.

Template Parameters
TOthersAdditional vector types (must be IVector2).
Parameters
p_aFirst vector.
p_bSecond vector.
p_restRemaining vectors.
Returns
Vector with maximum for each component.

◆ max() [3/3]

template<typename TType>
IVector2 spk::IVector2< TType >::max ( std::initializer_list< IVector2< TType > > p_values)
inlinestatic

Component-wise maximum across an initializer list.

Parameters
p_valuesCollection of vectors (must contain at least one element).
Returns
Vector with maximum for each component.
Exceptions
std::invalid_argumentwhen the list is empty.

◆ min() [1/3]

template<typename TType>
IVector2 spk::IVector2< TType >::min ( const IVector2< TType > & p_a,
const IVector2< TType > & p_b )
inlinestatic

Component-wise minimum of two vectors.

Parameters
p_aFirst vector.
p_bSecond vector.
Returns
Vector with minimum for each component.
See also
max
Here is the caller graph for this function:

◆ min() [2/3]

template<typename TType>
template<typename... TOthers>
requires (sizeof...(TOthers) > 0 && (std::is_same_v<TOthers, IVector2> && ...))
IVector2 spk::IVector2< TType >::min ( const IVector2< TType > & p_a,
const IVector2< TType > & p_b,
const TOthers &... p_rest )
inlinestatic

Component-wise minimum of multiple vectors.

Template Parameters
TOthersAdditional vector types (must be IVector2).
Parameters
p_aFirst vector.
p_bSecond vector.
p_restRemaining vectors.
Returns
Vector with minimum for each component.
See also
min(std::initializer_list<IVector2>)

◆ min() [3/3]

template<typename TType>
IVector2 spk::IVector2< TType >::min ( std::initializer_list< IVector2< TType > > p_values)
inlinestatic

Component-wise minimum across an initializer list.

Parameters
p_valuesCollection of vectors (must contain at least one element).
Returns
Vector with minimum for each component.
Exceptions
std::invalid_argumentwhen the list is empty.

◆ normalize()

template<typename TType>
IVector2< float > spk::IVector2< TType >::normalize ( ) const
inline

Return the vector once normalized by its length.

Returns
The normalized vector.
See also
squaredLength

◆ operator IVector2< TOther >()

template<typename TType>
template<typename TOther>
requires std::is_constructible_v<TOther, TType>
spk::IVector2< TType >::operator IVector2< TOther > ( ) const
inlineexplicitconstexpr

Explicitly converts to another IVector2 component type.

Template Parameters
TOtherTarget component type; must be constructible from TType.
Returns
Converted vector.

◆ operator!=()

template<typename TType>
bool spk::IVector2< TType >::operator!= ( const IVector2< TType > & p_other) const
inline

Negation of equality.

Parameters
p_otherVector to compare against.
Returns
true when any component differs.
See also
operator==

◆ operator*() [1/2]

template<typename TType>
IVector2 spk::IVector2< TType >::operator* ( const IVector2< TType > & p_other) const
inline

Component-wise multiplication.

Parameters
p_otherVector to multiply.
Returns
Product of both vectors.

◆ operator*() [2/2]

template<typename TType>
IVector2 spk::IVector2< TType >::operator* ( const TType & p_scalar) const
inline

Multiplies each component by a scalar.

Parameters
p_scalarScalar factor.
Returns
Scaled vector.

◆ operator*=() [1/2]

template<typename TType>
IVector2 & spk::IVector2< TType >::operator*= ( const IVector2< TType > & p_other)
inline

In-place component-wise multiplication.

Parameters
p_otherVector multiplier.
Returns
Reference to this vector after the operation.
See also
operator*

◆ operator*=() [2/2]

template<typename TType>
IVector2 & spk::IVector2< TType >::operator*= ( const TType & p_scalar)
inline

In-place scalar multiplication.

Parameters
p_scalarScalar factor.
Returns
Reference to this vector after the operation.
See also
operator*(const TType&) const

◆ operator+() [1/2]

template<typename TType>
IVector2 spk::IVector2< TType >::operator+ ( const IVector2< TType > & p_other) const
inline

Component-wise addition.

Parameters
p_otherVector to add.
Returns
Sum of both vectors.

◆ operator+() [2/2]

template<typename TType>
IVector2 spk::IVector2< TType >::operator+ ( const TType & p_scalar) const
inline

Adds a scalar to each component.

Parameters
p_scalarScalar value to add.
Returns
Offset vector.

◆ operator+=() [1/2]

template<typename TType>
IVector2 & spk::IVector2< TType >::operator+= ( const IVector2< TType > & p_other)
inline

In-place component-wise addition.

Parameters
p_otherVector to add.
Returns
Reference to this vector after the operation.
See also
operator+

◆ operator+=() [2/2]

template<typename TType>
IVector2 & spk::IVector2< TType >::operator+= ( const TType & p_scalar)
inline

In-place scalar addition.

Parameters
p_scalarScalar to add.
Returns
Reference to this vector after the operation.
See also
operator+(const TType&) const

◆ operator-() [1/3]

template<typename TType>
IVector2 spk::IVector2< TType >::operator- ( ) const
inline

Unary minus.

Returns
Vector with both components negated.

◆ operator-() [2/3]

template<typename TType>
IVector2 spk::IVector2< TType >::operator- ( const IVector2< TType > & p_other) const
inline

Component-wise subtraction.

Parameters
p_otherVector to subtract.
Returns
Difference of both vectors.

◆ operator-() [3/3]

template<typename TType>
IVector2 spk::IVector2< TType >::operator- ( const TType & p_scalar) const
inline

Subtracts a scalar from each component.

Parameters
p_scalarScalar value to subtract.
Returns
Offset vector.

◆ operator-=() [1/2]

template<typename TType>
IVector2 & spk::IVector2< TType >::operator-= ( const IVector2< TType > & p_other)
inline

In-place component-wise subtraction.

Parameters
p_otherVector to subtract.
Returns
Reference to this vector after the operation.
See also
operator-

◆ operator-=() [2/2]

template<typename TType>
IVector2 & spk::IVector2< TType >::operator-= ( const TType & p_scalar)
inline

In-place scalar subtraction.

Parameters
p_scalarScalar to subtract.
Returns
Reference to this vector after the operation.
See also
operator-(const TType&) const

◆ operator/() [1/2]

template<typename TType>
IVector2 spk::IVector2< TType >::operator/ ( const IVector2< TType > & p_other) const
inline

Component-wise division.

Parameters
p_otherVector to divide by.
Returns
Quotient of both vectors.
Exceptions
std::invalid_argumentwhen any divisor component is zero.

◆ operator/() [2/2]

template<typename TType>
IVector2 spk::IVector2< TType >::operator/ ( const TType & p_scalar) const
inline

Divides each component by a scalar.

Parameters
p_scalarScalar divisor.
Returns
Scaled vector.
Exceptions
std::invalid_argumentwhen divisor is zero.

◆ operator/=() [1/2]

template<typename TType>
IVector2 & spk::IVector2< TType >::operator/= ( const IVector2< TType > & p_other)
inline

In-place component-wise division.

Parameters
p_otherVector divisor.
Returns
Reference to this vector after the operation.
Exceptions
std::invalid_argumentwhen any divisor component is zero.
See also
operator/

◆ operator/=() [2/2]

template<typename TType>
IVector2 & spk::IVector2< TType >::operator/= ( const TType & p_scalar)
inline

In-place scalar division.

Parameters
p_scalarScalar divisor.
Returns
Reference to this vector after the operation.
Exceptions
std::invalid_argumentwhen divisor is zero.
See also
operator/(const TType&) const

◆ operator==()

template<typename TType>
bool spk::IVector2< TType >::operator== ( const IVector2< TType > & p_other) const
inline

Checks component-wise equality using SafeComparand.

Parameters
p_otherVector to compare against.
Returns
true if both components are equal.
See also
spk::SafeComparand

◆ perpendicular()

template<typename TType>
IVector2 spk::IVector2< TType >::perpendicular ( ) const
inline

Returns a perpendicular vector (rotated 90 degrees).

Returns
Vector (-y, x).

◆ squaredDistance()

template<typename TType>
float spk::IVector2< TType >::squaredDistance ( const IVector2< TType > & p_other) const
inline

Computes squared distance to another vector.

Parameters
p_otherTarget vector.
Returns
Squared distance.
See also
distance

◆ squaredLength()

template<typename TType>
float spk::IVector2< TType >::squaredLength ( ) const
inline

Computes squared vector length.

Returns
Sum of squared components.
See also
length
Here is the caller graph for this function:

◆ toString()

template<typename TType>
std::string spk::IVector2< TType >::toString ( ) const
inline

Converts the vector to a string.

Returns
Text form "(x, y)".

◆ toWstring()

template<typename TType>
std::wstring spk::IVector2< TType >::toWstring ( ) const
inline

Converts the vector to a wide string.

Returns
Text form "(x, y)" using wide characters.

◆ operator*

template<typename TType>
IVector2 operator* ( const TType & p_scalar,
const IVector2< TType > & p_vector )
friend

Multiplies a scalar by a vector (scalar on the left side).

Parameters
p_scalarScalar factor.
p_vectorVector value.
Returns
Scaled vector.
See also
operator*(const TType&) const

◆ operator+

template<typename TType>
IVector2 operator+ ( const TType & p_scalar,
const IVector2< TType > & p_vector )
friend

Adds a scalar to a vector (scalar on the left side).

Parameters
p_scalarScalar value.
p_vectorVector value.
Returns
Sum of scalar and vector.
See also
operator+(const TType&) const

◆ operator-

template<typename TType>
IVector2 operator- ( const TType & p_scalar,
const IVector2< TType > & p_vector )
friend

Subtracts vector components from a scalar (scalar on the left side).

Parameters
p_scalarScalar value.
p_vectorVector value.
Returns
Component-wise (scalar - vector).

◆ operator/

template<typename TType>
IVector2 operator/ ( const TType & p_scalar,
const IVector2< TType > & p_vector )
friend

Divides a scalar by each vector component (scalar on the left side).

Parameters
p_scalarScalar numerator.
p_vectorVector divisor.
Returns
Component-wise division.
Exceptions
std::invalid_argumentwhen any divisor component is zero.

◆ operator<< [1/2]

template<typename TType>
std::ostream & operator<< ( std::ostream & p_stream,
const IVector2< TType > & p_value )
friend

Streams the vector to an output stream.

Parameters
p_streamDestination stream.
p_valueVector to print.
Returns
Reference to the destination stream.

◆ operator<< [2/2]

template<typename TType>
std::wostream & operator<< ( std::wostream & p_stream,
const IVector2< TType > & p_value )
friend

Streams the vector to a wide output stream.

Parameters
p_streamDestination stream.
p_valueVector to print.
Returns
Reference to the destination stream.

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