MXVK Vulkan Framework 0.35.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
mxvk::vec2D Class Reference

Two-dimensional float vector with common arithmetic helpers. More...

#include <mxvk/include/mxvk/mxvk_math.h>

Public Member Functions

float Cos (const vec2D &v) const
 Compute the cosine of the angle between this vector and another vector.
float Cos (const vec2D &v) const
 Compute the cosine of the angle between this vector and another vector.
constexpr float DotProduct (const vec2D &v) const
 Compute the dot product with another vector.
float DotProduct (const vec2D &v) const
 Compute the dot product with another vector.
float Length () const
 Compute the Euclidean length of this vector.
float Length () const
 Compute the Euclidean length of this vector.
void Normalize ()
 Normalize this vector in place, or reset it to zero if it is too short.
void Normalize ()
 Normalize this vector in place, or reset it to zero if it is too short.
void Normalize (vec2D &v) const
 Write a normalized copy of this vector to v.
void Normalize (vec2D &v) const
 Write a normalized copy of this vector to v.
constexpr vec2D operator* (float k) const
 Scale this vector by a scalar.
vec2D operator* (float k) const
 Scale this vector by a scalar.
constexpr vec2D operator+ (const vec2D &v) const
 Add two vectors component-wise.
vec2D operator+ (const vec2D &v) const
 Add two vectors component-wise.
vec2D & operator+= (const vec2D &v)
 Add another vector to this vector.
vec2D & operator+= (const vec2D &v)
 Add another vector to this vector.
constexpr vec2D operator- (const vec2D &v) const
 Subtract two vectors component-wise.
vec2D operator- (const vec2D &v) const
 Subtract two vectors component-wise.
vec2D & operator-= (const vec2D &v)
 Subtract another vector from this vector.
vec2D & operator-= (const vec2D &v)
 Subtract another vector from this vector.
vec2D & operator= (const vec2D &)=default
vec2D & operator= (const vec2D &)=default
std::string Print (const std::string &name="v") const
 Format this vector as a named angle-bracket tuple.
std::string Print (const std::string &name="v") const
 Format this vector as a named angle-bracket tuple.
vec2D Scale (float k) const
 Return a scaled copy of this vector.
vec2D Scale (float k) const
 Return a scaled copy of this vector.
void ScaleThis (float k)
 Scale this vector in place.
void ScaleThis (float k)
 Scale this vector in place.
void Set (float x_value, float y_value)
 Set both vector coordinates.
void Set (float x_value, float y_value)
 Set both vector coordinates.
constexpr vec2D ()
 Construct the zero vector.
constexpr vec2D ()
 Construct the zero vector.
constexpr vec2D (float x_value, float y_value)
 Construct a vector from explicit coordinates.
constexpr vec2D (float x_value, float y_value)
 Construct a vector from explicit coordinates.

Public Attributes

float x = 0.0f
 X coordinate.
float y = 0.0f
 Y coordinate.

Detailed Description

Two-dimensional float vector with common arithmetic helpers.

Definition at line 158 of file mxvk_math.h.

Constructor & Destructor Documentation

◆ vec2D() [1/4]

mxvk::vec2D::vec2D ( )
inlineconstexpr

Construct the zero vector.

Definition at line 167 of file mxvk_math.h.

167: x(0.0f), y(0.0f) {}
float x
X coordinate.
Definition mxvk_math.h:161
float y
Y coordinate.
Definition mxvk_math.h:164

◆ vec2D() [2/4]

mxvk::vec2D::vec2D ( float x_value,
float y_value )
inlineconstexpr

Construct a vector from explicit coordinates.

Definition at line 170 of file mxvk_math.h.

170: x(x_value), y(y_value) {}

◆ vec2D() [3/4]

mxvk::vec2D::vec2D ( )
inlineconstexpr

Construct the zero vector.

Definition at line 183 of file mxvk_math_eigen.hpp.

183: x(0.0f), y(0.0f) {}

◆ vec2D() [4/4]

mxvk::vec2D::vec2D ( float x_value,
float y_value )
inlineconstexpr

Construct a vector from explicit coordinates.

Definition at line 186 of file mxvk_math_eigen.hpp.

186: x(x_value), y(y_value) {}

Member Function Documentation

◆ Cos() [1/2]

float mxvk::vec2D::Cos ( const vec2D & v) const
inlinenodiscard

Compute the cosine of the angle between this vector and another vector.

Definition at line 236 of file mxvk_math.h.

236 {
237 const float denom = Length() * v.Length();
238 return denom <= EPSILON ? 0.0f : std::clamp(DotProduct(v) / denom, -1.0f, 1.0f);
239 }
float Length() const
Compute the Euclidean length of this vector.
Definition mxvk_math.h:216
constexpr float DotProduct(const vec2D &v) const
Compute the dot product with another vector.
Definition mxvk_math.h:213
constexpr float EPSILON
Default tolerance used for floating-point singularity and zero-length checks.
Definition mxvk_math.h:37

◆ Cos() [2/2]

float mxvk::vec2D::Cos ( const vec2D & v) const
inlinenodiscard

Compute the cosine of the angle between this vector and another vector.

Definition at line 246 of file mxvk_math_eigen.hpp.

246 {
247 const float denom = Length() * v.Length();
248 return denom <= EPSILON ? 0.0f : std::clamp(DotProduct(v) / denom, -1.0f, 1.0f);
249 }

◆ DotProduct() [1/2]

float mxvk::vec2D::DotProduct ( const vec2D & v) const
inlinenodiscardconstexpr

Compute the dot product with another vector.

Definition at line 213 of file mxvk_math.h.

213{ return x * v.x + y * v.y; }

◆ DotProduct() [2/2]

float mxvk::vec2D::DotProduct ( const vec2D & v) const
inlinenodiscard

Compute the dot product with another vector.

Definition at line 224 of file mxvk_math_eigen.hpp.

224{ return ToEigen().dot(v.ToEigen()); }

◆ Length() [1/2]

float mxvk::vec2D::Length ( ) const
inlinenodiscard

Compute the Euclidean length of this vector.

Definition at line 216 of file mxvk_math.h.

216{ return std::sqrt(DotProduct(*this)); }

◆ Length() [2/2]

float mxvk::vec2D::Length ( ) const
inlinenodiscard

Compute the Euclidean length of this vector.

Definition at line 227 of file mxvk_math_eigen.hpp.

227{ return ToEigen().norm(); }

◆ Normalize() [1/4]

void mxvk::vec2D::Normalize ( )
inline

Normalize this vector in place, or reset it to zero if it is too short.

Definition at line 219 of file mxvk_math.h.

219 {
220 const float length = Length();
221 if (length <= EPSILON) {
222 x = 0.0f;
223 y = 0.0f;
224 return;
225 }
226 ScaleThis(1.0f / length);
227 }
void ScaleThis(float k)
Scale this vector in place.
Definition mxvk_math.h:207

◆ Normalize() [2/4]

void mxvk::vec2D::Normalize ( )
inline

Normalize this vector in place, or reset it to zero if it is too short.

Definition at line 230 of file mxvk_math_eigen.hpp.

230 {
231 const float length = Length();
232 if (length <= EPSILON) {
233 FromEigen(Eigen::Vector2f::Zero(), *this);
234 return;
235 }
236 FromEigen(ToEigen().normalized(), *this);
237 }

◆ Normalize() [3/4]

void mxvk::vec2D::Normalize ( vec2D & v) const
inline

Write a normalized copy of this vector to v.

Definition at line 230 of file mxvk_math.h.

230 {
231 v = *this;
232 v.Normalize();
233 }

◆ Normalize() [4/4]

void mxvk::vec2D::Normalize ( vec2D & v) const
inline

Write a normalized copy of this vector to v.

Definition at line 240 of file mxvk_math_eigen.hpp.

240 {
241 v = *this;
242 v.Normalize();
243 }

◆ operator*() [1/2]

vec2D mxvk::vec2D::operator* ( float k) const
inlinenodiscardconstexpr

Scale this vector by a scalar.

Definition at line 201 of file mxvk_math.h.

201{ return {x * k, y * k}; }

◆ operator*() [2/2]

vec2D mxvk::vec2D::operator* ( float k) const
inlinenodiscard

Scale this vector by a scalar.

Definition at line 215 of file mxvk_math_eigen.hpp.

215{ return FromEigen(ToEigen() * k); }

◆ operator+() [1/2]

vec2D mxvk::vec2D::operator+ ( const vec2D & v) const
inlinenodiscardconstexpr

Add two vectors component-wise.

Definition at line 181 of file mxvk_math.h.

181{ return {x + v.x, y + v.y}; }

◆ operator+() [2/2]

vec2D mxvk::vec2D::operator+ ( const vec2D & v) const
inlinenodiscard

Add two vectors component-wise.

Definition at line 197 of file mxvk_math_eigen.hpp.

197{ return FromEigen(ToEigen() + v.ToEigen()); }

◆ operator+=() [1/2]

vec2D & mxvk::vec2D::operator+= ( const vec2D & v)
inline

Add another vector to this vector.

Definition at line 184 of file mxvk_math.h.

184 {
185 x += v.x;
186 y += v.y;
187 return *this;
188 }

◆ operator+=() [2/2]

vec2D & mxvk::vec2D::operator+= ( const vec2D & v)
inline

Add another vector to this vector.

Definition at line 200 of file mxvk_math_eigen.hpp.

200 {
201 FromEigen(ToEigen() + v.ToEigen(), *this);
202 return *this;
203 }

◆ operator-() [1/2]

vec2D mxvk::vec2D::operator- ( const vec2D & v) const
inlinenodiscardconstexpr

Subtract two vectors component-wise.

Definition at line 191 of file mxvk_math.h.

191{ return {x - v.x, y - v.y}; }

◆ operator-() [2/2]

vec2D mxvk::vec2D::operator- ( const vec2D & v) const
inlinenodiscard

Subtract two vectors component-wise.

Definition at line 206 of file mxvk_math_eigen.hpp.

206{ return FromEigen(ToEigen() - v.ToEigen()); }

◆ operator-=() [1/2]

vec2D & mxvk::vec2D::operator-= ( const vec2D & v)
inline

Subtract another vector from this vector.

Definition at line 194 of file mxvk_math.h.

194 {
195 x -= v.x;
196 y -= v.y;
197 return *this;
198 }

◆ operator-=() [2/2]

vec2D & mxvk::vec2D::operator-= ( const vec2D & v)
inline

Subtract another vector from this vector.

Definition at line 209 of file mxvk_math_eigen.hpp.

209 {
210 FromEigen(ToEigen() - v.ToEigen(), *this);
211 return *this;
212 }

◆ operator=() [1/2]

vec2D & mxvk::vec2D::operator= ( const vec2D & )
default

◆ operator=() [2/2]

vec2D & mxvk::vec2D::operator= ( const vec2D & )
default

◆ Print() [1/2]

std::string mxvk::vec2D::Print ( const std::string & name = "v") const
inlinenodiscard

Format this vector as a named angle-bracket tuple.

Definition at line 242 of file mxvk_math.h.

242 {
243 std::ostringstream out;
244 out << name << '<' << x << ',' << y << '>';
245 return out.str();
246 }

◆ Print() [2/2]

std::string mxvk::vec2D::Print ( const std::string & name = "v") const
inlinenodiscard

Format this vector as a named angle-bracket tuple.

Definition at line 252 of file mxvk_math_eigen.hpp.

252 {
253 std::ostringstream out;
254 out << name << '<' << x << ',' << y << '>';
255 return out.str();
256 }

◆ Scale() [1/2]

vec2D mxvk::vec2D::Scale ( float k) const
inlinenodiscard

Return a scaled copy of this vector.

Definition at line 204 of file mxvk_math.h.

204{ return *this * k; }

◆ Scale() [2/2]

vec2D mxvk::vec2D::Scale ( float k) const
inlinenodiscard

Return a scaled copy of this vector.

Definition at line 218 of file mxvk_math_eigen.hpp.

218{ return *this * k; }

◆ ScaleThis() [1/2]

void mxvk::vec2D::ScaleThis ( float k)
inline

Scale this vector in place.

Definition at line 207 of file mxvk_math.h.

207 {
208 x *= k;
209 y *= k;
210 }

◆ ScaleThis() [2/2]

void mxvk::vec2D::ScaleThis ( float k)
inline

Scale this vector in place.

Definition at line 221 of file mxvk_math_eigen.hpp.

221{ FromEigen(ToEigen() * k, *this); }

◆ Set() [1/2]

void mxvk::vec2D::Set ( float x_value,
float y_value )
inline

Set both vector coordinates.

Definition at line 173 of file mxvk_math.h.

173 {
174 x = x_value;
175 y = y_value;
176 }

◆ Set() [2/2]

void mxvk::vec2D::Set ( float x_value,
float y_value )
inline

Set both vector coordinates.

Definition at line 189 of file mxvk_math_eigen.hpp.

189 {
190 x = x_value;
191 y = y_value;
192 }

Member Data Documentation

◆ x

float mxvk::vec2D::x = 0.0f

X coordinate.

Definition at line 161 of file mxvk_math.h.

◆ y

float mxvk::vec2D::y = 0.0f

Y coordinate.

Definition at line 164 of file mxvk_math.h.


The documentation for this class was generated from the following files: