vec2 class by value built-in

Two-dimensional vector with floating-point components

Typically used for positions, directions and speeds.

Constructors

vec2(const vec2&in)
Creates a copy of another vector
vec2(const vec2i&in)
Creates a vector from an integer vector
vec2(float, float)
Creates a vector from X and Y components
vec2(int, int)
Creates a vector from integer X and Y components

Functions

void normalize()
Scales the vector in place to a length of 1
vec2 normalized() const
Returns a copy of the vector scaled to a length of 1

Operators

vec2 opAdd(const vec2&in) const +
Adds two vectors
vec2& opAddAssign(const vec2&in) +=
Adds another vector in place
vec2& opAssign(const vec2&in) =
Copies another vector
vec2 opDiv(float) const /
Divides every component by a number
vec2 opDiv(const vec2&in) const /
Divides the vectors component by component
vec2& opDivAssign(float) /=
Divides every component by a number in place
vec2& opDivAssign(const vec2&in) /=
Divides by another vector component by component in place
bool opEquals(const vec2&in) const ==
Returns whether all components are exactly equal
vec2 opMul(float) const *
Multiplies every component by a number
vec2 opMul(const vec2&in) const *
Multiplies the vectors component by component
vec2& opMulAssign(float) *=
Multiplies every component by a number in place
vec2& opMulAssign(const vec2&in) *=
Multiplies by another vector component by component in place
vec2 opNeg() const -x
Returns the vector with all components negated
vec2 opSub(const vec2&in) const -
Subtracts two vectors
vec2& opSubAssign(const vec2&in) -=
Subtracts another vector in place

Properties

float length read-only
Length of the vector
float lengthSquared read-only
Squared length of the vector
float x
X component
float y
Y component

Function documentation

void vec2::normalize()

Scales the vector in place to a length of 1

A zero-length vector becomes NaN in all components.

vec2 vec2::normalized() const

Returns a copy of the vector scaled to a length of 1

A zero-length vector gives NaN in all components.

Operator documentation

bool vec2::opEquals(const vec2&in) const ==

Returns whether all components are exactly equal

Use closeTo() on the components to allow for rounding errors.

Property documentation

float vec2::lengthSquared read-only

Squared length of the vector

Cheaper than the length, useful for comparing distances.