Skip to main content

Vector4

A vector that holds 4 numbers

warning

This page is a WIP. It contains all the information in Figura's documentation but we're working on adding more helpful descriptions.

Can be created using functions in the "vectors" api

For this entire page assume:

local vec4 = vec(2, 5, 4, 96)

Math

add()

Adds the given vector or values to this one, and returns self for chaining

add(vec)

Parameters:

NameTypeDescriptionDefault
vecVector4--

Returns:

TypeDescription
Vector4Returns self for chaining

Example:

vec4:add(2, 0.5, 4, 5)

div()

Divides this vector by the given vector or values, and returns self for chaining

div(vec)

Parameters:

NameTypeDescriptionDefault
vecVector4--

Returns:

TypeDescription
Vector4Returns self for chaining

Example:

vec4:div(2, 3, 2, 3)

mul()

Multiplies the given vector or values into this one, and returns self for chaining

mul(vec)

Parameters:

NameTypeDescriptionDefault
vecVector4--

Returns:

TypeDescription
Vector4Returns self for chaining

Example:

vec4:mul(2, 3, 2, 3)

sub()

Subtracts the given vector or values from this one, and returns self for chaining

sub(vec)

Parameters:

NameTypeDescriptionDefault
vecVector4--

Returns:

TypeDescription
Vector4Returns self for chaining

Example:

vec4:sub(1, 0.5, 1, 0.5)

ceil()

Returns a copy of this vector with its values rounded up

ceil()

Returns:

TypeDescription
Vector4Returns self for chaining

Example:

vec4:ceil()

clampLength()

Modifies this vector so that its length is between minLength and maxLength

If the vector has length zero, it is unmodified

Returns self for chaining

clampLength(minLength, maxLength)

Parameters:

NameTypeDescriptionDefault
minLengthNumber--
maxLengthNumber--

Returns:

TypeDescription
Vector4Returns self for chaining

Example:

vec4:clampLength(1, 5)

clamped()

Returns a modified copy of this vector, with its length clamped from minLength to maxLength

If the vector has length zero, then the copy does too

clamped(minLength, maxLength)

Parameters:

NameTypeDescriptionDefault
minLengthNumber--
maxLengthNumber--

Returns:

TypeDescription
Vector4Returns self for chaining

Example:

vec4:clamped(1, 3)

dot()

Returns the dot product of this vector with the other

dot(vec)

Parameters:

NameTypeDescriptionDefault
vecVector4--

Returns:

TypeDescription
Number-

Example:

vec4:dot(vec(2, 2, 3, 5))

floor()

Returns a copy of this vector with its values rounded down

floor()

Returns:

TypeDescription
Vector4Returns self for chaining

Example:

vec4:floor()

length()

Returns the length of this vector

length()

Returns:

TypeDescription
Number-

Example:

vec4:length()

lengthSquared()

Returns the length of this vector squared

Suitable when you only care about relative lengths, because it avoids a square root

lengthSquared()

Returns:

TypeDescription
Number-

Example:

vec4:lengthSquared()

Transformations

scale()

Scales this vector by the given factor, and returns self for chaining

scale(factor)

Parameters:

NameTypeDescriptionDefault
factorNumber--

Returns:

TypeDescription
Vector4Returns self for chaining

Example:

vec4:scale(2)

normalize()

Modifies this vector so that its length is 1 unless its length was originally 0

Returns self for chaining

normalize()

Returns:

TypeDescription
Vector4Returns self for chaining

Example:

vec4:normalize()

normalized()

Returns a copy of this vector with length 1 unless its length was originally 0

normalized()

Returns:

TypeDescription
Vector4Returns self for chaining

Example:

vec4:normalized()

offset()

Offsets this vector by the given factor, adding the factor to all components, and returns self for chaining

offset(factor)

Parameters:

NameTypeDescriptionDefault
factorNumber--

Returns:

TypeDescription
Vector4Returns self for chaining

Example:

vec4:offset(2)

reduce()

Reduces this vector modulo the given vector or values, and returns self for chaining

reduce(vec)

Parameters:

NameTypeDescriptionDefault
vecVector4--

Returns:

TypeDescription
Vector4Returns self for chaining

Example:

vec4:reduce(1, 0.5, 2, 3)

transform()

Transforms this vector by the given matrix, and returns self for chaining

transform(mat)

Parameters:

NameTypeDescriptionDefault
matMatrix4--

Returns:

TypeDescription
Vector4Returns self for chaining

Example:

vec4:transform(matrices.mat4())

Utility

set()

Sets this vector to have the given values

Nil values are treated as zero

Returns self for chaining

set(vec)

Parameters:

NameTypeDescriptionDefault
vecVector4--

Returns:

TypeDescription
Vector4Returns self for chaining

Example:

vec4:set(1, 2, 3, 4)

applyFunc()

Calls the given function on each element of this vector, and sets the values of the vector to the returns

The current index and its value is given as arguments of the function

Returns self for chaining

applyFunc(func)

Parameters:

NameTypeDescriptionDefault
funcFunction--

Returns:

TypeDescription
Vector4Returns self for chaining

Example:

-- Example 1:
vec4:applyFunc(math.sqrt)
-- Example 2:
vec4:applyFunc(function(v)
return v + math.random() - 0.5
end)

copy()

Creates and returns a copy of this vector

copy()

Returns:

TypeDescription
Vector4Returns self for chaining

Example:

vec4:copy()

reset()

Resets this vector back to being all zeroes, and returns itself for chaining

reset()

Returns:

TypeDescription
Vector4Returns self for chaining

Example:

vec4:reset()

unpack()

Returns this vector's values as separate numbers

unpack()

Returns:

TypeDescription
Numberx value
Numbery value
Numberz value
Numberw value

Example:

local x, y, z, w = vec4:unpack()

Conversion

toDeg()

Returns a copy of this vector, in degrees

toDeg()

Returns:

TypeDescription
Vector4Returns self for chaining

Example:

vec4:toDeg()

toRad()

Returns a copy of this vector, in radians

toRad()

Returns:

TypeDescription
Vector4Returns self for chaining

Example:

vec4:toRad()

Fields

x

The first coordinate of this vector

Can also be gotten with the indices "r" and [1]

Example:

vec4.x
vec4.r
vec4[1]

y

The second coordinate of this vector

Can also be gotten with the indices "g" and [2]

Example:

vec4.y
vec4.g
vec4[2]

z

The third coordinate of this vector

Can also be gotten with the indices "b" and [3]

Example:

vec4.z
vec4.b
vec4[3]

w

The fourth coordinate of this vector

Can also be gotten with the indices "a" and [4]

Example:

vec4.w
vec4.a
vec4[4]