Module: 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#

Adds two vectors.

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

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#

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

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

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#

Computes the smallest unsigned angle between two vectors.

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

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#

Computes the signed scalar cross product of two vectors.

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

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#

Computes the distance between two points.

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

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#

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

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

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#

Computes the dot product of two vectors.

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

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#

Computes a vector's magnitude.

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

Arguments

NameTypeDescription
xnumber

the vector's x component

ynumber

the vector's y component

Returns

TypeDescription
number

the magnitude

lengthSquaredfunction#

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

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

Arguments

NameTypeDescription
xnumber

the vector's x component

ynumber

the vector's y component

Returns

TypeDescription
number

the squared magnitude

lerpfunction#

Linearly interpolates or extrapolates between two vectors.

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

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#

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

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

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#

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

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

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#

Projects a vector onto another vector.

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

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#

Reflects a vector across a normal.

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

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#

Rotates a vector around the origin.

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

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#

Multiplies a vector by a scalar.

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

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#

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

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

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#

Subtracts the second vector from the first.

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

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