module Math.Geometry.GridInternal
(
Grid(..),
BoundedGrid(..),
TriTriGrid,
triTriGrid,
ParaTriGrid,
paraTriGrid,
RectSquareGrid,
rectSquareGrid,
TorSquareGrid,
torSquareGrid,
HexHexGrid,
hexHexGrid,
ParaHexGrid,
paraHexGrid
) where
import Data.Eq.Unicode ((≡), (≠))
import Data.Function (on)
import Data.List (groupBy, nub, nubBy, sortBy)
import Data.Ord (comparing)
import Data.Ord.Unicode ((≤), (≥))
class Eq x ⇒ Grid g s x | g → s, g → x where
indices ∷ g → [x]
distance ∷ g → x → x → Int
minDistance ∷ g → [x] → x → Int
minDistance g xs x = minimum . map (distance g x) $ xs
size ∷ g → s
neighbours ∷ g → x → [x]
neighbours g x = filter (\a → distance g x a ≡ 1 ) $ indices g
numNeighbours ∷ g → x → Int
numNeighbours g = length . neighbours g
contains ∷ g → x → Bool
contains g x = x `elem` indices g
viewpoint ∷ g → x → [(x, Int)]
viewpoint g p = map f (indices g)
where f x = (x, distance g p x)
tileCount ∷ g → Int
tileCount = length . indices
empty ∷ g → Bool
empty g = tileCount g ≡ 0
nonEmpty ∷ g → Bool
nonEmpty = not . empty
edges ∷ g → [(x,x)]
edges g = nubBy sameEdge $ concatMap (`adjacentEdges` g) $ indices g
isAdjacent ∷ Grid g s x ⇒ g → x → x → Bool
isAdjacent g a b = distance g a b ≡ 1
adjacentTilesToward ∷ g → x → x → [x]
adjacentTilesToward g a b
| a ≡ b = []
| otherwise = filter f $ neighbours g a
where f x = distance g x b ≡ distance g a b 1
minimalPaths ∷ g → x → x → [[x]]
minimalPaths g a b | a ≡ b = [[a]]
| distance g a b ≡ 1 = [[a,b]]
| otherwise = map (a:) xs
where xs = concatMap (\x → minimalPaths g x b) ys
ys = adjacentTilesToward g a b
sameEdge ∷ Eq t ⇒ (t, t) → (t, t) → Bool
sameEdge (a,b) (c,d) = (a,b) ≡ (c,d) || (a,b) ≡ (d,c)
adjacentEdges ∷ Grid g s t ⇒ t → g → [(t, t)]
adjacentEdges i g = map (\j → (i,j)) $ neighbours g i
class Grid g s x ⇒ BoundedGrid g s x where
boundary ∷ g → [x]
isBoundary ∷ g → x → Bool
isBoundary g x = x `elem` boundary g
centre ∷ g → [x]
centre g = map fst . head . reverse . groupBy ((==) `on` snd) .
sortBy (comparing snd) $ xds
where xds = map (\y -> (y, minDistance g bs y)) $ indices g
bs = boundary g
isCentre ∷ g → x → Bool
isCentre g x = x `elem` centre g
triZ ∷ Int → Int → Int
triZ x y | even y = x y
| otherwise = x y + 1
triDistance ∷ Grid g s (Int, Int) ⇒ g → (Int, Int) → (Int, Int) → Int
triDistance g (x1, y1) (x2, y2) =
if g `contains` (x1, y1) && g `contains` (x2, y2)
then maximum [abs (x2x1), abs (y2y1), abs(z2z1)]
else undefined
where z1 = triZ x1 y1
z2 = triZ x2 y2
triNeighbours ∷ Grid g s (Int, Int) ⇒ g → (Int, Int) → [(Int, Int)]
triNeighbours g (x,y) = filter (g `contains`) xs
where xs | even y = [(x1,y+1), (x+1,y+1), (x+1,y1)]
| otherwise = [(x1,y1), (x1,y+1), (x+1,y1)]
data TriTriGrid = TriTriGrid Int [(Int, Int)] deriving Eq
instance Show TriTriGrid where
show (TriTriGrid s _) = "triTriGrid " ++ show s
instance Grid TriTriGrid Int (Int, Int) where
indices (TriTriGrid _ xs) = xs
neighbours = triNeighbours
distance = triDistance
contains (TriTriGrid s _) (x, y) = inTriGrid (x,y) s
size (TriTriGrid s _) = s
inTriGrid ∷ (Int, Int) → Int → Bool
inTriGrid (x, y) s = x ≥ 0 && y ≥ 0 && even (x+y) && abs z ≤ 2*s2
where z = triZ x y
instance BoundedGrid TriTriGrid Int (Int, Int) where
boundary g = west ++ east ++ south
where s = size g
west = [(0,k) | k ← [0,2..2*s2]]
east = [(k,2*s2k) | k ← [2,4..2*s2]]
south = [(k,0) | k ← [2*s4,2*s6..2]]
centre g = case s `mod` 3 of
0 → trefoilWithTop (k1,k+1) where k = (2*s) `div` 3
1 → [(k,k)] where k = (2*(s1)) `div` 3
2 → [(k+1,k+1)] where k = (2*(s2)) `div` 3
_ → error "This will never happen."
where s = size g
trefoilWithTop ∷ (Int, Int) → [(Int,Int)]
trefoilWithTop (i,j) = [(i,j), (i+2, j2), (i,j2)]
triTriGrid ∷ Int → TriTriGrid
triTriGrid s =
TriTriGrid s [(xx,yy) | xx ← [0..2*(s1)],
yy ← [0..2*(s1)],
(xx,yy) `inTriGrid` s]
data ParaTriGrid = ParaTriGrid (Int, Int) [(Int, Int)] deriving Eq
instance Show ParaTriGrid where
show (ParaTriGrid (r,c) _) = "paraTriGrid " ++ show r ++ " " ++ show c
instance Grid ParaTriGrid (Int, Int) (Int, Int) where
indices (ParaTriGrid _ xs) = xs
neighbours = triNeighbours
distance = triDistance
size (ParaTriGrid s _) = s
instance BoundedGrid ParaTriGrid (Int, Int) (Int, Int) where
boundary g = west ++ north ++ east ++ south
where (r,c) = size g
west = [(0,k) | k ← [0,2..2*r2], c>0]
north = [(k,2*r1) | k ← [1,3..2*c1], r>0]
east = [(2*c1,k) | k ← [2*r3,2*r5..1], c>0]
south = [(k,0) | k ← [2*c2,2*c4..2], r>0]
centre g = paraTriGridCentre . size $ g
paraTriGridCentre ∷ (Int, Int) → [(Int, Int)]
paraTriGridCentre (r,c)
| odd r && odd c = [(c1,r1), (c,r)]
| even r && even c && r == c = bowtie (c1,r1)
| even r && even c && r > c
= bowtie (c1,r3) ++ bowtie (c1,r1) ++ bowtie (c1,r+1)
| even r && even c && r < c
= bowtie (c3,r1) ++ bowtie (c1,r1) ++ bowtie (c+1,r1)
| otherwise = [(c1,r), (c,r1)]
bowtie :: (Int,Int) -> [(Int,Int)]
bowtie (i,j) = [(i,j), (i+1,j+1)]
paraTriGrid ∷ Int → Int → ParaTriGrid
paraTriGrid r c =
ParaTriGrid (r,c) [(x,y) | x ← [0..2*c1], y ← [0..2*r1], even (x+y)]
data RectSquareGrid = RectSquareGrid (Int, Int) [(Int, Int)] deriving Eq
instance Show RectSquareGrid where
show (RectSquareGrid (r,c) _) =
"rectSquareGrid " ++ show r ++ " " ++ show c
instance Grid RectSquareGrid (Int, Int) (Int, Int) where
indices (RectSquareGrid _ xs) = xs
neighbours g (x, y) =
filter (g `contains`) [(x1,y), (x,y+1), (x+1,y), (x,y1)]
distance g (x1, y1) (x2, y2) =
if g `contains` (x1, y1) && g `contains` (x2, y2)
then abs (x2x1) + abs (y2y1)
else undefined
size (RectSquareGrid s _) = s
adjacentTilesToward g a@(x1, y1) (x2, y2) =
filter (\i → g `contains` i && i ≠ a) $ nub [(x1,y1+dy),(x1+dx,y1)]
where dx = signum (x2x1)
dy = signum (y2y1)
instance BoundedGrid RectSquareGrid (Int, Int) (Int, Int) where
boundary g = cartesianIndices . size $ g
centre g = cartesianCentre . size $ g
cartesianIndices
∷ (Enum r, Enum c, Num r, Num c, Ord r, Ord c) ⇒
(r, c) → [(c, r)]
cartesianIndices (r, c) = west ++ north ++ east ++ south
where west = [(0,k) | k ← [0,1..r1], c>0]
north = [(k,r1) | k ← [1,2..c1], r>0]
east = [(c1,k) | k ← [r2,r3..0], c>1]
south = [(k,0) | k ← [c2,c3..1], r>1]
cartesianCentre ∷ (Int, Int) → [(Int, Int)]
cartesianCentre (r,c) = [(i,j) | i ← midpoints c, j ← midpoints r]
midpoints ∷ Int → [Int]
midpoints k = if even k then [m1,m] else [m]
where m = floor (k'/2.0)
k' = fromIntegral k ∷ Double
rectSquareGrid ∷ Int → Int → RectSquareGrid
rectSquareGrid r c =
RectSquareGrid (r,c) [(x,y) | x ← [0..c1], y ← [0..r1]]
data TorSquareGrid = TorSquareGrid (Int, Int) [(Int, Int)] deriving Eq
instance Show TorSquareGrid where
show (TorSquareGrid (r,c) _) = "torSquareGrid " ++ show r ++ " " ++ show c
instance Grid TorSquareGrid (Int, Int) (Int, Int) where
indices (TorSquareGrid _ xs) = xs
neighbours (TorSquareGrid (r,c) _) (x,y) =
nub $ filter (\(xx,yy) → xx ≠ x || yy ≠ y)
[((x1) `mod` c,y), (x,(y+1) `mod` r), ((x+1) `mod` c,y),
(x,(y1) `mod` r)]
distance g@(TorSquareGrid (r,c) _) (x1, y1) (x2, y2) =
if g `contains` (x1, y1) && g `contains` (x2, y2)
then min adx (abs (cadx)) + min ady (abs (rady))
else undefined
where adx = abs (x2 x1)
ady = abs (y2 y1)
size (TorSquareGrid s _) = s
torSquareGrid ∷ Int → Int → TorSquareGrid
torSquareGrid r c = TorSquareGrid (r,c) [(x, y) | x ← [0..c1], y ← [0..r1]]
hexDistance ∷ Grid g s (Int, Int) ⇒ g → (Int, Int) → (Int, Int) → Int
hexDistance g (x1, y1) (x2, y2) =
if g `contains` (x1, y1) && g `contains` (x2, y2)
then maximum [abs (x2x1), abs (y2y1), abs(z2z1)]
else undefined
where z1 = x1 y1
z2 = x2 y2
data HexHexGrid = HexHexGrid Int [(Int, Int)] deriving Eq
instance Show HexHexGrid where show (HexHexGrid s _) = "hexHexGrid " ++ show s
instance Grid HexHexGrid Int (Int, Int) where
indices (HexHexGrid _ xs) = xs
neighbours g (x,y) = filter (g `contains`)
[(x1,y), (x1,y+1), (x,y+1), (x+1,y), (x+1,y1), (x,y1)]
distance = hexDistance
size (HexHexGrid s _) = s
instance BoundedGrid HexHexGrid Int (Int, Int) where
boundary g =
north ++ northeast ++ southeast ++ south ++ southwest ++ northwest
where s = size g
north = [(k,s1) | k ← [s+1,s+2..0]]
northeast = [(k,s1k) | k ← [1,2..s1]]
southeast = [(s1,k) | k ← [1,2..(s)+1]]
south = [(k,(s)+1) | k ← [s2,s3..0]]
southwest = [(k,(s)+1k) | k ← [1,2..(s)+1]]
northwest = [(s+1,k) | k ← [1,2..s2]]
centre _ = [(0,0)]
hexHexGrid ∷ Int → HexHexGrid
hexHexGrid r = HexHexGrid r [(x, y) | x ← [r+1..r1], y ← f x]
where f x = if x < 0 then [1rx .. r1] else [1r .. r1x]
data ParaHexGrid = ParaHexGrid (Int, Int) [(Int, Int)] deriving Eq
instance Show ParaHexGrid where
show (ParaHexGrid (r,c) _) = "paraHexGrid " ++ show r ++ " " ++ show c
instance Grid ParaHexGrid (Int, Int) (Int, Int) where
indices (ParaHexGrid _ xs) = xs
neighbours g (x,y) = filter (g `contains`)
[(x1,y), (x1,y+1), (x,y+1), (x+1,y), (x+1,y1), (x,y1)]
distance = hexDistance
size (ParaHexGrid s _) = s
instance BoundedGrid ParaHexGrid (Int, Int) (Int, Int) where
boundary g = cartesianIndices . size $ g
centre g = cartesianCentre . size $ g
paraHexGrid ∷ Int → Int → ParaHexGrid
paraHexGrid r c =
ParaHexGrid (r,c) [(x, y) | x ← [0..c1], y ← [0..r1]]