Skip to content

Vector2

A 2D vector class for mathematical operations.

Properties

x float

cpp
float x

X component of the vector.

y float

cpp
float y

Y component of the vector.

Functions

Length() auto

cpp
auto vglx::Vector2::Length() const

Computes the approximate magnitude of the vector. This function uses a fast inverse square root approximation to compute the square root, which is significantly faster than the standard library sqrt, but introduces a small precision error (~0.001%).

LengthSquared() auto

cpp
auto vglx::Vector2::LengthSquared() const

Computes the squared length (magnitude) of the vector. This is a precise and inexpensive operation that avoids computing a square root. Use this when comparing relative lengths or avoiding unnecessary precision loss.

Max() auto &

cpp
auto & vglx::Vector2::Max(const Vector2 &v) noexcept
ParameterDescription
vVector to compare against.

Component-wise in-place maximum. Sets each component of this vector to the larger of the corresponding components in this vector and the given vector v.

Min() auto &

cpp
auto & vglx::Vector2::Min(const Vector2 &v) noexcept
ParameterDescription
vVector to compare against.

Component-wise in-place minimum. Sets each component of this vector to the smaller of the corresponding components in this vector and the given vector v.

Normalize() auto &

cpp
auto & vglx::Vector2::Normalize()

Normalizes the vector in-place. If the length is zero, the vector is left unchanged.

operator*=() auto &

cpp
auto & vglx::Vector2::operator*=(const Vector2 &v)
ParameterDescription
vNone

Scales each component by another vector.

operator*=() auto &

cpp
auto & vglx::Vector2::operator*=(float n)
ParameterDescription
nNone

Scales the vector by a scalar.

operator+=() auto &

cpp
auto & vglx::Vector2::operator+=(const Vector2 &v)
ParameterDescription
vNone

Adds another vector to this one.

operator-=() auto &

cpp
auto & vglx::Vector2::operator-=(const Vector2 &v)
ParameterDescription
vNone

Subtracts another vector from this one.

operator[]() auto &

cpp
auto & vglx::Vector2::operator[](int i)
ParameterDescription
iIndex (0 for x, 1 for y).

Accesses vector components by index.

operator[]() const auto &

cpp
const auto & vglx::Vector2::operator[](int i) const
ParameterDescription
iIndex (0 for x, 1 for y).

Accesses vector components by index (const).

Vector2() constexpr

cpp
vglx::Vector2::Vector2(float value)
ParameterDescription
valueValue to assign to both x and y.

Constructs a vector with both components set to the same value.

Vector2() constexpr

cpp
vglx::Vector2::Vector2(float x, float y)
ParameterDescription
xX component.
yY component.

Constructs a vector with specified x and y components.

Vector2::Right() auto

cpp
static constexpr auto vglx::Vector2::Right()

Returns the unit vector pointing to the right (1, 0).

Vector2::Up() auto

cpp
static constexpr auto vglx::Vector2::Up()

Returns the unit vector pointing upward (0, 1).

Vector2::Zero() auto

cpp
static constexpr auto vglx::Vector2::Zero()

Returns a zero vector (0, 0).

Dot() float

cpp
float Dot(const Vector2 &a, const Vector2 &b)
ParameterDescription
aFirst vector.
bSecond vector.

Computes the dot product between two vectors.

Lerp() auto

cpp
auto Lerp(const Vector2 &v1, const Vector2 &v2, float f)
ParameterDescription
v1Start vector.
v2End vector.
fInterpolation factor [0, 1].

Linearly interpolates between two vectors.

Normalize() auto

cpp
auto Normalize(const Vector2 &v)
ParameterDescription
vInput vector.

Returns a normalized copy of the given vector.

Released under the MIT License.