Module: nupp.math.vec2
Two-dimensional vector operations over (x, y) number pairs.
Module contents
Functions
| Function | Kind | Description |
|---|---|---|
add | function | Adds two vectors. |
angle | function | Computes a vector's signed direction from the positive x-axis. |
angleBetween | function | Computes the smallest unsigned angle between two vectors. |
cross | function | Computes the signed scalar cross product of two vectors. |
distance | function | Computes the distance between two points. |
distanceSquared | function | Computes the squared distance between two points without a square root. |
dot | function | Computes the dot product of two vectors. |
length | function | Computes a vector's magnitude. |
lengthSquared | function | Computes a vector's squared magnitude without a square root. |
lerp | function | Linearly interpolates or extrapolates between two vectors. |
moveTowards | function | Moves toward a target by at most a distance, without overshooting. |
normalize | function | Returns a unit vector in the same direction, or (0, 0) for zero. |
project | function | Projects a vector onto another vector. |
reflect | function | Reflects a vector across a normal. |
rotate | function | Rotates a vector around the origin. |
scale | function | Multiplies a vector by a scalar. |
signedAngleBetween | function | Computes the smallest signed angle from the first vector to the second. |
subtract | function | Subtracts 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
| Name | Type | Description |
|---|---|---|
ax | number | the first vector's x component |
ay | number | the first vector's y component |
bx | number | the second vector's x component |
by | number | the second vector's y component |
Returns
| Type | Description |
|---|---|
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): numberArguments
| Name | Type | Description |
|---|---|---|
x | number | the vector's x component |
y | number | the vector's y component |
Returns
| Type | Description |
|---|---|
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): numberArguments
| Name | Type | Description |
|---|---|---|
ax | number | the first vector's x component |
ay | number | the first vector's y component |
bx | number | the second vector's x component |
by | number | the second vector's y component |
Returns
| Type | Description |
|---|---|
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): numberArguments
| Name | Type | Description |
|---|---|---|
ax | number | the first vector's x component |
ay | number | the first vector's y component |
bx | number | the second vector's x component |
by | number | the second vector's y component |
Returns
| Type | Description |
|---|---|
number | the scalar cross product |
distancefunction#
Computes the distance between two points.
local distance: function(ax: number, ay: number, bx: number, by: number): numberArguments
| Name | Type | Description |
|---|---|---|
ax | number | the first point's x coordinate |
ay | number | the first point's y coordinate |
bx | number | the second point's x coordinate |
by | number | the second point's y coordinate |
Returns
| Type | Description |
|---|---|
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): numberArguments
| Name | Type | Description |
|---|---|---|
ax | number | the first point's x coordinate |
ay | number | the first point's y coordinate |
bx | number | the second point's x coordinate |
by | number | the second point's y coordinate |
Returns
| Type | Description |
|---|---|
number | the squared distance |
dotfunction#
Computes the dot product of two vectors.
local dot: function(ax: number, ay: number, bx: number, by: number): numberArguments
| Name | Type | Description |
|---|---|---|
ax | number | the first vector's x component |
ay | number | the first vector's y component |
bx | number | the second vector's x component |
by | number | the second vector's y component |
Returns
| Type | Description |
|---|---|
number | the dot product |
lengthfunction#
Computes a vector's magnitude.
local length: function(x: number, y: number): numberArguments
| Name | Type | Description |
|---|---|---|
x | number | the vector's x component |
y | number | the vector's y component |
Returns
| Type | Description |
|---|---|
number | the magnitude |
lengthSquaredfunction#
Computes a vector's squared magnitude without a square root.
local lengthSquared: function(x: number, y: number): numberArguments
| Name | Type | Description |
|---|---|---|
x | number | the vector's x component |
y | number | the vector's y component |
Returns
| Type | Description |
|---|---|
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
| Name | Type | Description |
|---|---|---|
ax | number | the starting x component |
ay | number | the starting y component |
bx | number | the ending x component |
by | number | the ending y component |
t | number | the unclamped interpolation factor |
Returns
| Type | Description |
|---|---|
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
| Name | Type | Description |
|---|---|---|
ax | number | the starting x component |
ay | number | the starting y component |
bx | number | the target x component |
by | number | the target y component |
distance | number | the maximum step; nonpositive values leave the start unchanged |
Returns
| Type | Description |
|---|---|
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
| Name | Type | Description |
|---|---|---|
x | number | the vector's x component |
y | number | the vector's y component |
Returns
| Type | Description |
|---|---|
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
| Name | Type | Description |
|---|---|---|
x | number | the vector's x component |
y | number | the vector's y component |
ontoX | number | the projection axis's x component |
ontoY | number | the projection axis's y component |
Returns
| Type | Description |
|---|---|
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
| Name | Type | Description |
|---|---|---|
x | number | the vector's x component |
y | number | the vector's y component |
normalX | number | the normal's x component |
normalY | number | the normal's y component |
Returns
| Type | Description |
|---|---|
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
| Name | Type | Description |
|---|---|---|
x | number | the vector's x component |
y | number | the vector's y component |
radians | number | the signed rotation in radians |
Returns
| Type | Description |
|---|---|
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
| Name | Type | Description |
|---|---|---|
x | number | the vector's x component |
y | number | the vector's y component |
factor | number | the scale factor |
Returns
| Type | Description |
|---|---|
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): numberArguments
| Name | Type | Description |
|---|---|---|
ax | number | the first vector's x component |
ay | number | the first vector's y component |
bx | number | the second vector's x component |
by | number | the second vector's y component |
Returns
| Type | Description |
|---|---|
number | the angle in |
subtractfunction#
Subtracts the second vector from the first.
local subtract: function(ax: number, ay: number, bx: number, by: number): (number, number)Arguments
| Name | Type | Description |
|---|---|---|
ax | number | the first vector's x component |
ay | number | the first vector's y component |
bx | number | the second vector's x component |
by | number | the second vector's y component |
Returns
| Type | Description |
|---|---|
number | the difference's x component |
number | the difference's y component |