vector
A basic 3D vector type and some common vector operations. This may be useful
when working with coordinates in Minecraft's world (such as those from the
gps API).
An introduction to vectors can be found on Wikipedia.
Changes
- New in version 1.31
| new(x, y, z) | Construct a new Vector with the given coordinates. |
|---|
- new(x, y, z)Source
Construct a new
Vectorwith the given coordinates.Parameters
- x
numberThe X coordinate or direction of the vector. - y
numberThe Y coordinate or direction of the vector. - z
numberThe Z coordinate or direction of the vector.
Returns
VectorThe constructed vector.
- x
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
VectorThe second vector to add.
Returns
VectorThe resulting vector
Usage
v1:add(v2)
v1 + v2
- o
- Vector:sub(o)Source
Subtracts one vector from another.
Parameters
- o
VectorThe vector to subtract.
Returns
VectorThe resulting vector
Usage
v1:sub(v2)
v1 - v2
- o
- Vector:mul(factor)Source
Multiplies a vector by a scalar value.
Parameters
- factor
numberThe scalar value to multiply with.
Returns
VectorA vector with value(x * m, y * m, z * m).
Usage
vector.new(1, 2, 3):mul(3)
vector.new(1, 2, 3) * 3
- factor
- Vector:div(factor)Source
Divides a vector by a scalar value.
Parameters
- factor
numberThe scalar value to divide by.
Returns
VectorA vector with value(x / m, y / m, z / m).
Usage
vector.new(1, 2, 3):div(3)
vector.new(1, 2, 3) / 3
- factor
- Vector:unm()Source
Negate a vector
Returns
VectorThe negated vector.
Usage
-vector.new(1, 2, 3)
- Vector:dot(o)Source
Compute the dot product of two vectors
Parameters
- o
VectorThe second vector to compute the dot product of.
Returns
numberThe dot product ofselfando.
Usage
v1:dot(v2)
- o
- Vector:cross(o)Source
Compute the cross product of two vectors
Parameters
- o
VectorThe second vector to compute the cross product of.
Returns
VectorThe cross product ofselfando.
Usage
v1:cross(v2)
- o
- Vector:length()Source
Get the length (also referred to as magnitude) of this vector.
Returns
numberThe length of this vector.
- Vector:normalize()Source
Divide this vector by its length, producing with the same direction, but of length 1.
Returns
VectorThe normalised vector
Usage
v:normalize()
- Vector:round([tolerance])Source
Construct a vector with each dimension rounded to the nearest value.
Parameters
- tolerance?
numberThe tolerance that we should round to, defaulting to 1. For instance, a tolerance of 0.5 will round to the nearest 0.5.
Returns
VectorThe rounded vector.
- tolerance?
- Vector:tostring()Source
Convert this vector into a string, for pretty printing.
Returns
stringThis vector's string representation.
Usage
v:tostring()
tostring(v)
- Vector:equals(other)Source
Check for equality between two vectors.
Parameters
- other
VectorThe second vector to compare to.
Returns
booleanWhether or not the vectors are equal.
- other