Skip to main content

vec2 Struct Template

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

Declaration

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

Public Member Typedefs Index

template <helios::math::concepts::IsNumeric T>
usingNumeric_type = T

Public Constructors Index

template <helios::math::concepts::IsNumeric T>
constexprvec2 () noexcept

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

template <helios::math::concepts::IsNumeric 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::concepts::IsNumeric 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::concepts::IsNumeric 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::concepts::IsNumeric 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::concepts::IsNumeric T>
auto toVec3 () const -> constexpr helios::math::vec3< T >

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

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

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

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

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

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

Normalizes this vec2<T>. More...

template <helios::math::concepts::IsNumeric 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...

template <helios::math::concepts::IsNumeric T>
constexpr boolsame (const vec2< T > &rgt) const

Private Member Attributes Index

template <helios::math::concepts::IsNumeric 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 34 of file vec2.ixx.

Public Member Typedefs

Numeric_type

template <helios::math::concepts::IsNumeric T>
using helios::math::vec2< T >::Numeric_type = T

Definition at line 44 of file vec2.ixx.

44 using Numeric_type = T;

Public Constructors

vec2()

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

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

Definition at line 49 of file vec2.ixx.

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

vec2()

template <helios::math::concepts::IsNumeric T>
constexpr 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 58 of file vec2.ixx.

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

Public Operators

operator[]()

template <helios::math::concepts::IsNumeric T>
constexpr 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 77 of file vec2.ixx.

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

operator[]()

template <helios::math::concepts::IsNumeric T>
constexpr 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 90 of file vec2.ixx.

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

operator==()

template <helios::math::concepts::IsNumeric T>
constexpr 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 150 of file vec2.ixx.

150 constexpr bool operator==(const vec2<T>& rgt) const {
151 return v[0] == rgt[0] && v[1] == rgt[1];
152 }

Public Member Functions

isNormalized()

template <helios::math::concepts::IsNumeric T>
constexpr 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 116 of file vec2.ixx.

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

Reference helios::math::EPSILON_LENGTH.

length()

template <helios::math::concepts::IsNumeric 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 100 of file vec2.ixx.

101 if (v[0] == 0 && v[1] == 0) {
102 return static_cast<FloatingPointType<T>>(0);
103 }
104 return std::hypot(v[0], v[1]);
105 }

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

normalize()

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

Normalizes this vec2<T>.

Returns

The normalized FloatingPointType<T> vector.

Definition at line 129 of file vec2.ixx.

130 if (v[0] == 0 && v[1] == 0) {
132 static_cast<FloatingPointType<T>>(0),
133 static_cast<FloatingPointType<T>>(0)
134 );
135 }
137 static_cast<FloatingPointType<T>>(v[0]) / length(),
138 static_cast<FloatingPointType<T>>(v[1]) / length()
139 );
140 }

Reference helios::math::vec2< T >::length.

same()

template <helios::math::concepts::IsNumeric T>
constexpr 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

Definition at line 171 of file vec2.ixx.

171 constexpr bool same(const vec2<T>& rgt, T epsilon = 0.0001) const requires std::is_floating_point_v<T> {
172 if constexpr (std::is_integral_v<T>) {
173 return *this == rgt;
174 }
175
176 return std::fabs(v[0] - rgt[0]) <= epsilon &&
177 std::fabs(v[1] - rgt[1]) <= epsilon;
178 }

same()

template <helios::math::concepts::IsNumeric T>
constexpr bool helios::math::vec2< T >::same (const vec2< T > & rgt)
inline constexpr

Definition at line 180 of file vec2.ixx.

180 constexpr bool same(const vec2<T>& rgt) const requires std::is_integral_v<T> {
181 return *this == rgt;
182 }

toVec3()

template <helios::math::concepts::IsNumeric T>
constexpr 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 65 of file vec2.ixx.

65 constexpr helios::math::vec3<T> toVec3() const {
66 return helios::math::vec3<T>{v[0], v[1], static_cast<T>(0)};
67 }

Private Member Attributes

v

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

Internal array storing the vector components.

Definition at line 40 of file vec2.ixx.

40 T v[2];

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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.