1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Letournel\PathFinder\Core; |
4
|
|
|
|
5
|
|
|
class NodeGrid |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
private |
9
|
|
|
$nodes, |
|
|
|
|
10
|
|
|
$height, |
11
|
|
|
$width; |
12
|
|
|
|
13
|
|
|
/* Matrix Axes |
|
|
|
|
14
|
|
|
* .--------------> j (width) coord y |
15
|
|
|
* | 1,1 1,2 1,3 |
16
|
|
|
* | 2,1 2,2 2,3 |
17
|
|
|
* | 3,1 ... |
18
|
|
|
* | |
19
|
|
|
* i (height) coor x |
20
|
|
|
*/ |
21
|
|
|
public function __construct(array $matrix) |
22
|
|
|
{ |
23
|
|
|
$this->height = $this->computeHeight($matrix); |
24
|
|
|
$this->width = $this->computeWidth($matrix); |
25
|
|
|
$this->nodes = $this->buildNodes($matrix); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
private function computeHeight(array $matrix) |
29
|
|
|
{ |
30
|
|
|
return count($matrix); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
private function computeWidth(array $matrix) |
34
|
|
|
{ |
35
|
|
|
$width = 0; |
36
|
|
|
foreach($matrix as $line) |
37
|
|
|
{ |
38
|
|
|
$width = max(count($line), $width); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $width; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
private function buildNodes(array $matrix) |
45
|
|
|
{ |
46
|
|
|
$nodes = array(); |
47
|
|
|
|
48
|
|
|
for($i = 0; $i < $this->height; $i++) |
49
|
|
|
{ |
50
|
|
|
$nodes[$i] = array(); |
51
|
|
|
for($j = 0; $j < $this->width; $j++) |
52
|
|
|
{ |
53
|
|
|
$walkable = isset($matrix[$i][$j]) ? $matrix[$i][$j] : false; |
54
|
|
|
$nodes[$i][$j] = new Node($i, $j, $walkable); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $nodes; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getNodes() |
62
|
|
|
{ |
63
|
|
|
return $this->nodes; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function buildWalkableNodesList() |
67
|
|
|
{ |
68
|
|
|
$list = array(); |
69
|
|
|
foreach($this->nodes as $line) |
70
|
|
|
{ |
71
|
|
|
foreach($line as $node) |
72
|
|
|
{ |
73
|
|
|
if($node->isWalkable()) |
74
|
|
|
{ |
75
|
|
|
$list[] = $node; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $list; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getNodeNumber($n) |
84
|
|
|
{ |
85
|
|
|
$x = floor($n / $this->width); |
86
|
|
|
$y = $n % $this->width; |
87
|
|
|
|
88
|
|
|
return $this->nodes[$x][$y]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getNodesNb() |
92
|
|
|
{ |
93
|
|
|
return $this->height * $this->width; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getWidth() |
97
|
|
|
{ |
98
|
|
|
return $this->width; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getHeight() |
102
|
|
|
{ |
103
|
|
|
return $this->height; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getWalkableNeighbors(Node $node) |
107
|
|
|
{ |
108
|
|
|
if(! $node->isWalkable()) |
109
|
|
|
{ |
110
|
|
|
return array(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$deltas = array( |
114
|
|
|
array(-1, -1), array(-1, +0), array(-1, +1), |
115
|
|
|
array(+0, -1), array(+0, +1), |
116
|
|
|
array(+1, -1), array(+1, +0), array(+1, +1), |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
$neighbors = array(); |
120
|
|
|
foreach($deltas as $delta) |
121
|
|
|
{ |
122
|
|
|
$x = $node->getX() + $delta[0]; |
123
|
|
|
$y = $node->getY() + $delta[1]; |
124
|
|
|
if($this->isWalkableAt($x, $y)) |
125
|
|
|
{ |
126
|
|
|
$neighbors[] = $this->getNodeAt($x, $y); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $neighbors; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
private function getNodeAt($x, $y) |
134
|
|
|
{ |
135
|
|
|
if(! array_key_exists($x, $this->nodes)) |
136
|
|
|
{ |
137
|
|
|
return null; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if(! array_key_exists($y, $this->nodes[$x])) |
141
|
|
|
{ |
142
|
|
|
return null; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $this->nodes[$x][$y]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
private function isWalkableAt($x, $y) |
149
|
|
|
{ |
150
|
|
|
$node = $this->getNodeAt($x, $y); |
151
|
|
|
if($node instanceof Node) |
152
|
|
|
{ |
153
|
|
|
return $node->isWalkable(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return false; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.