#include <rll/euclid/vector2d.h>
template <typename T = f32>
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::static auto from_array(std::
size_t N2, typename = std:: enable_if_t<N2 == 2>> 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::static auto from_tuple(std::
enable_if_t<std:: tuple_size_v<std:: tuple<Args...>> == 2>> 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 not0
and notNaN
orInfinity
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::auto cast() const → vector2d<U> constexpr
enable_if_t<is_num_v<T>>> - 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::auto map(F&& fn) > > -> auto constexpr
enable_if_t<std:: is_invocable_v<F, number_ type>>> - 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 not0
. - 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::auto zip(vector2d const& other, F&& fn) const → auto constexpr
enable_if_t<std:: is_invocable_v<F, number_ type, number_ type>>> - Applies the function fn to each component of this vector2d and the other vector2d.
Function documentation
static vector2d rll::vector2d::from_array(std:: array<number_ type, N2> const& other) constexpr
Constructs new vector2d from std::
.
Template parameters | |
---|---|
N2 | The size of the std:: . Must be equal to 2 . |
Parameters | |
other | The other std:: . |
template <typename T>
static vector2d rll::vector2d::from_pair(std:: pair<number_ type, number_ type> const& other) constexpr
Constructs new vector2d from std::
.
Parameters | |
---|---|
other | The other std:: . |
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
template <typename... Args, typename = std::
Constructs new vector2d from std::
.
Parameters | |
---|---|
other | The other std:: . |
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>
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>
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::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
template <typename F, typename = std::
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::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>
template <typename Q>
vector2d rll::vector2d::operator-(Q const& other) const constexpr
template <typename Q>
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
template <typename Q>
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>
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
template <typename F, typename = std::
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. |