Skip to content

Vector3

A 3D 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.

z float

cpp
float z

Z component of the vector.

Functions

Length() auto

cpp
auto vglx::Vector3::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::Vector3::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::Vector3::Max(const Vector3 &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::Vector3::Min(const Vector3 &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::Vector3::Normalize()

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

operator*=() auto &

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

Scales each component by another vector.

operator*=() auto &

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

Scales the vector by a scalar.

operator+=() auto &

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

Adds another vector to this one.

operator-=() auto &

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

Subtracts another vector from this one.

operator[]() auto &

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

Accesses vector components by index.

operator[]() const auto &

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

Accesses vector components by index (const).

Vector3() constexpr

cpp
vglx::Vector3::Vector3(float value)
ParameterDescription
valueValue to assign to x, y and z.

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

Vector3() constexpr

cpp
vglx::Vector3::Vector3(float x, float y, float z)
ParameterDescription
xX component.
yY component.
zZ component.

Constructs a vector with specified x, y, and z components.

Vector3::Forward() auto

cpp
static constexpr auto vglx::Vector3::Forward()

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

Vector3::Right() auto

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

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

Vector3::Up() auto

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

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

Vector3::Zero() auto

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

Returns a zero vector (0, 0, 0).

Cross() auto

cpp
auto Cross(const Vector3 &a, const Vector3 &b)
ParameterDescription
aFirst vector.
bSecond vector.

Computes the cross product between two vectors.

Dot() float

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

Computes the dot product between two vectors.

Lerp() auto

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

Linearly interpolates between two vectors.

Normalize() auto

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

Returns a normalized copy of the given vector.

Released under the MIT License.