Skip to content

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.

Fields

Name Type Description
x integer x-coordinate
y integer y-coordinate

cell.new

Initialise a new forma.cell.

local x, y = 1, 5
local new_cell = cell.new(x, y)

Parameters:

Name Type Description
x integer first coordinate
y integer second coordinate

Returns:

  • forma.cell — new forma.cell

cell.clone

Perform a copy of a cell.

local old_cell = cell.new(1, 1)
local new_cell = old_cell:clone()

Parameters:

Name Type Description
icell forma.cell cell to be copied

Returns:

  • forma.cell — copy of icell

cell.__add

Add two cell positions.

local c1, c2 = cell.new(1, 1), cell.new(0, 0)
local c3 = c2 + c1
assert(c3 == c1)

Parameters:

Name Type Description
a forma.cell first cell
b forma.cell second cell

Returns:

  • forma.cell — c = a + b

cell.__sub

Subtract two cell positions.

local c1, c2 = cell.new(1, 1), cell.new(2, 2)
local c3 = c2 - c1
assert(c3 == c1)

Parameters:

Name Type Description
a forma.cell first cell
b forma.cell second cell

Returns:

  • forma.cell — c = a - b

cell.__eq

Test for equality of two cell vectors.

assert(cell.new(0, 1) == cell.new(0, 1))

Parameters:

Name Type Description
a forma.cell first cell
b forma.cell second cell

Returns:

  • boolean — a == b

cell.__tostring

Render a cell as a string.

print(cell.new(1, 1))

Parameters:

Name Type Description
icell forma.cell the cell being rendered as a string

Returns:

  • string — string of the form (icell.x, icell.y)

cell.manhattan

Manhattan distance between cells.

local distance = cell.manhattan(cell.new(1, 2), cell.new(3, 4))

Parameters:

Name Type Description
a forma.cell first cell
b forma.cell second cell

Returns:

  • integer — L1(a,b) = |a.x-b.x| + |a.y-b.y|

cell.chebyshev

Chebyshev distance between cells.

local distance = cell.chebyshev(cell.new(1, 2), cell.new(3, 4))

Parameters:

Name Type Description
a forma.cell first cell
b forma.cell second cell

Returns:

  • integer — L_inf(a,b) = max(|a.x-b.x|, |a.y-b.y|)

cell.euclidean

Euclidean distance between cells.

local distance = cell.euclidean(cell.new(1, 2), cell.new(3, 4))

Parameters:

Name Type Description
a forma.cell first cell
b forma.cell second cell

Returns:

  • number — L_2(a,b) = sqrt((a-b)^2)

cell.euclidean2

Squared Euclidean distance between cells. A little faster than cell.euclidean as it avoids the sqrt.

local distance = cell.euclidean2(cell.new(1, 2), cell.new(3, 4))

Parameters:

Name Type Description
a forma.cell first cell
b forma.cell second cell

Returns:

  • number — L_2(a,b)^2 = (a-b)^2