More actions
Tag: 2017 source edit |
Relocate operators Tag: 2017 source edit |
||
Line 57: | Line 57: | ||
| self {{type|Vector}} | | self {{type|Vector}} | ||
|} | |} | ||
==== subtract ==== | |||
---- | |||
{{Sidebox | |||
|Performs [[wikipedia:Euclidean vector#Addition and subtraction|vector subtraction]] with another vector (in the order '''this - other'''), storing the result in this vector. | |||
|Mathematical definition | |||
}} | |||
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 [[#meta_sub_vv|- (operator)]]. | |||
{| class="wikitable" | |||
! Arguments !! Return Type | |||
|- | |||
| <code>subtract({{type|Vector}} other)</code> | |||
| self {{type|Vector}} | |||
|} | |||
==== offset ==== | |||
---- | |||
Adds ''factor'' to all components of this vector. To create a new instance from the result, see [[#meta_add_vn|+ (operator)]] or [[#meta_sub_vn|- (operator)]]. | |||
{| class="wikitable" | |||
! Arguments !! Return Type | |||
|- | |||
| <code>offset({{type|number}} factor)</code> | |||
| self {{type|Vector}} | |||
|} | |||
==== multiply ==== | |||
---- | |||
Multiplies vectors of the same size by multiplying their components. The result is written to this vector. | |||
{| class="wikitable" | |||
! Arguments !! Return Type | |||
|- | |||
| <code>multiply({{type|Vector}} other)</code> | |||
| self {{type|Vector}} | |||
|} | |||
=== Utility methods === | |||
==== copy ==== | |||
---- | |||
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|set]] or [[#add|add]] which modify the vector they are called with will not modify copies. | |||
{| class="wikitable" | |||
! Arguments !! Return Type | |||
|- | |||
| <code>copy()</code> | |||
| {{type|Vector}} | |||
|} | |||
==== set ==== | |||
---- | |||
Assigns the values from another vector of the same size to this vector in place. To create a new instance, see [[#copy|copy]]. | |||
{| class="wikitable" | |||
! Arguments !! Return Type | |||
|- | |||
| <code>set({{type|Vector}} other)</code> | |||
| self {{type|Vector}} | |||
|} | |||
=== Operators === | |||
Lua operators, such as <code>+</code>, <code>-</code>, and <code>*</code>, apply to vectors and have different results. | |||
==== + (operator) ==== | ==== + (operator) ==== | ||
Line 98: | Line 167: | ||
| {{type|Vector}} | | {{type|Vector}} | ||
| {{type|Vector}} | | {{type|Vector}} | ||
|} | |} | ||
Line 141: | Line 194: | ||
===== With a vector and a number <span id="meta_sub_vn"></span> ===== | ===== 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}} | {{Hatnote|Only applies when the number is on the '''right''' side of the operator.}} | ||
---- | ---- | ||
Line 155: | Line 208: | ||
===== With a number and a vector <span id="meta_sub_nv"></span> ===== | ===== 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}} | {{Hatnote|Only applies when the number is on the '''left''' side of the operator.}} | ||
---- | ---- | ||
Line 166: | Line 219: | ||
| {{type|Vector}} | | {{type|Vector}} | ||
| {{type|Vector}} | | {{type|Vector}} | ||
|} | |} |
Revision as of 01:46, 29 October 2024
Math operations
Mathematical operations that apply to all vectors, such as computing their length
length
Computes the Euclidean norm of the vector.
Computes the length of a vector.
Arguments | Return Type |
---|---|
length()
|
number |
lengthSquared
Computes the length of a vector, squared. Due to the nature of the length calculation, this is a little bit faster than length
.
Arguments | Return Type |
---|---|
lengthSquared()
|
number |
dot
Computes the dot product between two vectors. The argument must be the same type as the target vector.
Arguments | Return Type |
---|---|
dot(Vector other)
|
number |
add
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).
Arguments | Return Type |
---|---|
add(Vector other)
|
self Vector |
subtract
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).
Arguments | Return Type |
---|---|
subtract(Vector other)
|
self Vector |
offset
Adds factor to all components of this vector. To create a new instance from the result, see + (operator) or - (operator).
Arguments | Return Type |
---|---|
offset(number factor)
|
self Vector |
multiply
Multiplies vectors of the same size by multiplying their components. The result is written to this vector.
Arguments | Return Type |
---|---|
multiply(Vector other)
|
self Vector |
Utility methods
copy
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.
Arguments | Return Type |
---|---|
copy()
|
Vector |
set
Assigns the values from another vector of the same size to this vector in place. To create a new instance, see copy.
Arguments | Return Type |
---|---|
set(Vector other)
|
self Vector |
Operators
Lua operators, such as +
, -
, and *
, apply to vectors and have different results.
+ (operator)
With two vectors
Performs vector addition with another vector, and creates a new vector from the result.
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.
Left | Right | Result |
---|---|---|
Vector | Vector | Vector |
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}
With a vector and number
The result is a new vector of the same size which holds the result of adding the number to all components of this vector.
Left | Right | Result |
---|---|---|
Vector | number | Vector |
number | Vector | Vector |
- (operator)
With two vectors
Performs vector subtraction with another vector, and creates a new vector from the result.
Subtracts the right vector from the left vector. The result is a new vector of the same size which holds the difference.
Left | Right | Result |
---|---|---|
Vector | Vector | Vector |
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}
With a vector and a number
Returns a new vector of the same size containing the result of subtracting the number from all components of the vector.
Left | Right | Result |
---|---|---|
Vector | number | Vector |
With a number and a vector
Returns a new vector of the same size containing the result of negating all of the components of the vector, then adding the number. In effect, number - vector == (-vector) + number
.
Left | Right | Result |
---|---|---|
number | Vector | Vector |