-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirection.elm
80 lines (63 loc) · 1.51 KB
/
Direction.elm
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
module Direction
exposing
( Direction(..)
, getAngle
, getSideDirections
, rotateClockwise
)
import Maybe
import List.Extra
type Direction
= NORTH
| NORTHEAST
| EAST
| SOUTHEAST
| SOUTH
| SOUTHWEST
| WEST
| NORTHWEST
directionList : List Direction
directionList =
[ NORTH
, NORTHEAST
, EAST
, SOUTHEAST
, SOUTH
, SOUTHWEST
, WEST
, NORTHWEST
]
getIndex : Direction -> Maybe Int
getIndex dir =
List.Extra.elemIndex dir directionList
getAngle : Direction -> Int
getAngle dir =
case (getIndex dir) of
Just index ->
index * 45
Nothing ->
0
getSideDirections : Direction -> ( Direction, Direction )
getSideDirections dir =
let
index =
Maybe.withDefault 0 (getIndex dir)
numDirections =
List.length directionList
( leftSideIndex, rightSideIndex ) =
( (index + 2) % numDirections
, (index + 6) % numDirections
)
in
( Maybe.withDefault NORTH (List.Extra.getAt leftSideIndex directionList)
, Maybe.withDefault NORTH (List.Extra.getAt rightSideIndex directionList)
)
rotateClockwise : Direction -> Direction
rotateClockwise dir =
let
index =
Maybe.withDefault 0 (getIndex dir)
newDirection =
Maybe.withDefault NORTH (List.Extra.getAt ((index + 1) % (List.length directionList)) directionList)
in
newDirection