Module Vector

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

All methods handle vectors as immutable to avoid side effects. Hence they return a new vector rather than modifying the called one. Vectors support arithmetic operations like a + b and comparisions 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:new ([x=0[, y=0[, z=0]]]) Creates a new vector out of the components x, y, z.
Vector.x X component of the vector.
Vector.y Y component of the vector.
Vector.z Z component of the vector.
Vector:length () Returns the length of the vector.
Vector:dot (o) Returns the dot product of two vectors a and b.
Vector:cross (o) Returns the cross product of two vectors a and b.
Vector:normalized () Returns a normalized vector that points in the same direction as self.
Vector:copy () Returns a 1:1 copy of the vector.
Vector:__tostring () Returns a readable representation of the vector.


Class Vector

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.x
X component of the vector.
Vector.y
Y component of the vector.
Vector.z
Z component of the vector.
Vector:length ()
Returns the length of the vector.

Returns:

    number

Usage:

    local a = Vector(1, 2, 3)
    print(a:length())
    
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:cross (o)
Returns the cross product of two vectors a and b.

Parameters:

  • o vector

Returns:

    vector

Usage:

    a:cross(b)
Vector:normalized ()
Returns a normalized vector that points in the same direction as self.

Returns:

    vector

Usage:

    local a = Vector(1, 2, 3)
    local b = a:normalized()
    assert(b:length() == 1)
    
Vector:copy ()
Returns a 1:1 copy of the vector.

Returns:

    vector

Usage:

    local a = Vector(1, 2, 3)
    local b = a:copy()
    local assert(a == b)
    
Vector:__tostring ()
Returns a readable representation of the vector. Usually used for debug purposes.

Returns:

    string

Usage:

    print(tostring(Vector(1, 2, 3)))
generated by LDoc 1.4.3 Last updated 2019-07-03 15:00:59