-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.go
162 lines (140 loc) · 3.35 KB
/
grid.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
type Grid struct {
data []*Node
width int
height int
PathStart *Node
PathEnd *Node
IsSolved bool
}
func NewGrid(width, height int) *Grid {
g := &Grid{
data: make([]*Node, width*height),
width: width,
height: height,
}
// set x and y for each node in grid
for x := 0; x < g.Width(); x++ {
for y := 0; y < g.Height(); y++ {
g.Put(&Node{x: x, y: y})
}
}
// add connections for each node in grid
for _, n := range g.Nodes() {
node := g.Get(n.X(), n.Y())
if !g.IsTopEdge(node) {
node.AddNeighbour(g.North(node))
}
if !g.IsTopEdge(node) && !g.IsRightEdge(node) {
node.AddNeighbour(g.NorthEast(node))
}
if !g.IsRightEdge(node) {
node.AddNeighbour(g.East(node))
}
if !g.IsBottomEdge(node) && !g.IsRightEdge(node) {
node.AddNeighbour(g.SouthEast(node))
}
if !g.IsBottomEdge(node) {
node.AddNeighbour(g.South(node))
}
if !g.IsBottomEdge(node) && !g.IsLeftEdge(node) {
node.AddNeighbour(g.SouthWest(node))
}
if !g.IsLeftEdge(node) {
node.AddNeighbour(g.West(node))
}
if !g.IsTopEdge(node) && !g.IsLeftEdge(node) {
node.AddNeighbour(g.NorthWest(node))
}
}
return g
}
func (g *Grid) Width() int {
return g.width
}
func (g *Grid) Height() int {
return g.height
}
func (g *Grid) Nodes() []*Node { return g.data }
func (g *Grid) Put(node *Node) {
g.data[node.X()*g.Height()+node.Y()] = node
}
func (g *Grid) Get(x, y int) *Node {
return g.data[x*g.Height()+y]
}
func (g *Grid) IsTopLeftCorner(node *Node) bool {
return node.X() == 0 && node.Y() == 0
}
func (g *Grid) IsTopRightCorner(node *Node) bool {
return node.X() == g.Width()-1 && node.Y() == 0
}
func (g *Grid) IsBottomLeftCorner(node *Node) bool {
return node.X() == 0 && node.Y() == g.Height()-1
}
func (g *Grid) IsBottomRightCorner(node *Node) bool {
return node.X() == g.Width()-1 && node.Y() == g.Height()-1
}
func (g *Grid) IsTopEdge(node *Node) bool {
return node.Y() == 0
}
func (g *Grid) IsBottomEdge(node *Node) bool {
return node.Y() == g.Height()-1
}
func (g *Grid) IsLeftEdge(node *Node) bool {
return node.X() == 0
}
func (g *Grid) IsRightEdge(node *Node) bool {
return node.X() == g.Width()-1
}
func (g *Grid) IsWithinGrid(x, y int) bool {
return x >= 0 && x < g.width && y >= 0 && y < g.height
}
func (g *Grid) North(node *Node) *Node {
return g.Get(node.X(), node.Y()-1)
}
func (g *Grid) NorthEast(node *Node) *Node {
return g.Get(node.X()+1, node.Y()-1)
}
func (g *Grid) East(node *Node) *Node {
return g.Get(node.X()+1, node.Y())
}
func (g *Grid) SouthEast(node *Node) *Node {
return g.Get(node.X()+1, node.Y()+1)
}
func (g *Grid) South(node *Node) *Node {
return g.Get(node.X(), node.Y()+1)
}
func (g *Grid) SouthWest(node *Node) *Node {
return g.Get(node.X()-1, node.Y()+1)
}
func (g *Grid) West(node *Node) *Node {
return g.Get(node.X()-1, node.Y())
}
func (g *Grid) NorthWest(node *Node) *Node {
return g.Get(node.X()-1, node.Y()-1)
}
type Node struct {
x int
y int
neighbours []*Node
Parent *Node
IsVisited bool
IsObstacle bool
GlobalGoal float32
LocalGoal float32
}
func (n *Node) X() int {
return n.x
}
func (n *Node) Y() int {
return n.y
}
func (n *Node) Neighbours() []*Node {
return n.neighbours
}
func (n *Node) AddNeighbour(node *Node) {
n.neighbours = append(n.neighbours, node)
}
func (n *Node) Eq(node Node) bool {
return n.x == node.x && n.y == node.y
}