Vector2
A vector that holds 2 numbers
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 vec2 = vec(2, 5)
Math
add()
Adds the given vector or values to this one, and returns self for chaining
- Overload 1
- Overload 2
Example:
vec2:add(2, 0.5)
div()
Divides this vector by the given vector or values, and returns self for chaining
- Overload 1
- Overload 2
Example:
vec2:div(2, 3)
mul()
Multiplies the given vector or values into this one, and returns self for chaining
- Overload 1
- Overload 2
Example:
vec2:mul(2, 3)
sub()
Subtracts the given vector or values from this one, and returns self for chaining
- Overload 1
- Overload 2
Example:
vec2:sub(1, 0.5)
ceil()
Returns a copy of this vector with its values rounded up
ceil()
Returns:
Type | Description |
---|---|
Vector2 | Returns self for chaining |
Example:
vec2: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:
Name | Type | Description | Default |
---|---|---|---|
minLength | Number | - | - |
maxLength | Number | - | - |
Returns:
Type | Description |
---|---|
Vector2 | Returns self for chaining |
Example:
vec2: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:
Name | Type | Description | Default |
---|---|---|---|
minLength | Number | - | - |
maxLength | Number | - | - |
Returns:
Type | Description |
---|---|
Vector2 | Returns self for chaining |
Example:
vec2:clamped(1, 3)
dot()
Returns the dot product of this vector with the other
dot(vec)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vec | Vector2 | - | - |
Returns:
Type | Description |
---|---|
Number | - |
Example:
vec2:dot(vec(2, 2))
floor()
Returns a copy of this vector with its values rounded down
floor()
Returns:
Type | Description |
---|---|
Vector2 | Returns self for chaining |
Example:
vec2:floor()
length()
Returns the length of this vector
length()
Returns:
Type | Description |
---|---|
Number | - |
Example:
vec2: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:
Type | Description |
---|---|
Number | - |
Example:
vec2:lengthSquared()
Transformations
scale()
Scales this vector by the given factor, and returns self for chaining
scale(factor)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
factor | Number | - | - |
Returns:
Type | Description |
---|---|
Vector2 | Returns self for chaining |
Example:
vec2:scale(2)
normalize()
Modifies this vector so that its length is 1 unless its length was originally 0
Returns self for chaining
normalize()
Returns:
Type | Description |
---|---|
Vector2 | Returns self for chaining |
Example:
vec2:normalize()
normalized()
Returns a copy of this vector with length 1 unless its length was originally 0
normalized()
Returns:
Type | Description |
---|---|
Vector2 | Returns self for chaining |
Example:
vec2:normalized()
offset()
Offsets this vector by the given factor, adding the factor to all components, and returns self for chaining
offset(factor)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
factor | Number | - | - |
Returns:
Type | Description |
---|---|
Vector2 | Returns self for chaining |
Example:
vec2:offset(2)
reduce()
Reduces this vector modulo the given vector or values, and returns self for chaining
- Overload 1
- Overload 2
Example:
vec2:reduce(1, 0.5)
transform()
Transforms this vector by the given matrix, and returns self for chaining
transform(mat)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mat | Matrix2 | - | - |
Returns:
Type | Description |
---|---|
Vector2 | Returns self for chaining |
Example:
vec2:transform(matrices.mat2())
Utility
set()
Sets this vector to have the given values
Nil values are treated as zero
Returns self for chaining
- Overload 1
- Overload 2
Example:
vec2:offset(2)
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:
Name | Type | Description | Default |
---|---|---|---|
func | Function | - | - |
Returns:
Type | Description |
---|---|
Vector2 | Returns self for chaining |
Example:
-- Example 1:
vec2:applyFunc(math.sqrt)
-- Example 2:
vec2:applyFunc(function(v)
return v + math.random() - 0.5
end)
augmented()
Returns the augmented form of this vector
The augmented form is a Vector of the same length + 1
The new axis will have the given value, or 1 when it is not specified
- Overload 1
- Overload 2
Example:
vec2:augmented(4)
copy()
Creates and returns a copy of this vector
copy()
Returns:
Type | Description |
---|---|
Vector2 | Returns self for chaining |
Example:
vec2:copy()
reset()
Resets this vector back to being all zeroes, and returns itself for chaining
reset()
Returns:
Type | Description |
---|---|
Vector2 | Returns self for chaining |
Example:
vec2:reset()
unpack()
Returns this vector's values as separate numbers
unpack()
Returns:
Type | Description |
---|---|
Number | x value |
Number | y value |
Example:
local x, y = vec2:unpack()
Conversion
toDeg()
Returns a copy of this vector, in degrees
toDeg()
Returns:
Type | Description |
---|---|
Vector2 | Returns self for chaining |
Example:
vec2:toDeg()
toRad()
Returns a copy of this vector, in radians
toRad()
Returns:
Type | Description |
---|---|
Vector2 | Returns self for chaining |
Example:
vec2:toRad()
Fields
x
The first coordinate of this vector
Can also be gotten with the indices "r" and [1]
Example:
vec2.x
vec2.r
vec2[1]
y
The second coordinate of this vector
Can also be gotten with the indices "g" and [2]
Example:
vec2.y
vec2.g
vec2[2]