-
Notifications
You must be signed in to change notification settings - Fork 1
/
dirs.go
96 lines (84 loc) · 1.32 KB
/
dirs.go
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package grid
import "image"
type dir uint8
const (
North dir = iota
East
South
West
NorthWest
NorthEast
SouthEast
SouthWest
Up = North
Right = East
Down = South
Left = West
UpLeft = NorthWest
UpRight = NorthEast
DownRight = SouthEast
DownLeft = SouthWest
)
var coords = []image.Point{
{X: 0, Y: -1}, // N
{X: 1, Y: 0}, // E
{X: 0, Y: 1}, // S
{X: -1, Y: 0}, // W
{X: -1, Y: -1}, // NW
{X: 1, Y: -1}, // NE
{X: 1, Y: 1}, // SE
{X: -1, Y: 1}, // SW
}
var (
DirectionsCardinal = []dir{
North,
East,
South,
West,
}
DirectionsDiagonal = []dir{
NorthWest,
NorthEast,
SouthEast,
SouthWest,
}
DirectionsALL = []dir{
NorthWest,
North,
NorthEast,
East,
SouthEast,
South,
SouthWest,
West,
}
)
// Points returns displacements for given directions, in requested order.
func Points(dirs ...dir) (rv []image.Point) {
rv = make([]image.Point, len(dirs))
for i, d := range dirs {
rv[i] = coords[d]
}
return rv
}
// Invert returns opposite direction.
func (d dir) Invert() (rv dir) {
switch d {
case North:
return South
case East:
return West
case West:
return East
case NorthEast:
return SouthWest
case SouthWest:
return NorthEast
case NorthWest:
return SouthEast
case SouthEast:
return NorthWest
case South:
}
return North
}