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

Template:Main:Vector/Methods: Difference between revisions

Template page
PenguinEncounter (talk | contribs)
m →‎Operators: Adjust horizontal lines
Tag: 2017 source edit
PenguinEncounter (talk | contribs)
m →‎Operators: Adjust horizontal lines
Tag: 2017 source edit
Line 284: Line 284:


===== With a vector and number <span id="meta_add_vn"></span><span id="meta_add_nv"></span> =====
===== 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.}}
{{Hatnote|Operator is commutative and has the same result regardless of order.}}


Line 326: Line 325:


===== 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 340: Line 338:


===== 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 368: Line 365:


===== With a vector and a number <span id="meta_mul_vn"></span><span id="meta_mul_nv"></span> =====
===== 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.}}
{{Hatnote|Operator is commutative and has the same result regardless of order.}}


Line 386: Line 382:


===== With a vector and a matrix <span id="meta_mul_vm"> =====
===== 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]].}}
{{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]].}}


Line 414: Line 409:


===== With a vector and a number <span id="meta_div_vn"></span> =====
===== 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.}}
{{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.}}



Revision as of 02:30, 29 October 2024

Math operations

Mathematical operations that apply to all vectors, such as computing their length

length


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


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


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

transform


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:

Arguments Return Type
transform(Matrix mat) self Vector

divide


Divides vectors of the same size by dividing their components. The result is written to this vector.

Arguments Return Type
divide(Vector other) self Vector

reduce


For each component in this vector, perform the positive modulo operation on it and the equivalent component in the other vector.

Arguments Return Type
reduce(Vector other) self Vector

scale


Multiplies each component in this vector by factor.

Arguments Return Type
scale(number factor) self Vector

normalize


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.

Arguments Return Type
normalize() self Vector

clampLength


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.

Arguments Return Type
clampLength(number | nil minLength, number | nil maxLength) self Vector

clamped


Copies this vector, then clamps its length. See clampLength for details.

Arguments Return Type
clamped(number | nil minLength, number | nil maxLength) new Vector

normalized


Copies this vector, then normalizes it. See normalize for details.

Arguments Return Type
normalized() new Vector

toRad


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.

See also toDeg.

Arguments Return Type
toRad() new Vector

toDeg


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.

See also toRad.

Arguments Return Type
toDeg() new 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() new 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

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)

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