1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Barryvanveen\CCA; |
4
|
|
|
|
5
|
|
|
class Neighborhood |
6
|
|
|
{ |
7
|
|
|
/** @var Config */ |
8
|
|
|
protected $config; |
9
|
|
|
|
10
|
|
|
/** @var Coordinate */ |
11
|
|
|
protected $coordinate; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Neighborhood constructor. |
15
|
|
|
* |
16
|
|
|
* @param Config $config |
17
|
|
|
* @param Coordinate $coordinate |
18
|
|
|
*/ |
19
|
21 |
|
public function __construct(Config $config, Coordinate $coordinate) |
20
|
|
|
{ |
21
|
21 |
|
$this->config = $config; |
22
|
|
|
|
23
|
21 |
|
$this->coordinate = $coordinate; |
24
|
21 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return Coordinate[] |
28
|
|
|
* |
29
|
|
|
* @throws \Barryvanveen\CCA\Exceptions\InvalidNeighborhoodTypeException |
30
|
|
|
*/ |
31
|
21 |
|
public function getNeighbors(): array |
32
|
|
|
{ |
33
|
21 |
|
$neighborhoodType = $this->config->neighborhoodType(); |
34
|
|
|
|
35
|
21 |
|
$method = 'get'.ucfirst($neighborhoodType).'Neighbors'; |
36
|
|
|
|
37
|
21 |
|
return $this->{$method}($this->coordinate); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Retrieve an array of Moore neighborhood neighbors for the given coordinate. |
42
|
|
|
* |
43
|
|
|
* @param Coordinate $coordinate |
44
|
|
|
* |
45
|
|
|
* @return Coordinate[] |
46
|
|
|
*/ |
47
|
12 |
|
protected function getMooreNeighbors(Coordinate $coordinate): array |
48
|
|
|
{ |
49
|
12 |
|
$neigbors = []; |
50
|
|
|
|
51
|
12 |
|
$size = abs($this->config->neighborhoodSize()); |
52
|
|
|
|
53
|
12 |
|
for ($rowOffset = -1 * $size; $rowOffset <= $size; $rowOffset++) { |
54
|
12 |
|
for ($columnOffset = -1 * $size; $columnOffset <= $size; $columnOffset++) { |
55
|
12 |
|
if ($rowOffset === 0 && $columnOffset === 0) { |
56
|
12 |
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
12 |
|
$neigbors[] = new Coordinate( |
60
|
12 |
|
$this->wrapRow($coordinate->row(), $rowOffset), |
|
|
|
|
61
|
12 |
|
$this->wrapColumn($coordinate->column(), $columnOffset), |
|
|
|
|
62
|
12 |
|
$this->config->columns() |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
12 |
|
return $neigbors; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Retrieve an array of Neumann neighborhood neighbors for the given coordinate. |
72
|
|
|
* |
73
|
|
|
* @param Coordinate $coordinate |
74
|
|
|
* |
75
|
|
|
* @return Coordinate[] |
76
|
|
|
*/ |
77
|
9 |
|
protected function getNeumannNeighbors(Coordinate $coordinate): array |
78
|
|
|
{ |
79
|
9 |
|
$neigbors = []; |
80
|
|
|
|
81
|
9 |
|
$size = abs($this->config->neighborhoodSize()); |
82
|
|
|
|
83
|
9 |
|
for ($rowOffset = -1 * $size; $rowOffset <= $size; $rowOffset++) { |
84
|
9 |
|
for ($columnOffset = -1 * $size; $columnOffset <= $size; $columnOffset++) { |
85
|
9 |
|
if ($rowOffset === 0 && $columnOffset === 0) { |
86
|
9 |
|
continue; |
87
|
|
|
} |
88
|
|
|
|
89
|
9 |
|
if ((abs($rowOffset) + abs($columnOffset)) > $this->config->neighborhoodSize()) { |
90
|
9 |
|
continue; |
91
|
|
|
} |
92
|
|
|
|
93
|
9 |
|
$neigbors[] = new Coordinate( |
94
|
9 |
|
$this->wrapRow($coordinate->row(), $rowOffset), |
|
|
|
|
95
|
9 |
|
$this->wrapColumn($coordinate->column(), $columnOffset), |
|
|
|
|
96
|
9 |
|
$this->config->columns() |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
9 |
|
return $neigbors; |
102
|
|
|
} |
103
|
|
|
|
104
|
21 |
|
protected function wrapRow(int $row, int $rowOffset): int |
105
|
|
|
{ |
106
|
21 |
|
$row = $row + $rowOffset; |
107
|
|
|
|
108
|
21 |
|
if ($row < 0) { |
109
|
21 |
|
return $row + $this->config->rows(); |
110
|
|
|
} |
111
|
|
|
|
112
|
21 |
|
return $row % $this->config->rows(); |
113
|
|
|
} |
114
|
|
|
|
115
|
21 |
|
protected function wrapColumn(int $column, int $columnOffset): int |
116
|
|
|
{ |
117
|
21 |
|
$column = $column + $columnOffset; |
118
|
|
|
|
119
|
21 |
|
if ($column < 0) { |
120
|
21 |
|
return $column + $this->config->columns(); |
121
|
|
|
} |
122
|
|
|
|
123
|
21 |
|
return $column % $this->config->columns(); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|