Skip to main content

vec2 Struct Template

Represents a 2-dimensional vector of the generic type <T>. More...

Declaration

template <helios::math::Numeric T> struct helios::math::vec2<T> { ... }

Public Constructors Index

template <helios::math::Numeric T>
constexprvec2 () noexcept

Creates a new vec2 with its values initialized to (0, 0). More...

template <helios::math::Numeric T>
constexprvec2 (const T x, const T y) noexcept

Constructs a new vec2 with the specified x, y components. More...

Public Operators Index

template <helios::math::Numeric T>
constexpr const T &operator[] (const size_t i) const noexcept

Provides read only access to the vector components. Bounds checking is performed via `assert` in debug builds. More...

template <helios::math::Numeric T>
constexpr T &operator[] (const size_t i) noexcept

Provides read-write access to the vector components. Bounds checking is performed via `assert` in debug builds. More...

template <helios::math::Numeric T>
constexpr booloperator== (const vec2< T > &rgt) const

Strictly compares the elements of this vector with the elements of the rgt vector. More...

Public Member Functions Index

template <helios::math::Numeric T>
constexpr auto toVec3 () const -> helios::math::vec3< T >

Converts the current vec2<T> into a 3D vector representation. More...

template <helios::math::Numeric T>
auto length () const noexcept -> FloatingPointType< T >

Returns the magnitude of this vec2<T>. More...

template <helios::math::Numeric T>
constexpr boolisNormalized () const noexcept

Checks if this vector is normalized (unit length). More...

template <helios::math::Numeric T>
auto normalize () const noexcept -> vec2< FloatingPointType< T > >

Normalizes this vec2<T>. More...

template <helios::math::Numeric T>
constexpr boolsame (const vec2< T > &rgt, T epsilon=0.0001) const

Compares this vector's elements with the rgt vector considering an epsilon value. Returns true if for all elements the equation |a-b| <= epsilon holds. More...

Private Member Attributes Index

template <helios::math::Numeric T>
Tv[2]

Internal array storing the vector components. More...

Description

Represents a 2-dimensional vector of the generic type <T>.

The `vec2` struct provides a lightweight and efficient way to handle 2D vector mathematics for the numeric types float, int, double. For convenient access, the shorthands `vec2f`, `vec2d` and `vec2i` are available.

Template Parameters
T

The numeric type of the vector components.

Definition at line 33 of file vec2.ixx.

Public Constructors

vec2()

template <helios::math::Numeric T>
helios::math::vec2< T >::vec2 ()
inline constexpr noexcept

Creates a new vec2 with its values initialized to (0, 0).

Definition at line 46 of file vec2.ixx.

46 constexpr vec2() noexcept : v{static_cast<T>(0), static_cast<T>(0)} {}

vec2()

template <helios::math::Numeric T>
helios::math::vec2< T >::vec2 (const T x, const T y)
inline constexpr noexcept

Constructs a new vec2 with the specified x, y components.

Parameters
x

The value for the x component.

y

The value for the y component.

Definition at line 55 of file vec2.ixx.

55 constexpr vec2(const T x, const T y) noexcept : v{x, y} {}

Public Operators

operator[]()

template <helios::math::Numeric T>
const T & helios::math::vec2< T >::operator[] (const size_t i)
inline constexpr noexcept

Provides read only access to the vector components. Bounds checking is performed via `assert` in debug builds.

Parameters
i

The index to query

Returns

A const ref to the requested component.

Definition at line 74 of file vec2.ixx.

74 constexpr const T& operator[](const size_t i) const noexcept {
75 assert(i <= 1 && "vec2 - Index out of bounds.");
76 return this->v[i];
77 }

operator[]()

template <helios::math::Numeric T>
T & helios::math::vec2< T >::operator[] (const size_t i)
inline constexpr noexcept

Provides read-write access to the vector components. Bounds checking is performed via `assert` in debug builds.

Parameters
i

The index to query

Returns

A ref to the requested component.

Definition at line 87 of file vec2.ixx.

87 constexpr T& operator[](const size_t i) noexcept {
88 assert(i <= 1 && "vec2 - Index out of bounds.");
89 return this->v[i];
90 }

operator==()

template <helios::math::Numeric T>
bool helios::math::vec2< T >::operator== (const vec2< T > & rgt)
inline constexpr

Strictly compares the elements of this vector with the elements of the rgt vector.

Parameters
rgt

The right vector to compare for equal values

Returns

True if all elements are equal (==), false otherwise.

Definition at line 147 of file vec2.ixx.

147 constexpr bool operator==(const vec2<T>& rgt) const {
148 return v[0] == rgt[0] && v[1] == rgt[1];
149 }

Public Member Functions

isNormalized()

template <helios::math::Numeric T>
bool helios::math::vec2< T >::isNormalized ()
inline constexpr noexcept

Checks if this vector is normalized (unit length).

A vector is considered normalized if its squared length equals 1 within the tolerance defined by EPSILON_LENGTH.

Returns

true if the vector is approximately unit length, false otherwise.

Definition at line 113 of file vec2.ixx.

113 constexpr bool isNormalized() const noexcept {
114 const auto lenSquared =
115 static_cast<FloatingPointType<T>>(v[0]) * static_cast<FloatingPointType<T>>(v[0]) +
116 static_cast<FloatingPointType<T>>(v[1]) * static_cast<FloatingPointType<T>>(v[1]);
117
118 return std::abs(lenSquared - static_cast<FloatingPointType<T>>(1.0)) <= helios::math::EPSILON_LENGTH;
119 }

length()

template <helios::math::Numeric T>
FloatingPointType< T > helios::math::vec2< T >::length ()
inline noexcept

Returns the magnitude of this vec2<T>.

Returns

The magnitude of this vector as type FloatingPointType<T>.

Definition at line 97 of file vec2.ixx.

97 inline FloatingPointType<T> length() const noexcept {
98 if (v[0] == 0 && v[1] == 0) {
99 return static_cast<FloatingPointType<T>>(0);
100 }
101 return std::hypot(v[0], v[1]);
102 }

Referenced by helios::math::vec2< float >::normalize.

normalize()

template <helios::math::Numeric T>
vec2< FloatingPointType< T > > helios::math::vec2< T >::normalize ()
inline noexcept

Normalizes this vec2<T>.

Returns

The normalized FloatingPointType<T> vector.

Definition at line 126 of file vec2.ixx.

126 inline vec2<FloatingPointType<T>> normalize() const noexcept {
127 if (v[0] == 0 && v[1] == 0) {
129 static_cast<FloatingPointType<T>>(0),
130 static_cast<FloatingPointType<T>>(0)
131 );
132 }
134 static_cast<FloatingPointType<T>>(v[0]) / length(),
135 static_cast<FloatingPointType<T>>(v[1]) / length()
136 );
137 }

same()

template <helios::math::Numeric T>
bool helios::math::vec2< T >::same (const vec2< T > & rgt, T epsilon=0.0001)
inline constexpr

Compares this vector's elements with the rgt vector considering an epsilon value. Returns true if for all elements the equation |a-b| <= epsilon holds.

Parameters
rgt

The other vector to compare with this vector for equality.

epsilon

The epsilon value to use for comparison. If omitted, the default epsilon (0.0001) is used.

Returns

True if the elements of the vectors are considered equal, otherwise false.

See Also

https://realtimecollisiondetection.net/blog/?p=89

Todo

account for abs (values close to zero) and rel (larger values), move epsilon to global constant?

Definition at line 168 of file vec2.ixx.

168 constexpr bool same(const vec2<T>& rgt, T epsilon = 0.0001) const {
169 return std::fabs(v[0] - rgt[0]) <= epsilon &&
170 std::fabs(v[1] - rgt[1]) <= epsilon;
171 }

toVec3()

template <helios::math::Numeric T>
helios::math::vec3< T > helios::math::vec2< T >::toVec3 ()
inline constexpr

Converts the current vec2<T> into a 3D vector representation.

Returns

A 3D vector corresponding to the converted representation of the object or input.

Definition at line 62 of file vec2.ixx.

62 constexpr helios::math::vec3<T> toVec3() const {
63 return helios::math::vec3<T>{v[0], v[1], static_cast<T>(0)};
64 }

Private Member Attributes

v

template <helios::math::Numeric T>
T helios::math::vec2< T >::v[2]

Internal array storing the vector components.

Definition at line 39 of file vec2.ixx.

39 T v[2];

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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.