Lua operators, such as <code>+</code>, <code>-</code>, and <code>*</code>, apply to vectors and have different results.
==== + (operator) ====
===== With two vectors <span id="meta_add_vv"></span> =====
----
Adds the values from this vector and another vector of the same size. The result is a ''new'' vector of the same size which holds the sum.
{| class="wikitable"
! Left !! Right !! Result
|-
| {{type|{{{T|Vector}}}}}
| {{type|{{{T|Vector}}}}}
| new {{type|{{{T|Vector}}}}}
|}
<syntaxhighlight lang="lua">
local a = vec(1, 2, 3)
local b = vec(4, 4, 6)
print(a + b, a, b) -- {5, 6, 9} {1, 2, 3} {4, 4, 6}
</syntaxhighlight>
===== With a vector and number <span id="meta_add_vn"></span><span id="meta_add_nv"></span> =====
{{Hatnote|Operator is commutative and has the same result regardless of order.}}
The result is a new vector of the same size which holds the result of adding the number to all components of this vector.
{| class="wikitable"
! Left !! Right !! Result
|-
| {{type|{{{T|Vector}}}}}
| {{type|number}}
| new {{type|{{{T|Vector}}}}}
|-
| {{type|number}}
| {{type|{{{T|Vector}}}}}
| new {{type|{{{T|Vector}}}}}
|}
==== - (operator) ====
===== As a unary operator <span id="meta_unm"></span> =====
----
Returns a new vector of the same size, but all the components negated. Effectively the same as <code>[[#meta_mul_vn|*]] -1</code>.
{| class="wikitable"
! Operand !! Result
|-
| {{type|{{{T|Vector}}}}}
| new {{type|{{{T|Vector}}}}}
|}
===== With two vectors <span id="meta_sub_vv"></span> =====
----
{{Sidebox
|Performs [[wikipedia:Euclidean vector#Addition and subtraction|vector subtraction]] with another vector, and creates a new vector from the result.
|Mathematical definition
}}
Subtracts the right vector from the left vector. The result is a ''new'' vector of the same size which holds the difference.
{| class="wikitable"
! Left !! Right !! Result
|-
| {{type|{{{T|Vector}}}}}
| {{type|{{{T|Vector}}}}}
| new {{type|{{{T|Vector}}}}}
|}
<syntaxhighlight lang="lua">
local a = vec(1, 2, 3)
local b = vec(4, 4, 6)
print(b - a, a, b) -- {3, 2, 3} {1, 2, 3} {4, 4, 6}
</syntaxhighlight>
===== With a vector and a number <span id="meta_sub_vn"></span> =====
{{Hatnote|Only applies when the number is on the '''right''' side of the operator.}}
Returns a new vector of the same size containing the result of subtracting the number from all components of the vector.
{| class="wikitable"
! Left !! Right !! Result
|-
| {{type|{{{T|Vector}}}}}
| {{type|number}}
| new {{type|{{{T|Vector}}}}}
|}
===== With a number and a vector <span id="meta_sub_nv"></span> =====
{{Hatnote|Only applies when the number is on the '''left''' side of the operator.}}
Returns a new vector of the same size containing the result of negating all of the components of the vector, then [[#meta_add_vn|adding]] the number. In effect, <code>number - vector == (-vector) + number</code>.
{| class="wikitable"
! Left !! Right !! Result
|-
| {{type|number}}
| {{type|{{{T|Vector}}}}}
| new {{type|{{{T|Vector}}}}}
|}
==== * (operator) ====
===== With two vectors <span id="meta_mul_vv"></span> =====
----
Returns a new vector of the same size containing the result of [[#multiply|multiplying]] the vectors together (component multiplication.)
{| class="wikitable"
! Left !! Right !! Result
|-
| {{type|{{{T|Vector}}}}}
| {{type|{{{T|Vector}}}}}
| new {{type|{{{T|Vector}}}}}
|}
===== With a vector and a number <span id="meta_mul_vn"></span><span id="meta_mul_nv"></span> =====
{{Hatnote|Operator is commutative and has the same result regardless of order.}}
Returns a new vector of the same size containing the result of [[#scale|scaling]] the vector by the number (scalar multiplication.)
{| class="wikitable"
! Left !! Right !! Result
|-
| {{type|{{{T|Vector}}}}}
| {{type|number}}
| new {{type|{{{T|Vector}}}}}
|-
| {{type|number}}
| {{type|{{{T|Vector}}}}}
| new {{type|{{{T|Vector}}}}}
|}
===== With a vector and a matrix <span id="meta_mul_vm"> =====
{{Hatnote|This definition only applies if the vector is on the left side. If the vector is on the right side, see [[{{{M|Matrix}}}#meta_mul_mv|the associated operator on the matrix]].}}
Returns a new vector of the same size containing the result of [[#transform|transforming]] the vector with the matrix.
{| class="wikitable"
! Left !! Right !! Result
|-
| {{type|{{{T|Vector}}}}}
| {{type|{{{M|Matrix}}}}}
| new {{type|{{{T|Vector}}}}}
|}
==== / (operator) ====
===== With two vectors <span id="meta_div_vv"></span> =====
----
Returns a new vector of the same size containing the result of [[#divide|dividing]] the left vector by the right vector (component division.)
{| class="wikitable"
! Left !! Right !! Result
|-
| {{type|{{{T|Vector}}}}}
| {{type|{{{T|Vector}}}}}
| new {{type|{{{T|Vector}}}}}
|}
===== With a vector and a number <span id="meta_div_vn"></span> =====
{{Hatnote|Only applies when the number is on the '''right''' side of the operator. There is no definition with the number on the left side of the operator.}}
Returns a new vector of the same size containing the result of [[#scale|scaling]] the vector by the [[wikipedia:Multiplicative inverse|reciprocal]] of the number.
Returns <code>true</code> if '''all''' of the components of the left vector are strictly less than or equal to the corresponding components of the right vector.
{| class="wikitable"
! Left !! Right !! Result
|-
| {{type|{{{T|Vector}}}}}
| {{type|{{{T|Vector}}}}}
| {{type|boolean}}
|}
<syntaxhighlight lang="lua">
local A = vec(1, 2, 3)
local B = vec(4, 5, 6)
local C = vec(1, 2, 3)
print(A <= B) -- true
print(B <= C) -- true
</syntaxhighlight>
==== tostring return value <span id="meta_tostring"></span> ====
----
Converts the vector to a string representing its components.
{| class="wikitable"
! Operand !! Result
|-
| {{type|{{{T|Vector}}}}}
| {{type|string}}
|}
|}
Revision as of 02:54, 29 October 2024
Math operations
Mathematical operations that apply to all vectors, such as computing their length
Performs vector addition with another vector, storing the result in this vector.
Adds the values from this vector and another vector of the same size, and writes them to this vector. To create a new instance from the sum, see + (operator).
Performs vector subtraction with another vector (in the order this - other), storing the result in this vector.
Subtracts the passed vector of the same size from this vector, and writes the result to this vector. To create a new instance from the difference, see - (operator).
Multiplies the provided matrix with this vector, in the order M * V, and writes the resulting vector to this vector.
Applies a matrix transformation to this vector. Equivalent to multiplying the matrix by the vector. The matrix must be the same size as the vector; the following are valid:
Normalizes this vector in-place. After this operation, the length of the vector will be 1, unless the length is currently 0. If this vector's length is 0, then no normalization will occur.
Scales this vector such that its length is within the bounds specified by minLength and maxLength. If this vector's length is 0, no scaling will be performed, even if minLength is greater than 0. If this vector's length is already within the specified bounds, no scaling will be performed.
Both minLength and maxLength can be omitted or set to nil, resulting in no lower or upper bound on the length of the vector.
Returns a copy of this vector, scaled by a factor of (pi / 180).
If this vector represents a rotation in degrees, the result will be a rotation in radians.
Returns a copy of this vector, scaled by a factor of (180 / pi).
If this vector represents a rotation in radians, the result will be a rotation in degrees.
Creates a copy of this vector. Creating a copy will make a new Vector with the same size and values, but which is disconnected from the original. This means that methods like set or add which modify the vector they are called with will not modify copies.