MXVK Vulkan Framework 0.24.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.
vec2Doperator+= (const vec2D &v)
 Add another vector to this vector.
vec2Doperator+= (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.
vec2Doperator-= (const vec2D &v)
 Subtract another vector from this vector.
vec2Doperator-= (const vec2D &v)
 Subtract another vector from this vector.
vec2Doperator= (const vec2D &)=default
vec2Doperator= (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 177 of file mxvk_math.h.

Constructor & Destructor Documentation

◆ vec2D() [1/4]

mxvk::vec2D::vec2D ( )
inlineconstexpr

Construct the zero vector.

Definition at line 186 of file mxvk_math.h.

186: x(0.0f), y(0.0f) {}
float x
X coordinate.
Definition mxvk_math.h:180
float y
Y coordinate.
Definition mxvk_math.h:183

◆ vec2D() [2/4]

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

Construct a vector from explicit coordinates.

Definition at line 189 of file mxvk_math.h.

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

◆ vec2D() [3/4]

mxvk::vec2D::vec2D ( )
inlineconstexpr

Construct the zero vector.

Definition at line 202 of file mxvk_math_eigen.hpp.

202: 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 205 of file mxvk_math_eigen.hpp.

205: 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 267 of file mxvk_math.h.

267 {
268 const float denom = Length() * v.Length();
269 return denom <= EPSILON ? 0.0f : std::clamp(DotProduct(v) / denom, -1.0f, 1.0f);
270 }
float Length() const
Compute the Euclidean length of this vector.
Definition mxvk_math.h:245
constexpr float DotProduct(const vec2D &v) const
Compute the dot product with another vector.
Definition mxvk_math.h:240
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 279 of file mxvk_math_eigen.hpp.

279 {
280 const float denom = Length() * v.Length();
281 return denom <= EPSILON ? 0.0f : std::clamp(DotProduct(v) / denom, -1.0f, 1.0f);
282 }

◆ DotProduct() [1/2]

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

Compute the dot product with another vector.

Definition at line 240 of file mxvk_math.h.

240 {
241 return x * v.x + y * v.y;
242 }

◆ DotProduct() [2/2]

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

Compute the dot product with another vector.

Definition at line 253 of file mxvk_math_eigen.hpp.

253 {
254 return ToEigen().dot(v.ToEigen());
255 }

◆ Length() [1/2]

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

Compute the Euclidean length of this vector.

Definition at line 245 of file mxvk_math.h.

245 {
246 return std::sqrt(DotProduct(*this));
247 }

◆ Length() [2/2]

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

Compute the Euclidean length of this vector.

Definition at line 258 of file mxvk_math_eigen.hpp.

258 {
259 return ToEigen().norm();
260 }

◆ 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 250 of file mxvk_math.h.

250 {
251 const float length = Length();
252 if (length <= EPSILON) {
253 x = 0.0f;
254 y = 0.0f;
255 return;
256 }
257 ScaleThis(1.0f / length);
258 }
void ScaleThis(float k)
Scale this vector in place.
Definition mxvk_math.h:234

◆ 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 263 of file mxvk_math_eigen.hpp.

263 {
264 const float length = Length();
265 if (length <= EPSILON) {
266 FromEigen(Eigen::Vector2f::Zero(), *this);
267 return;
268 }
269 FromEigen(ToEigen().normalized(), *this);
270 }

◆ Normalize() [3/4]

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

Write a normalized copy of this vector to v.

Definition at line 261 of file mxvk_math.h.

261 {
262 v = *this;
263 v.Normalize();
264 }

◆ Normalize() [4/4]

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

Write a normalized copy of this vector to v.

Definition at line 273 of file mxvk_math_eigen.hpp.

273 {
274 v = *this;
275 v.Normalize();
276 }

◆ operator*() [1/2]

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

Scale this vector by a scalar.

Definition at line 224 of file mxvk_math.h.

224 {
225 return {x * k, y * k};
226 }

◆ operator*() [2/2]

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

Scale this vector by a scalar.

Definition at line 238 of file mxvk_math_eigen.hpp.

238 {
239 return FromEigen(ToEigen() * k);
240 }

◆ operator+() [1/2]

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

Add two vectors component-wise.

Definition at line 200 of file mxvk_math.h.

200 {
201 return {x + v.x, y + v.y};
202 }

◆ operator+() [2/2]

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

Add two vectors component-wise.

Definition at line 216 of file mxvk_math_eigen.hpp.

216 {
217 return FromEigen(ToEigen() + v.ToEigen());
218 }

◆ operator+=() [1/2]

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

Add another vector to this vector.

Definition at line 205 of file mxvk_math.h.

205 {
206 x += v.x;
207 y += v.y;
208 return *this;
209 }

◆ operator+=() [2/2]

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

Add another vector to this vector.

Definition at line 221 of file mxvk_math_eigen.hpp.

221 {
222 FromEigen(ToEigen() + v.ToEigen(), *this);
223 return *this;
224 }

◆ operator-() [1/2]

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

Subtract two vectors component-wise.

Definition at line 212 of file mxvk_math.h.

212 {
213 return {x - v.x, y - v.y};
214 }

◆ operator-() [2/2]

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

Subtract two vectors component-wise.

Definition at line 227 of file mxvk_math_eigen.hpp.

227 {
228 return FromEigen(ToEigen() - v.ToEigen());
229 }

◆ operator-=() [1/2]

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

Subtract another vector from this vector.

Definition at line 217 of file mxvk_math.h.

217 {
218 x -= v.x;
219 y -= v.y;
220 return *this;
221 }

◆ operator-=() [2/2]

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

Subtract another vector from this vector.

Definition at line 232 of file mxvk_math_eigen.hpp.

232 {
233 FromEigen(ToEigen() - v.ToEigen(), *this);
234 return *this;
235 }

◆ 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 273 of file mxvk_math.h.

273 {
274 std::ostringstream out;
275 out << name << '<' << x << ',' << y << '>';
276 return out.str();
277 }

◆ 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 285 of file mxvk_math_eigen.hpp.

285 {
286 std::ostringstream out;
287 out << name << '<' << x << ',' << y << '>';
288 return out.str();
289 }

◆ Scale() [1/2]

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

Return a scaled copy of this vector.

Definition at line 229 of file mxvk_math.h.

229 {
230 return *this * k;
231 }

◆ Scale() [2/2]

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

Return a scaled copy of this vector.

Definition at line 243 of file mxvk_math_eigen.hpp.

243 {
244 return *this * k;
245 }

◆ ScaleThis() [1/2]

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

Scale this vector in place.

Definition at line 234 of file mxvk_math.h.

234 {
235 x *= k;
236 y *= k;
237 }

◆ ScaleThis() [2/2]

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

Scale this vector in place.

Definition at line 248 of file mxvk_math_eigen.hpp.

248 {
249 FromEigen(ToEigen() * k, *this);
250 }

◆ Set() [1/2]

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

Set both vector coordinates.

Definition at line 192 of file mxvk_math.h.

192 {
193 x = x_value;
194 y = y_value;
195 }

◆ Set() [2/2]

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

Set both vector coordinates.

Definition at line 208 of file mxvk_math_eigen.hpp.

208 {
209 x = x_value;
210 y = y_value;
211 }

Member Data Documentation

◆ x

float mxvk::vec2D::x = 0.0f

X coordinate.

Definition at line 180 of file mxvk_math.h.

◆ y

float mxvk::vec2D::y = 0.0f

Y coordinate.

Definition at line 183 of file mxvk_math.h.


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