Skip to content

Vector4 ​

Represents a 4D floating-point vector.

Vector4 stores four components (x, y, z, w) and is typically used for homogeneous coordinates, and general 4D math. It provides basic arithmetic, and utility helpers.

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 all four components.

Vector4() constexpr ​

Constructs a vector from individual 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.

cpp
float w {};

x float ​

X component.

cpp
float x {};

y float ​

Y component.

cpp
float y {};

z float ​

Z component.

cpp
float z {};

Functions ​

Length() float ​

Computes the vector length.

cpp
float Vector4::Length() const;

LengthSquared() float ​

Computes the squared vector length.

Useful when comparing lengths without paying the cost of a square root.

cpp
float Vector4::LengthSquared() const;

Max() Vector4& ​

Applies a component-wise maximum.

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

Min() Vector4& ​

Applies a component-wise minimum.

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

Normalize() Vector4& ​

Normalizes the vector in-place.

If the length is zero, the vector is unchanged.

cpp
Vector4& Vector4::Normalize();

operator*=() Vector4& ​

Multiplies the vector component-wise by another vector in-place.

cpp
Vector4& Vector4::operator* =(const Vector4& v);
ParameterDescription
vVector to multiply.

operator*=() Vector4& ​

Multiplies the vector by a scalar in-place.

cpp
Vector4& Vector4::operator* =(float n);
ParameterDescription
nScalar value.

operator+=() Vector4& ​

Adds another vector in-place.

cpp
Vector4& Vector4::operator+=(const Vector4& v);
ParameterDescription
vVector to add.

operator-=() Vector4& ​

Subtracts another vector in-place.

cpp
Vector4& Vector4::operator-=(const Vector4& v);
ParameterDescription
vVector to subtract.

operator[]() float& ​

Accesses a component by index.

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

operator[]() float ​

Accesses a component by index.

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

Vector4::UnitW() Vector4 ​

Returns the unit vector along the W axis.

cpp
static constexpr Vector4 Vector4::UnitW();

Vector4::UnitX() Vector4 ​

Returns the unit vector along the X axis.

cpp
static constexpr Vector4 Vector4::UnitX();

Vector4::UnitY() Vector4 ​

Returns the unit vector along the Y axis.

cpp
static constexpr Vector4 Vector4::UnitY();

Vector4::UnitZ() Vector4 ​

Returns the unit vector along the Z axis.

cpp
static constexpr Vector4 Vector4::UnitZ();

Vector4::Zero() Vector4 ​

Returns the zero vector.

cpp
static constexpr Vector4 Vector4::Zero();

Dot() float ​

Computes the dot product of two 4D vectors.

Computes the scalar product (), which measures how aligned the two vectors are.

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

Lerp() auto ​

Linearly interpolates between two 4D vectors.

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

Normalize() Vector4 ​

Returns a normalized copy of a vector.

If the input has zero length, the zero vector is returned.

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

operator*() Vector4 ​

Multiplies two vectors component-wise.

cpp
Vector4 operator* (const Vector4& a, const Vector4& b);
ParameterDescription
aFirst vector.
bSecond vector.

operator*() Vector4 ​

Multiplies a vector by a scalar.

cpp
Vector4 operator* (const Vector4& v, float n);
ParameterDescription
vInput vector.
nScalar value.

operator*() Vector4 ​

Multiplies a scalar by avector.

cpp
Vector4 operator* (float n, const Vector4& v);
ParameterDescription
nScalar value.
vInput vector.

operator+() Vector4 ​

Adds two 4D vectors.

cpp
Vector4 operator+(const Vector4& a, const Vector4& b);
ParameterDescription
aFirst vector.
bSecond vector.

operator-() Vector4 ​

Subtracts one 4D vector from another.

cpp
Vector4 operator-(const Vector4& a, const Vector4& b);
ParameterDescription
aFirst vector.
bSecond vector.

operator/() Vector4 ​

Divides two vectors component-wise.

cpp
Vector4 operator/(const Vector4& a, const Vector4& b);
ParameterDescription
aFirst vector.
bSecond vector.

operator/() Vector4 ​

Divides a vector by a scalar.

cpp
Vector4 operator/(const Vector4& v, float n);
ParameterDescription
vInput vector.
nScalar value.

Released under the MIT License.