Module Vector

A 3-dimensional vector implementation that can be used for convenience.

Arithmetic methods return new vectors rather than modifying their operands. Vector components can still be assigned directly, as shown below.

Vectors support arithmetic operations like a + b and comparisons like a == b.

Usage:

    local v = Vector(1, 2, 3)
    v.x = 7
    print(v.x)
    print(v:length())
    print(tostring(v))
    print(tostring(2 * v + v:normalized()))

Class Vector

Vector.x X component of the vector.
Vector.y Y component of the vector.
Vector.z Z component of the vector.
Vector:copy () Returns a 1:1 copy of the vector.
Vector:cross (o) Returns the cross product of two vectors a and b.
Vector:dot (o) Returns the dot product of two vectors a and b.
Vector:length () Returns the length of the vector.
Vector:new ([x=0[, y=0[, z=0]]]) Creates a new vector out of the components x, y, z.
Vector:normalized () Returns a unit vector pointing in the same direction as self.


Class Vector

Vector.x
X component of the vector.
Vector.y
Y component of the vector.
Vector.z
Z component of the vector.
Vector:copy ()
Returns a 1:1 copy of the vector.

Returns:

    vector

Usage:

    local a = Vector(1, 2, 3)
    local b = a:copy()
    assert(a == b)
Vector:cross (o)
Returns the cross product of two vectors a and b.

Parameters:

  • o vector

Returns:

    vector

Usage:

    a:cross(b)
Vector:dot (o)

Returns the dot product of two vectors a and b.

Formally the dot product of two vectors

a = (a1, a2, a3), b = (b1, b2, b3)

is defined as

a1 * b1 + a2 * b2 + a3 * b3

Parameters:

  • o vector

Returns:

    number

Usage:

    a:dot(b)
Vector:length ()
Returns the length of the vector.

Returns:

    number

Usage:

    local a = Vector(1, 2, 3)
    print(a:length())
Vector:new ([x=0[, y=0[, z=0]]])
Creates a new vector out of the components x, y, z. You may use the alias Vector(x, y, z) instead.

Parameters:

  • x number X component of the vector. (default 0)
  • y number Y component of the vector. (default 0)
  • z number Z component of the vector. (default 0)

Returns:

    vector

Usage:

    local a = Vector()
    local b = Vector(1, 2)
    local c = Vector(1, 2, 3)
Vector:normalized ()
Returns a unit vector pointing in the same direction as self. Do not normalize a zero-length vector (division by zero).

Returns:

    vector

Usage:

    local a = Vector(1, 2, 3)
    local b = a:normalized()
    assert(b:length() == 1)
generated by LDoc 1.4.3 Last updated 2026-11-20 13:32:29