vector.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.

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.normalize
Function
Syntax
vector.normalize(
  • self : table
)
Colon notation: self:normalize()

Returns table vector
API vector
Source CC:Tweaked (source)
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.