You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.8 KiB

module Datatypes.Cell (Cell(..), rotateClockwise, rotateCounterclockwise ) where
type Cell = (Int, Int)
rotateClockwise :: Cell -> Cell -> Cell
rotateClockwise pivot cell = let cubeCell = toCube cell
cubePivot = toCube pivot
cubeZero = cubeCell `cubeDiff` cubePivot
rotatedZero = rotateCubeClockwise cubeZero
rotatedCube = rotatedZero `cubeSum` cubePivot
resultCell = fromCube rotatedCube
in resultCell
rotateCounterclockwise :: Cell -> Cell -> Cell
rotateCounterclockwise pivot cell = let cubeCell = toCube cell
cubePivot = toCube pivot
cubeZero = cubeCell `cubeDiff` cubePivot
rotatedZero = rotateCubeCounterclockwise cubeZero
rotatedCube = rotatedZero `cubeSum` cubePivot
resultCell = fromCube rotatedCube
in resultCell
-- PRIVATE
type CubeCell = (Int, Int, Int)
toCube :: Cell -> CubeCell
toCube (x, y) = (xx, yy, zz) where
xx = x - (y - (y `mod` 2)) `div` 2
zz = y
yy = -(xx + zz)
rotateCubeClockwise :: CubeCell -> CubeCell
rotateCubeClockwise (xx, yy, zz) = (-zz, -xx, -yy)
rotateCubeCounterclockwise :: CubeCell -> CubeCell
rotateCubeCounterclockwise (xx, yy, zz) = (-yy, -zz, -xx)
fromCube :: CubeCell -> Cell
fromCube (xx, yy, zz) = (x, y) where
x = xx + (zz - (zz `mod` 2)) `div` 2
y = zz
cubeDiff :: CubeCell -> CubeCell -> CubeCell
cubeDiff (x1, y1, z1) (x2, y2, z2) = (x1 - x2, y1 - y2, z1 - z2)
cubeSum :: CubeCell -> CubeCell -> CubeCell
cubeSum (x1, y1, z1) (x2, y2, z2) = (x1 + x2, y1 + y2, z1 + z2)