More actions
m →dot: adjust caps Tag: 2017 source edit |
Add more methods Tag: 2017 source edit |
||
Line 40: | Line 40: | ||
| <code>dot({{type|Vector}} other)</code> | | <code>dot({{type|Vector}} other)</code> | ||
| {{type|number}} | | {{type|number}} | ||
|} | |||
==== add ==== | |||
---- | |||
{{Sidebox | |||
|Performs [[wikipedia:Euclidean vector#Addition and subtraction|vector addition]] with another vector, storing the result in this vector. | |||
|Mathematical definition | |||
}} | |||
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 [[#__add|+ (operator)]]. | |||
{| class="wikitable" | |||
! Arguments !! Return Type | |||
|- | |||
| <code>add({{type|Vector}} other)</code> | |||
| self {{type|Vector}} | |||
|} | |||
==== + (operator) ==== | |||
<span id="__add"></span> | |||
---- | |||
{{Sidebox | |||
|Performs [[wikipedia:Euclidean vector#Addition and subtraction|vector addition]] with another vector, and creates a new vector from the result. | |||
|Mathematical definition | |||
}} | |||
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|Vector}} | |||
| {{type|Vector}} | |||
| {{type|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> | |||
=== Utility operations === | |||
==== 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}} | |||
|} | |} |
Revision as of 01:19, 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 |
+ (operator)
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}
Utility operations
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 |