vector.normalize

From ComputerCraft
Jump to: navigation, search

vector.normalize
Function
Syntax
vector.normalize()
Colon notation: self:normalize()

Returns table vector
API vector
Source CC:Tweaked (source)

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.

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