Vector API

From ComputerCraft
Jump to: navigation, search
FunctionFunctionvector.add
Given two vectors, self and object, this function will add the object to self to create a new vector. This can also be written as self + object. If either argument was not created with vector.new, this function will not work as designed.
Syntax vector.add()
Colon notation: self:add()
Returns table vector
Part of CC:Tweaked (source)
API vector
ExampleExampleAdd two vectors
Creates two new vectors then adds them together.
Code
local vectorA = vector.new(240, 71, -3040)
local vectorB = vector.new(2553, 32, -314)
local vectorC = vector.add(vectorA, vectorB)
print("My new vector has the components:", vectorC.x, vectorC.y, vectorC.z)
Output My new vector has the components: 2793 103 -3354


FunctionFunctionvector.cross
Given two vectors, self and object, this function will output their cross product. If the arguments do not follow Computercraft's vector style (a table with the contents: x, y, and z) the function could output undesired results.
Syntax vector.cross()
Colon notation: self:cross()
Returns table cross product
Part of CC:Tweaked (source)
API vector
ExampleExampleFind the cross product
Finds the cross product of two vectors
Code
local vectorA = vector.new(6, 7, -8)
local vectorB = vector.new(2, 3.2, 5)
local crossProduct = vector.cross(vectorA, vectorB)
print("The cross product of my two vectors is:", crossProduct)
Output The cross product of my two vectors is: 60.6 -46 5.2
ExampleExampleBehind the scenes
An example of what the function looks like
Code
local vectorA = vector.new(6, 7, -8)
local vectorB = vector.new(2, 3.2, 5)
local crossProduct = vector.new(
    (vectorA.y * vectorB.z - vectorA.z * vectorB.y),
    (vectorA.z * vectorB.x - vectorA.x * vectorB.z),
    (vectorA.x * vectorB.y - vectorA.y * vectorB.x))
print("The cross product of my two vectors is:", crossProduct)
Output The cross product of my two vectors is: 60.6 -46 5.2


FunctionFunctionvector.div
Given a vector, self, and a scalar this function will divide each component in self by the scalar to create a new vector. This can also be written as self / scalar. If self was not created with vector.new, this function will not work as designed.
Syntax vector.div()
Colon notation: self:div()
Returns table vector
Part of CC:Tweaked (source)
API vector
ExampleExampleDivide a vector
Divide a vector by a number.
Code
local vectorA = vector.new(240, 71, -3040)
local vectorB = vector.div(vectorA, 2)
print("My new vector has the components:", vectorB.x, vectorB.y, vectorB.z)
Output My new vector has the components: 120 35.5 -1520


FunctionFunctionvector.dot
Given two vectors, self and object, this function will multiply and add the components of object and self and output their dot product. If either argument does not follow Computercraft's vector style (a table with the contents: x, y, and z) the function will error.
Syntax vector.dot()
Colon notation: self:dot()
Returns number dot product
Part of CC:Tweaked (source)
API vector
ExampleExampleFind the dot product
Finds the dot product of two vectors
Code
local vectorA = vector.new(6, 7, -8)
local vectorB = vector.new(2, 3.2, 5)
local dotProduct = vector.dot(vectorA, vectorB) --12+22.4+(-40)
print("The dot product of my two vectors is:", dotProduct)
Output The dot product of my two vectors is: -5.6
ExampleExampleBehind the scenes
An example of what the function looks like
Code
local vectorA = vector.new(6, 7, -8)
local vectorB = vector.new(2, 3.2, 5)
local dotProduct = (vectorA.x*vectorB.x)+(vectorA.y*vectorB.y)+(vectorA.z*vectorB.z)
print("The dot product of my two vectors is:", dotProduct)
Output The dot product of my two vectors is: -5.6


FunctionFunctionvector.length
Given a vector, self, this function will square and add the components of self to define the length of the vector. If self does not follow Computercraft's vector style (a table with the contents: x, y, and z) the function will error.
Syntax vector.length()
Colon notation: self:length()
Returns number length
Part of CC:Tweaked (source)
API vector
ExampleExampleFind a vectors length
Prints the length if a vector
Code
local vectorA = vector.new(6, 7, -8)
print("The length of my vector is:", vectorA.length(vectorA))
Output The length of my vector is: 12.20655...
ExampleExampleBehind the scenes
An example of what the function looks like
Code
local vectorA = vector.new(6, 7, -8)
local length = (vectorA.x*vectorA.x)+(vectorA.y*vectorA.y)+(vectorA.z*vectorA.z)
print("The length of my vector is:", length)
Output The length of my vector is: 12.20655...


FunctionFunctionvector.mul
Given a vector, self, and a scalar this function will multiply each component in self by the scalar to create a new vector. This can also be written as self * scalar. If self was not created with vector.new, this function will not work as designed.
Syntax vector.mul()
Colon notation: self:mul()
Returns table vector
Part of CC:Tweaked (source)
API vector
ExampleExampleMultiply a vector
Multiples a vector by a number.
Code
local vectorA = vector.new(240, 71, -3040)
local vectorB = vector.mul(vectorBA, 2)
print("My new vector has the components:", vectorB.x, vectorB.y, vectorB.z)
Output My new vector has the components: 480 142 -6080


FunctionFunctionvector.new
Given three coordinates, x, y, and z, this function will create a new vector when called. If an argument is not a number and cannot be converted to a number the corresponding coordinate will default to zero.
Syntax vector.new()
Returns table vector
Part of CC:Tweaked (source)
API vector
ExampleExampleCreating a vector
Creates a new vector and prints the components.
Code
local myVector = vector.new(240, 71, -3040)
print("My vector has the components:", myVector.x, myVector.y, myVector.z)
Output My vector has the components: 240 71 -3040
ExampleExampleLocating self
Acquiring the executing computer's own location and printing it on screen.
Code
local location = vector.new(gps.locate())
print("Current location is:", location)
Output Current location is: 38 65 392


FunctionFunctionvector.normalize
Given a vector, self, this function will convert self to a unit vector, utilizing a process known as normalization. If self does not follow Computercraft's vector style (a table with the contents: x, y, and z) the function may return unexpected results.
Syntax vector.normalize()
Colon notation: self:normalize()
Returns table vector
Part of CC:Tweaked (source)
API vector
ExampleExampleNormalize a vector
Converts myVector into a unit vector
Code
local myVector = vector.new(240, 71, -3040)
local unitVector = vector.normalize(myVector)
print("My unit vector has the components:", unitVector.x, unitVector.y, unitVector.z)
Output My unit vector has the components: 0.0786... 0.0232... -0.9966...
ExampleExampleBehind the scenes
An example of what the function looks like
Code
local myVector = vector.new(240, 71, -3040)
local length = myVector:length()
my unitVector = vector.new(
    unitVector.x / length,
    unitVector.y / length,
    unitVector.z / length)
print("My unit vector has the components:", unitVector.x, unitVector.y, unitVector.z)
Output My unit vector has the components: 0.0786... 0.0232... -0.9966...

Vector.round

FunctionFunctionvector.sub
Given two vectors, self and object, this function will subtract object from self to create a new vector. This can also be written as self - object. If either argument was not created with vector.new, this function will not work as designed.
Syntax vector.sub()
Colon notation: self:sub()
Returns table vector
Part of CC:Tweaked (source)
API vector
ExampleExampleSubtracts two vectors
Creates two new vectors then adds them together.
Code
local vectorA = vector.new(240, 71, -3040)
local vectorB = vector.new(2553, 32, -314)
local vectorC = vector.sub(vectorA, vectorB)
print("My new vector has the components:", vectorC.x, vectorC.y, vectorC.z)
Output My new vector has the components: -2313 39 -2726


FunctionFunctionvector.tostring
Given a vector, self, this function will create a new string out of the vector's components. This can also be written as print(self). If self does not follow Computercraft's vector style (a table with the contents: x, y, and z) the function will error.
Syntax vector.tostring()
Colon notation: self:tostring()
Returns string vector
Part of CC:Tweaked (source)
API vector
ExampleExamplePrint a vector
Prints our vector
Code
local myVector = vector.new(240, 71, -3040)
print("My new vector has the components:", myVector)
Output My new vector has the components: 240 71 -3040


FunctionFunctionvector.unm
Given a vector, self, this function will apply the unary operation, unary minus, to each component in self to create a new vector. This can also be written as -self. If self was not created with vector.new, this function will not work as designed.
Syntax vector.unm()
Colon notation: self:unm()
Returns table vector
Part of CC:Tweaked (source)
API vector
ExampleExampleApply unary minus
Applies unary minus to a vector and prints the resulting vector
Code
local vectorA = vector.new(240, 71, -3040)
local vectorB = vector.unm(vectorA)
print("My new vector has the components:", vectorB.x, vectorB.y, vectorB.z)
Output My new vector has the components: -240 -71 3040