Vector4
A vector that holds 4 numbers
Can be created using functions in the "vectors" api
For this entire page assume:
local vec4 = vec(2, 5, 3, 4)
add(Vector4)
Adds the given vector or values to this one, and returns self for chaining
Example:
vec4:add(2, 0.5, 4, 5)
length()
Returns the length of this vector
Example:
vec4:length()
floor()
Returns a copy of this vector with its values rounded down
Example:
vec4:floor()
ceil()
Returns a copy of this vector with its values rounded up
Example:
vec4:ceil()
scale(Number)
Scales this vector by the given factor, and returns self for chaining
Example:
vec4:scale(2)
offset(Number)
Offsets this vector by the given factor, adding the factor to all components, and returns self for chaining
Example:
vec4:offset(2)
transform(Matrix3)
Transforms this vector by the given matrix, and returns self for chaining
Example:
vec4:transform(matrices.mat4())
dot(Vector4)
Returns the dot product of this vector with the other
Example:
vec4:dot(vec(2, 2, 3, 5))
set(Vector4)
Sets this vector to have the given values
Nil values are treated as zero
Returns self for chaining
Example:
vec4:set(10, 3, 2, 4)
copy()
Creates and returns a copy of this vector
Example:
vec4:copy()
normalize()
Modifies this vector so that its length is 1, unless its length was originally 0
Returns self for chaining
Example:
vec4:normalize()
reset()
Resets this vector back to being all zeroes, and returns itself for chaining
Example:
vec4:reset()
reduce(Vector4)
Reduces this vector modulo the given vector or values, and returns self for chaining
Example:
vec4:reduce(1, 0.5, 2, 3)
normalized()
Returns a copy of this vector with length 1, unless its length was originally 0
Example:
vec4:normalized()
sub(Vector4)
Subtracts the given vector or values from this one, and returns self for chaining
Example:
vec4:sub(1, 0.5, 1, 0.5)
mul(Vector4)
Multiplies the given vector or values into this one, and returns self for chaining
Example:
vec4:mul(2, 3, 2, 3)
div(Vector4)
Divides this vector by the given vector or values, and returns self for chaining
Example:
vec4:mul(2, 3, 2, 3)
applyFunc(fun)
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
Examples:
-- Example 1:
vec4:applyFunc(math.sqrt)
-- Example 2:
vec4:applyFunc(function(v)
return v + math.random() - 0.5
end)
toRad()
Returns a copy of this vector, in radians
Example:
vec4:toRad()
toDeg()
Returns a copy of this vector, in degrees
Example:
vec4:toDeg()
clampLength(Number,Number)
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
Example:
vec4:clampLength(1, 5)
unpack()
Returns each of this vector values as argument
Example:
vec4:unpack()
clamped(Number,Number)
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
Example:
vec4:clamped(1, 3)
lengthSquared()
Returns the length of this vector squared
Suitable when you only care about relative lengths, because it avoids a square root
Example:
vec4:lengthSquared()
x
The first coordinate of this vector
Can also be gotten with the indices "r" and [1]
Example:
vec4.x
y
The second coordinate of this vector
Can also be gotten with the indices "g" and [2]
Example:
vec4.y
z
The third coordinate of this vector
Can also be gotten with the indices "b" and [3]
Example:
vec4.z
w
The fourth coordinate of this vector
Can also be gotten with the indices "a" and [4]
Example:
vec4.w