Skip to content

Vector4

A 4D vector class for mathematical operations.

Construction

Constructors


Vector4() constexpr

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

cpp
Vector4::Vector4(float value);
ParameterDescription
valueValue to assign to x, y, z, and w.

Vector4() constexpr

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

cpp
Vector4::Vector4(float x, float y, float z, float w);
ParameterDescription
xX component.
yY component.
zZ component.
wW component.

Properties

w float

W component of the vector.

cpp
float w {};

x float

X component of the vector.

cpp
float x {};

y float

Y component of the vector.

cpp
float y {};

z float

Z component of the vector.

cpp
float z {};

Functions

Length() auto

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%).

cpp
auto Vector4::Length() const;

LengthSquared() auto

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.

cpp
auto Vector4::LengthSquared() const;

Max() auto &

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.

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

Min() auto &

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.

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

Normalize() auto &

Normalizes the vector in-place.

If the length is zero, the vector is left unchanged.

cpp
auto& Vector4::Normalize();

operator*=() auto &

Scales each component by another vector.

cpp
auto& Vector4::operator* =(const Vector4& v);
ParameterDescription
v

operator*=() auto &

Scales the vector by a scalar.

cpp
auto& Vector4::operator* =(float n);
ParameterDescription
n

operator+=() auto &

Adds another vector to this one.

cpp
auto& Vector4::operator+=(const Vector4& v);
ParameterDescription
v

operator-=() auto &

Subtracts another vector from this one.

cpp
auto& Vector4::operator-=(const Vector4& v);
ParameterDescription
v

operator[]() auto &

Accesses vector components by index.

cpp
auto& Vector4::operator[](int i);
ParameterDescription
iIndex (0 for x, 1 for y, 2 for z, 3 for w).

operator[]() const auto &

Accesses vector components by index (const).

cpp
const auto& Vector4::operator[](int i) const;
ParameterDescription
iIndex (0 for x, 1 for y, 2 for z, 3 for w).

Vector4::Zero() auto

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

cpp
static constexpr auto Vector4::Zero();

Dot() float

Computes the dot product between two vectors.

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

Lerp() auto

Linearly interpolates between two vectors.

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

Normalize() auto

Returns a normalized copy of the given vector.

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

Released under the MIT License.