/
Orientation.hs
30 lines (24 loc) · 874 Bytes
/
Orientation.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module Orientation where
data Direction = DUp | DRight | DDown | DLeft deriving (Show, Eq)
data Orientation = Reg { dir :: Direction } | Mir { dir :: Direction } deriving (Show, Eq)
isMir :: Orientation -> Bool
isMir (Reg _) = False
isMir (Mir _) = True
-- Returns the next clockwise facing direction (90 degree increments)
turnCW :: Direction -> Direction
turnCW DLeft = DUp
turnCW DUp = DRight
turnCW DRight = DDown
turnCW DDown = DLeft
-- Turn counter clockwise (== clockwise 3 times)
turnCCW :: Direction -> Direction
turnCCW = turnCW . turnCW . turnCW
-- Turn around (== clockwise twice)
turnOpp :: Direction -> Direction
turnOpp = turnCW . turnCW
-- Adjust indices by moving in a direction
moveDir :: Direction -> (Int, Int) -> (Int, Int)
moveDir DLeft (x,y) = (x-1, y)
moveDir DUp (x,y) = (x, y-1)
moveDir DRight (x,y) = (x+1, y)
moveDir DDown (x,y) = (x, y+1)