vector
The vector API provides methods to create and manipulate vectors.
An introduction to vectors can be found on Wikipedia.
new(x, y, z) | Construct a new Vector with the given coordinates. |
---|
- new(x, y, z)Source
Construct a new Vector with the given coordinates.
Parameters
- x number The X coordinate or direction of the vector.
- y number The Y coordinate or direction of the vector.
- z number The Z coordinate or direction of the vector.
Returns
- Vector The constructed vector.
Types
Vector
A 3-dimensional vector, with x
, y
, and z
values.
This is suitable for representing both position and directional vectors.
- Vector:add(o)Source
Adds two vectors together.
Parameters
- o Vector The second vector to add.
Returns
- Vector The resulting vector
Usage
v1:add(v2)
v1 + v2
- Vector:sub(o)Source
Subtracts one vector from another.
Parameters
- o Vector The vector to subtract.
Returns
- Vector The resulting vector
Usage
v1:sub(v2)
v1 - v2
- Vector:mul(m)Source
Multiplies a vector by a scalar value.
Parameters
- m number The scalar value to multiply with.
Returns
- Vector A vector with value
(x * m, y * m, z * m)
.
Usage
v:mul(3)
v * 3
- Vector:div(m)Source
Divides a vector by a scalar value.
Parameters
- m number The scalar value to divide by.
Returns
- Vector A vector with value
(x / m, y / m, z / m)
.
Usage
v:div(3)
v / 3
- Vector:unm()Source
Negate a vector
Returns
- Vector The negated vector.
Usage
-v
- Vector:dot(o)Source
Compute the dot product of two vectors
Parameters
- o Vector The second vector to compute the dot product of.
Returns
- Vector The dot product of
self
ando
.
Usage
v1:dot(v2)
- Vector:cross(o)Source
Compute the cross product of two vectors
Parameters
- o Vector The second vector to compute the cross product of.
Returns
- Vector The cross product of
self
ando
.
Usage
v1:cross(v2)
- Vector:length()Source
Get the length (also referred to as magnitude) of this vector.
Returns
- number The length of this vector.
- Vector:normalize()Source
Divide this vector by its length, producing with the same direction, but of length 1.
Returns
- Vector The normalised vector
Usage
v:normalize()
- Vector:round([tolerance])Source
Construct a vector with each dimension rounded to the nearest value.
Parameters
- tolerance? number The tolerance that we should round to, defaulting to 1. For instance, a tolerance of 0.5 will round to the nearest 0.5.
Returns
- Vector The rounded vector.
- Vector:tostring()Source
Convert this vector into a string, for pretty printing.
Returns
- string This vector's string representation.
Usage
v:tostring()
tostring(v)