Kraxorax / elm-matrix-a / Neighbours

Module for getting neighbouring fields of some (x, y). Works with a few different matrix topologies. Topology specifies how neighbours are found across matrix edges.


type MatrixTopology
    = Plane
    | Torus
    | StripHorizontal
    | StripVertical

Possible topologies

If we mark (0, 0) spot with X in 4x4 Matrix, N marks
neighbours on given topology.

                        Strip           Strip
Plane:      Torus:      horizontal:     vertical:

X N - -     X N - N     X N - N         X N - -
N N - -     N N - N     N N - N         N N - -
- - - -     - - - -     - - - -         - - - -
- - - -     N N - N     - - - -         N N - -

neighbours : MatrixTopology -> Basics.Int -> Basics.Int -> Matrix a -> Array a

Given a topology and (x, y) returns array of neighbours from given matrix.

myMatrix = generate 4 4 (\x y -> (String.fromInt x) ++ (String.fromInt y))
--  "00"    "10"    "20"    "30"
--  "01"    "11"    "21"    "31"
--  "02"    "12"    "22"    "32"
--  "03"    "13"    "23"    "33"

neighbours Plane 0 0 myMatix
-- Array.fromList ["01","11","10"]

neighbours Torus 0 0 myMatix
-- Array.fromList ["33","03","13","31","01","11","10","30"]

neighbours stripHorizontal 0 0 myMatrix
-- Array.fromList ["31","01","11","10","30"]

neighbours StripVertical 0 0 myMatix
-- Array.fromList ["03","13","01","11","10"]