Vector API

Functionvector.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(
  • self : table
  • object : table
)
Colon notation: self:add(
  • object : table
)
Returns table vector
Part of CC:Tweaked (source)
API vector
ExampleAdd two vectors
Creates two new vectors then adds them together.
Code
<nowiki>
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)
    </nowiki>
Output My new vector has the components: 2793 103 -3354
Functionvector.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(
  • self : table
  • object : table
)
Colon notation: self:cross(
  • object : table
)
Returns table cross product
Part of CC:Tweaked (source)
API vector
ExampleFind the cross product
Finds the cross product of two vectors
Code
<nowiki>
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)
    </nowiki>
Output The cross product of my two vectors is: 60.6 -46 5.2
ExampleBehind the scenes
An example of what the function looks like
Code
<nowiki>
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)
    </nowiki>
Output The cross product of my two vectors is: 60.6 -46 5.2
Functionvector.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(
  • self : table
  • scalar : number
)
Colon notation: self:div(
  • scalar : number
)
Returns table vector
Part of CC:Tweaked (source)
API vector
ExampleDivide a vector
Divide a vector by a number.
Code
<nowiki>
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)
    </nowiki>
Output My new vector has the components: 120 35.5 -1520
Functionvector.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(
  • self : table
  • object : table
)
Colon notation: self:dot(
  • object : table
)
Returns number dot product
Part of CC:Tweaked (source)
API vector
ExampleFind the dot product
Finds the dot product of two vectors
Code
<nowiki>
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)
    </nowiki>
Output The dot product of my two vectors is: -5.6
ExampleBehind the scenes
An example of what the function looks like
Code
<nowiki>
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)
    </nowiki>
Output The dot product of my two vectors is: -5.6
Functionvector.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(
  • self : table
)
Colon notation: self:length()
Returns number length
Part of CC:Tweaked (source)
API vector
ExampleFind a vectors length
Prints the length if a vector
Code
<nowiki>
local vectorA = vector.new(6, 7, -8)
print("The length of my vector is:", vectorA.length(vectorA))
    </nowiki>
Output The length of my vector is: 12.20655...
ExampleBehind the scenes
An example of what the function looks like
Code
<nowiki>
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)
    </nowiki>
Output The length of my vector is: 12.20655...
Functionvector.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(
  • self : table
  • scalar : number
)
Colon notation: self:mul(
  • scalar : number
)
Returns table vector
Part of CC:Tweaked (source)
API vector
ExampleMultiply a vector
Multiples a vector by a number.
Code
<nowiki>
local vectorA = vector.new(240, 71, -3040)
local vectorB = vector.mul(vectorA, 2)
print("My new vector has the components:", vectorB.x, vectorB.y, vectorB.z)
    </nowiki>
Output My new vector has the components: 480 142 -6080
Functionvector.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(
  • x? : number
  • y? : number
  • z? : number
)
Returns table vector
Part of CC:Tweaked (source)
API vector
ExampleCreating a vector
Creates a new vector and prints the components.
Code
<nowiki>
local myVector = vector.new(240, 71, -3040)
print("My vector has the components:", myVector.x, myVector.y, myVector.z)
    </nowiki>
Output My vector has the components: 240 71 -3040
ExampleLocating self
Acquiring the executing computer's own location and printing it on screen.
Code
<nowiki>
local location = vector.new(gps.locate())
print("Current location is:", location)
    </nowiki>
Output Current location is: 38 65 392
Functionvector.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(
  • self : table
)
Colon notation: self:normalize()
Returns table vector
Part of CC:Tweaked (source)
API vector
ExampleNormalize a vector
Converts myVector into a unit vector
Code
<nowiki>
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)
    </nowiki>
Output My unit vector has the components: 0.0786... 0.0232... -0.9966...
ExampleBehind the scenes
An example of what the function looks like
Code
<nowiki>
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)
    </nowiki>
Output My unit vector has the components: 0.0786... 0.0232... -0.9966...

Vector.round

Functionvector.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(
  • self : table
  • object : table
)
Colon notation: self:sub(
  • object : table
)
Returns table vector
Part of CC:Tweaked (source)
API vector
ExampleSubtracts two vectors
Creates two new vectors then adds them together.
Code
<nowiki>
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)
    </nowiki>
Output My new vector has the components: -2313 39 -2726
Functionvector.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(
  • self : table
)
Colon notation: self:tostring()
Returns string vector
Part of CC:Tweaked (source)
API vector
ExamplePrint a vector
Prints our vector
Code
<nowiki>
local myVector = vector.new(240, 71, -3040)
print("My new vector has the components:", myVector)
    </nowiki>
Output My new vector has the components: 240 71 -3040
Functionvector.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(
  • self : table
)
Colon notation: self:unm()
Returns table vector
Part of CC:Tweaked (source)
API vector
ExampleApply unary minus
Applies unary minus to a vector and prints the resulting vector
Code
<nowiki>
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)
    </nowiki>
Output My new vector has the components: -240 -71 3040
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.