Module forma.cell
Integer point/vector class defining the position of a cell.
The cell class behaves much as a normal 2D vector class, with the restriction that its components must be integer-valued. A subset of normal vector operations are available, namely a vector addition and subtraction, along with a vector equality check.
Along with the cell class definition, a number of distance measures between cell positions are provided. Specifically, the Manhattan, Chebyshev and Euclidean distances.
Functions
new (x, y) | Initialise a new forma.cell. |
clone (icell) | Perform a copy of a cell. |
Metamethods
__add (a, b) | Add two cell positions |
__sub (a, b) | Subtract two cell positions |
__eq (a, b) | Test for equality of two cell vectors. |
__tostring (icell) | Render a cell as a string. |
Distance measures
manhattan (a, b) | Manhattan distance between cells. |
chebyshev (a, b) | Chebyshev distance between cells. |
euclidean (a, b) | Euclidean distance between cells. |
euclidean2 (a, b) | Squared Euclidean distance between cells. |
Functions
- new (x, y)
-
Initialise a new forma.cell.
Parameters:
- x first coordinate
- y second coordinate
Returns:
-
new forma.cell
Usage:
local x, y = 1, 5 local new_cell = cell.new(x,y)
- clone (icell)
-
Perform a copy of a cell.
Parameters:
- icell to be copied
Returns:
-
copy of
icell
Usage:
local old_cell = cell.new(1,1) local new_cell = old_cell:clone()
Metamethods
- __add (a, b)
-
Add two cell positions
Parameters:
- a first cell
- b second cell
Returns:
-
c = a + b
Usage:
local c1, c2 = cell.new(1,1), cell.new(0,0) local c3 = c2 + c1 assert(c3 == c1)
- __sub (a, b)
-
Subtract two cell positions
Parameters:
- a first cell
- b second cell
Returns:
-
c = a - b
Usage:
local c1, c2 = cell.new(1,1), cell.new(2,2) local c3 = c2 - c1 assert(c3 == c1)
- __eq (a, b)
-
Test for equality of two cell vectors.
assert(cell.new(0,1) == cell.new(0,1)
Parameters:
- a first cell
- b second cell
Returns:
-
a == b
- __tostring (icell)
-
Render a cell as a string.
Parameters:
- icell the forma.cell being rendered as a string
Returns:
-
string of the form
(icell.x, icell.y)
Usage:
print(cell.new(1,1))
Distance measures
- manhattan (a, b)
-
Manhattan distance between cells.
Parameters:
- a first cell
- b second cell
Returns:
-
L1(a,b) = |a.x-b.x| + |a.y-b.y|
Usage:
local distance = cell.manhattan(cell.new(1,2), cell.new(3,4))
- chebyshev (a, b)
-
Chebyshev distance between cells.
Parameters:
- a first cell
- b second cell
Returns:
-
L_inf(a,b) = max(|a.x-b.x|, |a.y-b.y|)
Usage:
local distance = cell.chebyshev(cell.new(1,2), cell.new(3,4))
- euclidean (a, b)
-
Euclidean distance between cells.
Parameters:
- a first cell
- b second cell
Returns:
-
L_2(a,b) = sqrt((a-b)^2)
Usage:
local distance = cell.euclidean(cell.new(1,2), cell.new(3,4))
- euclidean2 (a, b)
-
Squared Euclidean distance between cells.
A little faster than cell.euclidean as it avoids the sqrt.
Parameters:
- a first cell
- b second cell
Returns:
-
L_2(a,b)^2 = (a-b)^2
Usage:
local distance = cell.euclidean2(cell.new(1,2), cell.new(3,4))