Skip to content

Vector2

Represents a 2D vector with floating-point components.

Vector2 stores an (x, y) pair and is used for positions, directions, UV coordinates, and general 2D math. It provides basic arithmetic, normalization, and utility helpers.

Construction

Constructors


Vector2() constexpr

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

cpp
Vector2::Vector2(float value);
ParameterDescription
valueValue to assign to both components.

Vector2() constexpr

Constructs a vector from individual components.

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

Properties

x float

X component.

cpp
float x {};

y float

Y component.

cpp
float y {};

Functions

Length() float

Returns the vector length.

cpp
float Vector2::Length() const;

LengthSquared() float

Returns the squared vector length.

cpp
float Vector2::LengthSquared() const;

Max() Vector2&

Applies a component-wise maximum with another vector.

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

Min() Vector2&

Applies a component-wise minimum with another vector.

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

Normalize() Vector2&

Normalizes the vector in-place.

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

cpp
Vector2& Vector2::Normalize();

operator*=() Vector2&

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

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

operator*=() Vector2&

Multiplies the vector by a scalar in-place.

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

operator+=() Vector2&

Adds another vector in-place.

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

operator-=() Vector2&

Subtracts another vector in-place.

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

operator[]() float &

Accesses a component by index.

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

operator[]() const float

Accesses a component by index.

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

Vector2::Right() Vector2

Returns a unit vector pointing to the right.

cpp
static constexpr Vector2 Vector2::Right();

Vector2::Up() Vector2

Returns a unit vector pointing up.

cpp
static constexpr Vector2 Vector2::Up();

Vector2::Zero() Vector2

Returns the zero vector.

cpp
static constexpr Vector2 Vector2::Zero();

Dot() float

Computes the dot product of two 2D vectors.

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

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

Lerp() Vector2

Linearly interpolates between two vectors.

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

Normalize() Vector2

Returns a normalized copy of a vector.

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

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

operator*() Vector2

Multiplies two vectors component-wise.

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

operator*() Vector2

Multiplies a vector by a scalar.

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

operator*() Vector2

Multiplies a scalar by a vector.

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

operator+() Vector2

Adds two 2D vectors.

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

operator-() Vector2

Subtracts one 2D vector from another.

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

operator/() Vector2

Divides a vector by a scalar.

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

Released under the MIT License.