Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Template:Main:Vector/Operators: Difference between revisions

Template page
PenguinEncounter (talk | contribs)
m PenguinEncounter moved page User:PenguinEncounter/Vector/Operators to Vector/Operators: Move to mainspace from user draft
PenguinEncounter (talk | contribs)
m PenguinEncounter moved page Vector/Operators to Template:Main:Vector/Operators: trying to get this out of the search results
 
(2 intermediate revisions by the same user not shown)
Line 174: Line 174:
----
----


Returns the number of components in this vector {{#if:{{{Size|}}}|({{{Size}}}.)|.}}
Returns the number of components in this vector{{#if:{{{Size|}}}| ({{{Size}}})|.}}


{| class="wikitable"
{| class="wikitable"

Latest revision as of 23:40, 29 October 2024

Lua operators, such as +, -, and *, apply to vectors and have different results.

+ (operator)

With two vectors

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 new 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
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.

Left Right Result
Vector number new Vector
number Vector new Vector

- (operator)

As a unary operator

Returns a new vector of the same size, but all the components negated. Effectively the same as * -1.

Operand Result
Vector new Vector
With two vectors

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 new 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
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.

Left Right Result
Vector number new Vector
With a number and a vector
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 adding the number. In effect, number - vector == (-vector) + number.

Left Right Result
number Vector new Vector

* (operator)

With two vectors

Returns a new vector of the same size containing the result of multiplying the vectors together (component multiplication.)

Left Right Result
Vector Vector new Vector
With a vector and a number
Operator is commutative and has the same result regardless of order.

Returns a new vector of the same size containing the result of scaling the vector by the number (scalar multiplication.)

Left Right Result
Vector number new Vector
number Vector new Vector
With a vector and a matrix
This definition only applies if the vector is on the left side. If the vector is on the right side, see the associated operator on the matrix.

Returns a new vector of the same size containing the result of transforming the vector with the matrix.

Left Right Result
Vector Matrix new Vector

/ (operator)

With two vectors

Returns a new vector of the same size containing the result of dividing the left vector by the right vector (component division.)

Left Right Result
Vector Vector new Vector
With a vector and a number
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 scaling the vector by the reciprocal of the number.

Left Right Result
Vector number new Vector

# (operator)


Returns the number of components in this vector.

Operand Result
Vector number

< (operator)


Returns true if all of the components of the left vector are strictly less than the corresponding components of the right vector.

Left Right Result
Vector Vector boolean
local A = vec(1, 2, 3)
local B = vec(4, 5, 6)
local C = vec(4, 1, 6)
print(A < B) -- true
print(B < C) -- false

<= (operator)


Returns true if all of the components of the left vector are strictly less than or equal to the corresponding components of the right vector.

Left Right Result
Vector Vector boolean
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

tostring return value


Converts the vector to a string representing its components.

Operand Result
Vector string