|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Barryvanveen\CCA; |
|
6
|
|
|
|
|
7
|
|
|
use Barryvanveen\CCA\Config\NeighborhoodOptions; |
|
8
|
|
|
|
|
9
|
|
|
class Neighborhood |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var Config */ |
|
12
|
|
|
protected $config; |
|
13
|
|
|
|
|
14
|
|
|
/** @var Coordinate */ |
|
15
|
|
|
protected $coordinate; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Neighborhood constructor. |
|
19
|
|
|
* |
|
20
|
|
|
* @param Config $config |
|
21
|
|
|
* @param Coordinate $coordinate |
|
22
|
|
|
*/ |
|
23
|
21 |
|
public function __construct(Config $config, Coordinate $coordinate) |
|
24
|
|
|
{ |
|
25
|
21 |
|
$this->config = $config; |
|
26
|
|
|
|
|
27
|
21 |
|
$this->coordinate = $coordinate; |
|
28
|
21 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return Coordinate[] |
|
32
|
|
|
*/ |
|
33
|
21 |
|
public function getNeighbors(): array |
|
34
|
|
|
{ |
|
35
|
21 |
|
$neighbors = []; |
|
36
|
|
|
|
|
37
|
21 |
|
$offsets = range(-1 * $this->config->neighborhoodSize(), $this->config->neighborhoodSize()); |
|
38
|
|
|
|
|
39
|
21 |
|
foreach ($offsets as $rowOffset) { |
|
40
|
21 |
|
foreach ($offsets as $columnOffset) { |
|
41
|
21 |
|
if (!$this->isValidNeighbor($rowOffset, $columnOffset)) { |
|
42
|
21 |
|
continue; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
21 |
|
$neighbors[] = $this->createCoordinateFromOffsets($this->coordinate, $rowOffset, $columnOffset); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
21 |
|
return $neighbors; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
21 |
|
protected function createCoordinateFromOffsets(Coordinate $current, int $rowOffset, int $columnOffset): Coordinate |
|
53
|
|
|
{ |
|
54
|
21 |
|
return new Coordinate( |
|
55
|
21 |
|
$this->wrapRow($current->row(), $rowOffset), |
|
56
|
21 |
|
$this->wrapColumn($current->column(), $columnOffset), |
|
57
|
21 |
|
$this->config->columns() |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Determine if the offsets result in a valid neighbor. |
|
63
|
|
|
* |
|
64
|
|
|
* @param int $rowOffset |
|
65
|
|
|
* @param int $columnOffset |
|
66
|
|
|
* |
|
67
|
|
|
* @return bool |
|
68
|
|
|
*/ |
|
69
|
21 |
|
protected function isValidNeighbor(int $rowOffset, int $columnOffset): bool |
|
70
|
|
|
{ |
|
71
|
21 |
|
if (abs($rowOffset) + abs($columnOffset) === 0) { |
|
|
|
|
|
|
72
|
21 |
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
21 |
|
if ($this->config->neighborhoodType() === NeighborhoodOptions::NEIGHBORHOOD_TYPE_MOORE) { |
|
76
|
12 |
|
return true; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
9 |
|
if ((abs($rowOffset) + abs($columnOffset)) > $this->config->neighborhoodSize()) { |
|
80
|
9 |
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
9 |
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
21 |
|
protected function wrapRow(int $row, int $rowOffset): int |
|
87
|
|
|
{ |
|
88
|
21 |
|
$row = $row + $rowOffset; |
|
89
|
|
|
|
|
90
|
21 |
|
if ($row < 0) { |
|
91
|
21 |
|
return $row + $this->config->rows(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
21 |
|
return $row % $this->config->rows(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
21 |
|
protected function wrapColumn(int $column, int $columnOffset): int |
|
98
|
|
|
{ |
|
99
|
21 |
|
$column = $column + $columnOffset; |
|
100
|
|
|
|
|
101
|
21 |
|
if ($column < 0) { |
|
102
|
21 |
|
return $column + $this->config->columns(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
21 |
|
return $column % $this->config->columns(); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|