Difference between revisions of "Vector (API)"

From ComputerCraft Wiki
Jump to: navigation, search
(Someone really borked up the HTML table. Fixed.)
(Changing float to number)
 
(20 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{NeedsWork|Needs function pages, some parameters missing types ''[[User:Pokepal101|Pokepal101]] 02:42, 29 December 2012 (MSK)''}}
+
The vector API provides methods to create and manipulate vectors.
  
The vector API provides methods to create and manipulate vectors.  An introduction to vectors can be found on [[Wikipedia:Euclidean_vector|Wikipedia]].
+
An introduction to vectors can be found on [[Wikipedia:Euclidean_vector|Wikipedia]].
  
 
<table style="width: 100%; border: solid 1px black; margin: 2px; border-spacing: 0px;">
 
<table style="width: 100%; border: solid 1px black; margin: 2px; border-spacing: 0px;">
Line 11: Line 11:
 
<tr><td style="width: 350px; background: #E0E0E0; padding: .4em; font-weight:bold;">Method Name</td><td style="background: #E0E0E0; padding: .4em; font-weight:bold;">Description</td></tr>
 
<tr><td style="width: 350px; background: #E0E0E0; padding: .4em; font-weight:bold;">Method Name</td><td style="background: #E0E0E0; padding: .4em; font-weight:bold;">Description</td></tr>
  
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[vector.new]](x, y, z)</td>
+
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[vector.new]]({{type|number}} x, {{type|number}} y, {{type|number}} z)</td>
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Creates a vector.</td></tr>
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Creates a vector.</td></tr>
  
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[vectorA:add]]([[vector]] vectorB)</td>
+
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[vectorA:add]]([[Vector (API)|vector]] vectorB)</td>
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Adds vectorB to vectorA and returns the resulted vector. Can also be used by writing vectorA + vectorB.</td></tr>
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Adds ''vectorB'' to ''vectorA'' and returns the resulting vector. Can also be used by writing vectorA + vectorB.</td></tr>
  
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[vectorA:sub]]([[vector]] vectorB)</td>
+
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[vectorA:sub]]([[Vector (API)|vector]] vectorB)</td>
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Subtracts vectorB from vectorA and returns the resulted vector. Can also be used by writing vectorA - vectorB.</td></tr>
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Subtracts ''vectorB'' from ''vectorA'' and returns the resulting vector. Can also be used by writing vectorA - vectorB.</td></tr>
  
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[vectorA:mul]](n)</td>
+
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[vectorA:mul]]({{type|number}} n)</td>
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Scalar multiplies vectorA with n and returns the resulted vector. Can also be used by writing vectorA * n.</td></tr>
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Scalar multiplies ''vectorA'' with ''n'' and returns the resulting vector. Can also be used by writing vectorA * n.</td></tr>
  
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[vectorA:dot]]([[vector]] vectorB)</td>
+
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[vectorA:dot]]([[Vector (API)|vector]] vectorB)</td>
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Returns the dot product of vectorA and vectorB.</td></tr>
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Returns the dot product of ''vectorA'' and ''vectorB''.</td></tr>
  
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[vectorA:cross]]([[vector]] vectorB)</td>
+
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[vectorA:cross]]([[Vector (API)|vector]] vectorB)</td>
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Returns the vector which resulted in the cross product of vectorA and vectorB.</td></tr>
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Returns the vector which resulted in the cross product of ''vectorA'' and ''vectorB''.</td></tr>
  
 
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[vectorA:length]]()</td>
 
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[vectorA:length]]()</td>
Line 43: Line 43:
  
 
[[Category:APIs]]
 
[[Category:APIs]]
 +
 +
 +
Vectors act much like tables, storing the coordinate data with labels "x", "y" and "z".  However, it can be worthwhile to use vectors instead of tables in certain situations.
 +
 +
 +
Among other things, vectors can be useful to aid in creating navigation algorithms using GPS coordinates.
 +
 +
To transform GPS quickly into vector format, simply use:
 +
local a = vector.new(gps.locate())
 +
 +
Then, getting the displacements between a turtle position and a desired location becomes:
 +
local b = vector.new(20, 85, 40)
 +
local c = a - b
 +
--displacement in the x direction:  c.x
 +
--displacement in the y direction:  c.y
 +
--displacement in the z direction:  c.z
 +
 +
By making use of vectors, code can be cleaner and easier to follow.

Latest revision as of 13:26, 18 July 2013

The vector API provides methods to create and manipulate vectors.

An introduction to vectors can be found on Wikipedia.

Grid disk.png   Vector (API)

Method NameDescription
vector.new(number x, number y, number z) Creates a vector.
vectorA:add(vector vectorB) Adds vectorB to vectorA and returns the resulting vector. Can also be used by writing vectorA + vectorB.
vectorA:sub(vector vectorB) Subtracts vectorB from vectorA and returns the resulting vector. Can also be used by writing vectorA - vectorB.
vectorA:mul(number n) Scalar multiplies vectorA with n and returns the resulting vector. Can also be used by writing vectorA * n.
vectorA:dot(vector vectorB) Returns the dot product of vectorA and vectorB.
vectorA:cross(vector vectorB) Returns the vector which resulted in the cross product of vectorA and vectorB.
vectorA:length() Returns the vector's length.
vectorA:normalize() Normalizes the vector and returns the result as a new vector.
vectorA:round() Rounds the vector coordinates to the nearest integers and returns the result as a new vector.
vectorA:tostring() Returns a string representation of the vector in the form of "x,y,z".


Vectors act much like tables, storing the coordinate data with labels "x", "y" and "z". However, it can be worthwhile to use vectors instead of tables in certain situations.


Among other things, vectors can be useful to aid in creating navigation algorithms using GPS coordinates.

To transform GPS quickly into vector format, simply use:

local a = vector.new(gps.locate())

Then, getting the displacements between a turtle position and a desired location becomes:

local b = vector.new(20, 85, 40)
local c = a - b
--displacement in the x direction:  c.x
--displacement in the y direction:  c.y
--displacement in the z direction:  c.z

By making use of vectors, code can be cleaner and easier to follow.