template <typename T = f32>
rll::vector2d struct

A two-dimensional vector tagged with a unit.

Template parameters
T Number type. Must satisfy concept rll::concepts::num. Default is f32.

Public types

using angle_type = angle<number_type>
Associated angle type.
using number_type = T
Underlying number type.
using point2d_type = point2d<number_type>
Associated point2d type.
using size2d_type = size2d<number_type>
Associated size2d type.

Public static functions

template <std::size_t N2, typename = std::enable_if_t<N2 == 2>>
static auto from_array(std::array<number_type, N2> const& other) →  vector2d constexpr
Constructs new vector2d from std::array.
static auto from_pair(std::pair<number_type, number_type> const& other) →  vector2d constexpr
Constructs new vector2d from std::pair.
template <typename... Args, typename = std::enable_if_t<std::tuple_size_v<std::tuple<Args...>> == 2>>
static auto from_tuple(std::tuple<Args...> const& other) →  vector2d constexpr
Constructs new vector2d from std::tuple.
static auto one() →  vector2d constexpr
Constructs new vector with all values set to one.
static auto splat(number_type value) →  vector2d constexpr
Constructs new vector2d, setting all components to the same value.
static auto zero() →  vector2d constexpr
The same as origin.

Constructors, destructors, conversion operators

operator bool() const explicit constexpr
Returns true if underlying values is both not 0 and not NaN or Infinity in case of floating point.
vector2d() constexpr
Constructs new vector with zero coordinates.
vector2d(number_type x, number_type y) constexpr
Constructs new vector from given scalar values.
vector2d(number_type value) explicit constexpr
Constructs new vector, setting all components to the same value.
vector2d(size2d_type const& other) explicit constexpr
Constructs new vector from size2d.
vector2d(point2d<T> const& other) explicit constexpr
Constructs new vector from point2d.

Public functions

auto abs() const →  vector2d constexpr
Returns the absolute value of each component.
auto angle_to(vector2d const& other) const →  angle_type constexpr
Returns the signed angle between this vector and another vector.
auto angle_to_x_axis() const →  angle_type constexpr
Returns the signed angle between this vector and the x axis.
template <typename U, typename = std::enable_if_t<is_num_v<T>>>
auto cast() const →  vector2d<U> constexpr
Cast from one numeric representation to another, preserving the units.
auto ceil() const →  vector2d constexpr
Rounds each component to the smallest integer equal or greater than the original value.
auto clamp(vector2d const& start, vector2d const& end) const →  vector2d constexpr
Returns the vector2d each component of which clamped by corresponding components of start and end.
auto cross(vector2d const& other) const →  number_type constexpr
Returns the norm of the cross product of this and another vector2d.
auto cross(T const& other) const →  number_type constexpr
Returns the norm of the cross product of this and another vector2d.
auto dot(vector2d const& other) const →  number_type constexpr
Returns dot product of this and another vector2d.
auto floor() const →  vector2d constexpr
Rounds each component to the largest integer equal or less than the original value.
auto inverted() const →  vector2d constexpr
Returns inverted vector2d.
auto is_finite() const →  bool constexpr
Returns true if all members are finite.
auto is_positive() const →  bool constexpr
Returns true if all members are positive.
auto length_scalar() const →  number_type constexpr
Returns this vector3d's length as scalar value.
auto length_squared() const →  number_type constexpr
Returns this vector3d's length squared.
auto lerp(vector2d const& other, f32 t) const →  vector2d constexpr
Linearly interpolate between this vector2d and another vector2d.
template <typename F, typename = std::enable_if_t<std::is_invocable_v<F, number_type>>>
auto map(F&& fn) > > -> auto constexpr
Applies the function fn to each component of the vector2d.
auto max(vector2d const& other) const →  vector2d constexpr
Returns the largest x and y values of this and another vector2d.
auto min(vector2d const& other) const →  vector2d constexpr
Returns the smallest x and y values of this and another vector2d.
auto normalized() const →  vector2d constexpr
Returns the vector with length normalized.
auto operator!() const →  bool constexpr
Returns false if underlying values is both not 0.
auto operator!=(vector2d const& other) const →  bool constexpr
Returns true if the vector is not equal to the other vector.
auto operator*(number_type const& other) const →  vector2d constexpr
Multiplies vector by a scalar.
auto operator*=(number_type const& other) →  vector2d& constexpr
Multiplication assignment operator.
auto operator+() const →  vector2d constexpr
Returns an unchanged copy of this vector.
template <typename Q>
auto operator+(Q const& other) const →  vector2d constexpr
Adds vector and either point2d, size2d or vector2d.
template <typename Q>
auto operator+=(Q const& other) →  vector2d& constexpr
Adds vector and either point2d, size2d or vector2d.
auto operator-() const →  vector2d constexpr
Returns a negated copy of this vector.
template <typename Q>
auto operator-(Q const& other) const →  vector2d constexpr
Subtracts vector and either point2d, size2d or vector2d.
template <typename Q>
auto operator-=(Q const& other) →  vector2d& constexpr
Subtracts vector and either point2d, size2d or vector2d.
auto operator/(number_type const& other) const →  vector2d constexpr
Divides vector by a scalar.
auto operator/=(number_type const& other) →  vector2d& constexpr
Division assignment operator.
auto operator==(vector2d const& other) const →  bool constexpr
Returns true if the vector is equal to the other vector.
auto project(vector2d const& other) const →  vector2d constexpr
Returns this vector projected onto another vector.
auto reflected(vector2d const& normal) const →  vector2d constexpr
Returns a reflection vector using an incident ray and a surface normal.
auto round() const →  vector2d constexpr
Rounds each component to the nearest integer value.
auto to_array() const →  std::array<number_type, 2> constexpr
Casts into an array of two values.
auto to_f32() const →  vector2d constexpr
Casts into f32 vector2d.
auto to_f64() const →  vector2d<f64> constexpr
Casts into f64 vector2d.
auto to_i32() const →  vector2d<i32> constexpr
Casts into i32 vector2d.
auto to_i64() const →  vector2d<i64> constexpr
Casts into i64 vector2d.
auto to_isize() const →  vector2d<isize> constexpr
Casts into isize vector2d.
auto to_pair() const →  std::pair<number_type, number_type> constexpr
Casts into a pair of two values.
auto to_point2d() const →  point2d_type constexpr
Casts this vector to a point2d.
auto to_size2d() const →  size2d_type constexpr
Casts this vector to a size2d.
auto to_string() const →  std::string
Returns string representation of the vector2d.
auto to_tuple() const →  std::tuple<number_type, number_type> constexpr
Casts into a tuple of two values.
auto to_u32() const →  vector2d<u32> constexpr
Casts into u32 vector2d.
auto to_u64() const →  vector2d<u64> constexpr
Casts into u64 vector2d.
auto to_usize() const →  vector2d<usize> constexpr
Casts into usize vector2d.
auto x() const →  number_type constexpr
Returns the x-coordinate of the vector2d as scalar value.
auto x_mut() →  number_type& constexpr
Returns mutable reference to the x-coordinate of the vector2d as scalar value.
auto y() const →  number_type constexpr
Returns the y-coordinate of the vector2d as scalar value.
auto y_mut() →  number_type& constexpr
Returns mutable reference to the y-coordinate of the vector2d as scalar value.
template <typename F, typename = std::enable_if_t<std::is_invocable_v<F, number_type, number_type>>>
auto zip(vector2d const& other, F&& fn) const →  auto constexpr
Applies the function fn to each component of this vector2d and the other vector2d.

Function documentation

template <typename T>
template <std::size_t N2, typename = std::enable_if_t<N2 == 2>>
static vector2d rll::vector2d::from_array(std::array<number_type, N2> const& other) constexpr

Constructs new vector2d from std::array.

Template parameters
N2 The size of the std::array. Must be equal to 2.
Parameters
other The other std::array.

template <typename T>
static vector2d rll::vector2d::from_pair(std::pair<number_type, number_type> const& other) constexpr

Constructs new vector2d from std::pair.

Parameters
other The other std::pair.

template <typename T>
template <typename... Args, typename = std::enable_if_t<std::tuple_size_v<std::tuple<Args...>> == 2>>
static vector2d rll::vector2d::from_tuple(std::tuple<Args...> const& other) constexpr

Constructs new vector2d from std::tuple.

Parameters
other The other std::tuple.

template <typename T>
static vector2d rll::vector2d::splat(number_type value) constexpr

Constructs new vector2d, setting all components to the same value.

Parameters
value The value to set all components to.

template <typename T>
rll::vector2d::operator bool() const explicit constexpr

Returns true if underlying values is both not 0 and not NaN or Infinity in case of floating point.

template <typename T>
rll::vector2d::vector2d(number_type x, number_type y) constexpr

Constructs new vector from given scalar values.

Parameters
x The x-coordinate.
y The y-coordinate.

template <typename T>
rll::vector2d::vector2d(number_type value) explicit constexpr

Constructs new vector, setting all components to the same value.

Parameters
value The value to set all components to.

template <typename T>
rll::vector2d::vector2d(size2d_type const& other) explicit constexpr

Constructs new vector from size2d.

Parameters
other The other size2d.

template <typename T>
rll::vector2d::vector2d(point2d<T> const& other) explicit constexpr

Constructs new vector from point2d.

Parameters
other The other point2d.

template <typename T>
vector2d rll::vector2d::abs() const constexpr

Returns the absolute value of each component.

Returns The absolute value of each component.

template <typename T>
angle_type rll::vector2d::angle_to(vector2d const& other) const constexpr

Returns the signed angle between this vector and another vector.

Parameters
other The other vector.
Returns The signed angle between this vector and the other vector.

template <typename T>
angle_type rll::vector2d::angle_to_x_axis() const constexpr

Returns the signed angle between this vector and the x axis.

Returns The signed angle between this vector and the x axis.

Positive values counted counterclockwise, where 0 is +x axis, and PI/2 is +y axis.

template <typename T>
template <typename U, typename = std::enable_if_t<is_num_v<T>>>
vector2d<U> rll::vector2d::cast() const constexpr

Cast from one numeric representation to another, preserving the units.

Template parameters
U New number type.
Returns The vector2d with the new number type and the same value.

template <typename T>
vector2d rll::vector2d::ceil() const constexpr

Rounds each component to the smallest integer equal or greater than the original value.

This behavior is preserved for negative values (unlike the basic cast).

template <typename T>
vector2d rll::vector2d::clamp(vector2d const& start, vector2d const& end) const constexpr

Returns the vector2d each component of which clamped by corresponding components of start and end.

Parameters
start The start vector2d.
end The end vector2d.
Returns The clamped vector2d.

template <typename T>
number_type rll::vector2d::cross(vector2d const& other) const constexpr

Returns the norm of the cross product of this and another vector2d.

Parameters
other The other vector2d.
Returns The norm of the cross product of this and another vector2d.

Cross product is defined as [x1, y1] x [x2, y2] = x1 * y2 - y1 * x2.

template <typename T>
number_type rll::vector2d::cross(T const& other) const constexpr

Returns the norm of the cross product of this and another vector2d.

Parameters
other The other vector2d.
Returns The norm of the cross product of this and another vector2d.

Cross product is defined as [x1, y1] x [x2, y2] = x1 * y2 - y1 * x2.

template <typename T>
number_type rll::vector2d::dot(vector2d const& other) const constexpr

Returns dot product of this and another vector2d.

Parameters
other The other vector2d.
Returns The dot product of this and another vector2d.

template <typename T>
vector2d rll::vector2d::floor() const constexpr

Rounds each component to the largest integer equal or less than the original value.

This behavior is preserved for negative values (unlike the basic cast).

template <typename T>
vector2d rll::vector2d::inverted() const constexpr

Returns inverted vector2d.

Returns Inverted vector2d.

template <typename T>
number_type rll::vector2d::length_scalar() const constexpr

Returns this vector3d's length as scalar value.

Returns The length of this vector3d.

template <typename T>
number_type rll::vector2d::length_squared() const constexpr

Returns this vector3d's length squared.

Returns The length squared of this vector3d.

template <typename T>
vector2d rll::vector2d::lerp(vector2d const& other, f32 t) const constexpr

Linearly interpolate between this vector2d and another vector2d.

Parameters
other The other vector2d.
t Interpolation factor.
Returns The interpolated vector2d.

Example:

using rll::vector2d;
auto const from = vector2d(0.0F, 10.0F);
auto const to = vector2d(8.0F, -4.0F);
fmt::println("{}", from.lerp(to, -1.0F));
fmt::println("{}", from.lerp(to, 0.0F));
fmt::println("{}", from.lerp(to, 0.5F));
fmt::println("{}", from.lerp(to, 1.0F));
fmt::println("{}", from.lerp(to, 2.0F));

Output:

[-8.0, 24.0]
[0.0, 10.0]
[4.0, 3.0]
[8.0, -4.0]
[16.0, -18.0]

template <typename T>
template <typename F, typename = std::enable_if_t<std::is_invocable_v<F, number_type>>>
auto rll::vector2d::map(F&& fn) > > constexpr

Applies the function fn to each component of the vector2d.

Template parameters
F The type of function to apply.
Parameters
fn The function to apply.

template <typename T>
vector2d rll::vector2d::max(vector2d const& other) const constexpr

Returns the largest x and y values of this and another vector2d.

Parameters
other The other vector2d.
Returns vector2d with the largest x and y values.

template <typename T>
vector2d rll::vector2d::min(vector2d const& other) const constexpr

Returns the smallest x and y values of this and another vector2d.

Parameters
other The other vector2d.
Returns vector2d with the smallest x and y values.

template <typename T>
vector2d rll::vector2d::normalized() const constexpr

Returns the vector with length normalized.

Returns The normalized vector.

template <typename T>
bool rll::vector2d::operator!=(vector2d const& other) const constexpr

Returns true if the vector is not equal to the other vector.

Parameters
other The other vector.
Returns true if the vector is not equal to the other vector.

template <typename T>
vector2d rll::vector2d::operator*(number_type const& other) const constexpr

Multiplies vector by a scalar.

Parameters
other The multiplier.
Returns The result of multiplication.

template <typename T>
vector2d& rll::vector2d::operator*=(number_type const& other) constexpr

Multiplication assignment operator.

Parameters
other Scalar value.
Returns Reference to this vector.

template <typename T>
vector2d rll::vector2d::operator+() const constexpr

Returns an unchanged copy of this vector.

Returns An unchanged copy of this vector.

template <typename T>
template <typename Q>
vector2d rll::vector2d::operator+(Q const& other) const constexpr

Adds vector and either point2d, size2d or vector2d.

Template parameters
Q The type of the summand. Can be point2d, vector2d or size2d.
Parameters
other The summand.
Returns The sum.

template <typename T>
template <typename Q>
vector2d& rll::vector2d::operator+=(Q const& other) constexpr

Adds vector and either point2d, size2d or vector2d.

Template parameters
Q The type of the summand. Can be point2d, vector2d or size2d.
Parameters
other The summand.
Returns The sum.

template <typename T>
vector2d rll::vector2d::operator-() const constexpr

Returns a negated copy of this vector.

Returns A negated copy of this vector.

template <typename T>
template <typename Q>
vector2d rll::vector2d::operator-(Q const& other) const constexpr

Subtracts vector and either point2d, size2d or vector2d.

Template parameters
Q The type of the subtrahend. Can be point2d, vector2d or size2d.
Parameters
other The subtrahend.
Returns The difference.

template <typename T>
template <typename Q>
vector2d& rll::vector2d::operator-=(Q const& other) constexpr

Subtracts vector and either point2d, size2d or vector2d.

Template parameters
Q The type of the subtrahend. Can be point2d, vector2d or size2d.
Parameters
other The subtrahend.
Returns The difference.

template <typename T>
vector2d rll::vector2d::operator/(number_type const& other) const constexpr

Divides vector by a scalar.

Parameters
other The divisor.
Returns The result of division.

template <typename T>
vector2d& rll::vector2d::operator/=(number_type const& other) constexpr

Division assignment operator.

Parameters
other Scalar value.
Returns Reference to this vector.

template <typename T>
bool rll::vector2d::operator==(vector2d const& other) const constexpr

Returns true if the vector is equal to the other vector.

Parameters
other The other vector.
Returns true if the vector is equal to the other vector.

template <typename T>
vector2d rll::vector2d::project(vector2d const& other) const constexpr

Returns this vector projected onto another vector.

Parameters
other The other vector.
Returns The projection of this vector onto the other vector.

template <typename T>
vector2d rll::vector2d::reflected(vector2d const& normal) const constexpr

Returns a reflection vector using an incident ray and a surface normal.

Parameters
normal The surface normal.
Returns The reflection vector.

template <typename T>
vector2d rll::vector2d::round() const constexpr

Rounds each component to the nearest integer value.

This behavior is preserved for negative values (unlike the basic cast).

template <typename T>
point2d_type rll::vector2d::to_point2d() const constexpr

Casts this vector to a point2d.

Returns The point2d representation of this vector.

template <typename T>
size2d_type rll::vector2d::to_size2d() const constexpr

Casts this vector to a size2d.

Returns The size2d representation of this vector.

template <typename T>
std::string rll::vector2d::to_string() const

Returns string representation of the vector2d.

Returns String representation of the object.

Size is represented as it's numeric values. If the underlying number type is floating vector2d, it is rounded to two decimal places.

template <typename T>
number_type rll::vector2d::x() const constexpr

Returns the x-coordinate of the vector2d as scalar value.

Returns The x-coordinate of the vector2d.

template <typename T>
number_type& rll::vector2d::x_mut() constexpr

Returns mutable reference to the x-coordinate of the vector2d as scalar value.

Returns Mutable reference to the x-coordinate of the vector2d.

template <typename T>
number_type rll::vector2d::y() const constexpr

Returns the y-coordinate of the vector2d as scalar value.

Returns The y-coordinate of the vector2d.

template <typename T>
number_type& rll::vector2d::y_mut() constexpr

Returns mutable reference to the y-coordinate of the vector2d as scalar value.

Returns Mutable reference to the y-coordinate of the vector2d.

template <typename T>
template <typename F, typename = std::enable_if_t<std::is_invocable_v<F, number_type, number_type>>>
auto rll::vector2d::zip(vector2d const& other, F&& fn) const constexpr

Applies the function fn to each component of this vector2d and the other vector2d.

Template parameters
F The type of function to apply.
Parameters
other The other vector2d to apply.
fn The function to apply.