Skip to content

Vector3

Represents a 3D vector with floating-point components.

Vector3 stores an (x, y, z) triple and is used for positions, directions, normals, and general 3D math. It provides basic arithmetic, normalization, and utility helpers.

Construction

Constructors


Vector3() constexpr

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

cpp
Vector3::Vector3(float value);
ParameterDescription
valueValue to assign to all components.

Vector3() constexpr

Constructs a vector from individual components.

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

Properties

x float

X component.

cpp
float x {};

y float

Y component.

cpp
float y {};

z float

Z component.

cpp
float z {};

Functions

Length() float

Returns the vector length.

cpp
float Vector3::Length() const;

LengthSquared() float

Returns the squared vector length.

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

cpp
float Vector3::LengthSquared() const;

Max() Vector3&

Applies a component-wise maximum with another vector.

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

Min() Vector3&

Applies a component-wise minimum with another vector.

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

Normalize() Vector3&

Normalizes the vector in-place.

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

cpp
Vector3& Vector3::Normalize();

operator*=() Vector3&

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

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

operator*=() Vector3&

Multiplies the vector by a scalar in-place.

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

operator+=() Vector3&

Adds another vector in-place.

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

operator-=() Vector3&

Subtracts another vector in-place.

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

operator[]() float &

Accesses a component by index.

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

operator[]() const float

Accesses a component by index.

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

Vector3::Forward() Vector3

Returns a unit vector pointing forward.

cpp
static constexpr Vector3 Vector3::Forward();

Vector3::Right() Vector3

Returns a unit vector pointing right.

cpp
static constexpr Vector3 Vector3::Right();

Vector3::Up() Vector3

Returns a unit vector pointing up.

cpp
static constexpr Vector3 Vector3::Up();

Vector3::Zero() Vector3

Returns the zero vector.

cpp
static constexpr Vector3 Vector3::Zero();

Cross() Vector3

Computes the cross product of two 3D vectors.

Returns a vector perpendicular to both inputs, following the right-hand rule.

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

Dot() float

Computes the dot product of two 3D vectors.

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

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

Lerp() Vector3

Linearly interpolates between two 3D vectors.

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

Normalize() Vector3

Returns a normalized copy of a vector.

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

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

operator*() Vector3

Multiplies two vectors component-wise.

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

operator*() Vector3

Multiplies a vector by a scalar.

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

operator*() Vector3

Multiplies a scalar by a vector.

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

operator+() Vector3

Adds two 3D vectors.

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

operator-() Vector3

Subtracts one 3D vector from another.

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

operator/() Vector3

Divides a vector by a scalar.

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

Released under the MIT License.