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.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 normalized vector that points 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() local 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 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)