nupp.math.vec2

Two-dimensional vector operations over (x, y) number pairs.

Module contents

Functions

FunctionKindDescription
addfunctionAdds two vectors.
anglefunctionComputes a vector's signed direction from the positive x-axis.
angleBetweenfunctionComputes the smallest unsigned angle between two vectors.
crossfunctionComputes the signed scalar cross product of two vectors.
distancefunctionComputes the distance between two points.
distanceSquaredfunctionComputes the squared distance between two points without a square root.
dotfunctionComputes the dot product of two vectors.
lengthfunctionComputes a vector's magnitude.
lengthSquaredfunctionComputes a vector's squared magnitude without a square root.
lerpfunctionLinearly interpolates or extrapolates between two vectors.
moveTowardsfunctionMoves toward a target by at most a distance, without overshooting.
normalizefunctionReturns a unit vector in the same direction, or (0, 0) for zero.
projectfunctionProjects a vector onto another vector.
reflectfunctionReflects a vector across a normal.
rotatefunctionRotates a vector around the origin.
scalefunctionMultiplies a vector by a scalar.
signedAngleBetweenfunctionComputes the smallest signed angle from the first vector to the second.
subtractfunctionSubtracts the second vector from the first.

Functions#

addfunction#

local add: function(ax: number, ay: number, bx: number, by: number): (number, number)

Adds two vectors.

Arguments

NameTypeDescription
axnumber

the first vector's x component

aynumber

the first vector's y component

bxnumber

the second vector's x component

bynumber

the second vector's y component

Returns

TypeDescription
number

the sum's x component

number

the sum's y component

anglefunction#

local angle: function(x: number, y: number): number

Computes a vector's signed direction from the positive x-axis.

Arguments

NameTypeDescription
xnumber

the vector's x component

ynumber

the vector's y component

Returns

TypeDescription
number

the angle in radians, or zero for the zero vector

angleBetweenfunction#

local angleBetween: function(ax: number, ay: number, bx: number, by: number): number

Computes the smallest unsigned angle between two vectors.

Arguments

NameTypeDescription
axnumber

the first vector's x component

aynumber

the first vector's y component

bxnumber

the second vector's x component

bynumber

the second vector's y component

Returns

TypeDescription
number

the angle from zero through pi, or zero for a zero vector

crossfunction#

local cross: function(ax: number, ay: number, bx: number, by: number): number

Computes the signed scalar cross product of two vectors.

Arguments

NameTypeDescription
axnumber

the first vector's x component

aynumber

the first vector's y component

bxnumber

the second vector's x component

bynumber

the second vector's y component

Returns

TypeDescription
number

the scalar cross product

distancefunction#

local distance: function(ax: number, ay: number, bx: number, by: number): number

Computes the distance between two points.

Arguments

NameTypeDescription
axnumber

the first point's x coordinate

aynumber

the first point's y coordinate

bxnumber

the second point's x coordinate

bynumber

the second point's y coordinate

Returns

TypeDescription
number

the distance

distanceSquaredfunction#

local distanceSquared: function(ax: number, ay: number, bx: number, by: number): number

Computes the squared distance between two points without a square root.

Arguments

NameTypeDescription
axnumber

the first point's x coordinate

aynumber

the first point's y coordinate

bxnumber

the second point's x coordinate

bynumber

the second point's y coordinate

Returns

TypeDescription
number

the squared distance

dotfunction#

local dot: function(ax: number, ay: number, bx: number, by: number): number

Computes the dot product of two vectors.

Arguments

NameTypeDescription
axnumber

the first vector's x component

aynumber

the first vector's y component

bxnumber

the second vector's x component

bynumber

the second vector's y component

Returns

TypeDescription
number

the dot product

lengthfunction#

local length: function(x: number, y: number): number

Computes a vector's magnitude.

Arguments

NameTypeDescription
xnumber

the vector's x component

ynumber

the vector's y component

Returns

TypeDescription
number

the magnitude

lengthSquaredfunction#

local lengthSquared: function(x: number, y: number): number

Computes a vector's squared magnitude without a square root.

Arguments

NameTypeDescription
xnumber

the vector's x component

ynumber

the vector's y component

Returns

TypeDescription
number

the squared magnitude

lerpfunction#

local lerp: function(ax: number, ay: number, bx: number, by: number, t: number): (number, number)

Linearly interpolates or extrapolates between two vectors.

Arguments

NameTypeDescription
axnumber

the starting x component

aynumber

the starting y component

bxnumber

the ending x component

bynumber

the ending y component

tnumber

the unclamped interpolation factor

Returns

TypeDescription
number

the interpolated x component

number

the interpolated y component

moveTowardsfunction#

local moveTowards: function(ax: number, ay: number, bx: number, by: number, distance: number): (number, number)

Moves toward a target by at most a distance, without overshooting.

Arguments

NameTypeDescription
axnumber

the starting x component

aynumber

the starting y component

bxnumber

the target x component

bynumber

the target y component

distancenumber

the maximum step; nonpositive values leave the start unchanged

Returns

TypeDescription
number

the resulting x component

number

the resulting y component

normalizefunction#

local normalize: function(x: number, y: number): (number, number)

Returns a unit vector in the same direction, or (0, 0) for zero.

Arguments

NameTypeDescription
xnumber

the vector's x component

ynumber

the vector's y component

Returns

TypeDescription
number

the normalized x component

number

the normalized y component

projectfunction#

local project: function(x: number, y: number, ontoX: number, ontoY: number): (number, number)

Projects a vector onto another vector.

Arguments

NameTypeDescription
xnumber

the vector's x component

ynumber

the vector's y component

ontoXnumber

the projection axis's x component

ontoYnumber

the projection axis's y component

Returns

TypeDescription
number

the projected x component, or zero for a zero axis

number

the projected y component, or zero for a zero axis

reflectfunction#

local reflect: function(x: number, y: number, normalX: number, normalY: number): (number, number)

Reflects a vector across a normal.

Arguments

NameTypeDescription
xnumber

the vector's x component

ynumber

the vector's y component

normalXnumber

the normal's x component

normalYnumber

the normal's y component

Returns

TypeDescription
number

the reflected x component, or the input x for a zero normal

number

the reflected y component, or the input y for a zero normal

rotatefunction#

local rotate: function(x: number, y: number, radians: number): (number, number)

Rotates a vector around the origin.

Arguments

NameTypeDescription
xnumber

the vector's x component

ynumber

the vector's y component

radiansnumber

the signed rotation in radians

Returns

TypeDescription
number

the rotated x component

number

the rotated y component

scalefunction#

local scale: function(x: number, y: number, factor: number): (number, number)

Multiplies a vector by a scalar.

Arguments

NameTypeDescription
xnumber

the vector's x component

ynumber

the vector's y component

factornumber

the scale factor

Returns

TypeDescription
number

the scaled x component

number

the scaled y component

signedAngleBetweenfunction#

local signedAngleBetween: function(ax: number, ay: number, bx: number, by: number): number

Computes the smallest signed angle from the first vector to the second.

Arguments

NameTypeDescription
axnumber

the first vector's x component

aynumber

the first vector's y component

bxnumber

the second vector's x component

bynumber

the second vector's y component

Returns

TypeDescription
number

the angle in [-pi, pi), or zero for a zero vector

subtractfunction#

local subtract: function(ax: number, ay: number, bx: number, by: number): (number, number)

Subtracts the second vector from the first.

Arguments

NameTypeDescription
axnumber

the first vector's x component

aynumber

the first vector's y component

bxnumber

the second vector's x component

bynumber

the second vector's y component

Returns

TypeDescription
number

the difference's x component

number

the difference's y component