1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Undemanding\Difference; |
4
|
|
|
|
5
|
|
|
class ConnectedDifferences |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var array |
9
|
|
|
*/ |
10
|
|
|
private $bitmap; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var int |
14
|
|
|
*/ |
15
|
|
|
private $width; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
private $height; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $boundaries = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param Difference $difference |
29
|
|
|
*/ |
30
|
|
|
public function __construct(Difference $difference) |
31
|
|
|
{ |
32
|
|
|
$this->bitmap = $difference->getBitmap(); |
33
|
|
|
$this->width = $difference->getWidth(); |
34
|
|
|
$this->height = $difference->getHeight(); |
35
|
|
|
|
36
|
|
|
$this->boundaries = $this->findBoundaries(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Find separate boundaries. |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
private function findBoundaries() |
45
|
|
|
{ |
46
|
|
|
$pixels = []; |
47
|
|
|
|
48
|
|
|
for ($y = 0; $y < $this->height; $y++) { |
49
|
|
|
for ($x = 0; $x < $this->width; $x++) { |
50
|
|
|
if ($this->bitmap[$y][$x] > 0) { |
51
|
|
|
$pixels["{$x}:{$y}"] = [ |
52
|
|
|
"group" => null, |
53
|
|
|
"x" => $x, |
54
|
|
|
"y" => $y, |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$group = 1; |
61
|
|
|
$boundaries = []; |
62
|
|
|
|
63
|
|
|
foreach ($pixels as $i => $pixel) { |
64
|
|
|
$adjacent = $this->adjacent($pixel); |
65
|
|
|
|
66
|
|
|
if (!$pixel["group"]) { |
67
|
|
|
foreach ($adjacent as $key) { |
68
|
|
|
if (isset($pixels[$key]) and $pixels[$key]["group"]) { |
|
|
|
|
69
|
|
|
$pixel["group"] = $pixels[$key]["group"]; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (!$pixel["group"]) { |
75
|
|
|
$pixel["group"] = $group++; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
foreach ($adjacent as $key) { |
79
|
|
|
if (isset($pixels[$key])) { |
80
|
|
|
$pixels[$key]["group"] = $pixel["group"]; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$groups = array_values(array_unique(array_map(function($pixel) { |
86
|
|
|
return $pixel["group"]; |
87
|
|
|
}, $pixels))); |
88
|
|
|
|
89
|
|
|
foreach ($groups as $group) { |
90
|
|
|
$filtered = array_filter($pixels, function($pixel) use ($group) { |
91
|
|
|
return $pixel["group"] === $group; |
92
|
|
|
}); |
93
|
|
|
|
94
|
|
|
$ax = $this->width; |
95
|
|
|
$bx = 0; |
96
|
|
|
$ay = $this->height; |
97
|
|
|
$by = 0; |
98
|
|
|
|
99
|
|
|
foreach ($filtered as $pixel) { |
100
|
|
|
$x = $pixel["x"]; |
101
|
|
|
$y = $pixel["y"]; |
102
|
|
|
|
103
|
|
|
if ($x > $bx) { |
104
|
|
|
$bx = $x; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($x < $ax) { |
108
|
|
|
$ax = $x; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($y > $by) { |
112
|
|
|
$by = $y; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ($y < $ay) { |
116
|
|
|
$ay = $y; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
array_push($boundaries, [ |
121
|
|
|
"top" => $ay, |
122
|
|
|
"right" => $bx, |
123
|
|
|
"bottom" => $by, |
124
|
|
|
"left" => $ax, |
125
|
|
|
]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $boundaries; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return ConnectedDifferences |
133
|
|
|
*/ |
134
|
|
|
public function withJoinedBoundaries() |
135
|
|
|
{ |
136
|
|
|
$keep = []; |
137
|
|
|
|
138
|
|
|
foreach ($this->boundaries as $boundary) { |
139
|
|
|
foreach ($keep as $i => $kept) { |
140
|
|
|
if ($this->intersect($boundary, $kept)) { |
141
|
|
View Code Duplication |
if ($boundary["top"] < $kept["top"]) { |
|
|
|
|
142
|
|
|
$keep[$i]["top"] = $boundary["top"]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
View Code Duplication |
if ($boundary["right"] > $kept["right"]) { |
|
|
|
|
146
|
|
|
$keep[$i]["right"] = $boundary["right"]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
View Code Duplication |
if ($boundary["bottom"] > $kept["bottom"]) { |
|
|
|
|
150
|
|
|
$keep[$i]["bottom"] = $boundary["bottom"]; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
View Code Duplication |
if ($boundary["left"] < $kept["left"]) { |
|
|
|
|
154
|
|
|
$keep[$i]["left"] = $boundary["left"]; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
continue 2; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
array_push($keep, $boundary); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $this->cloneWith("boundaries", $keep); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string $property |
169
|
|
|
* @param mixed $value |
170
|
|
|
* |
171
|
|
|
* @return ConnectedDifferences |
172
|
|
|
*/ |
173
|
|
|
private function cloneWith($property, $value) |
174
|
|
|
{ |
175
|
|
|
$clone = clone $this; |
176
|
|
|
$clone->$property = $value; |
177
|
|
|
|
178
|
|
|
return $clone; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Labels for adjacent pixels. |
183
|
|
|
* |
184
|
|
|
* @param array $pixel |
185
|
|
|
* |
186
|
|
|
* @return array |
187
|
|
|
*/ |
188
|
|
|
private function adjacent($pixel) |
189
|
|
|
{ |
190
|
|
|
$adjacent = [ |
191
|
|
|
($pixel["x"] - 1) . ":" . ($pixel["y"] - 1), |
192
|
|
|
($pixel["x"] - 1) . ":" . $pixel["y"], |
193
|
|
|
($pixel["x"] - 1) . ":" . ($pixel["y"] + 1), |
194
|
|
|
$pixel["x"] . ":" . ($pixel["y"] - 1), |
195
|
|
|
$pixel["x"] . ":" . ($pixel["y"] + 1), |
196
|
|
|
($pixel["x"] + 1) . ":" . ($pixel["y"] - 1), |
197
|
|
|
($pixel["x"] + 1) . ":" . $pixel["y"], |
198
|
|
|
($pixel["x"] + 1) . ":" . ($pixel["y"] + 1), |
199
|
|
|
]; |
200
|
|
|
return $adjacent; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Tell if two boundaries overlap. |
205
|
|
|
* |
206
|
|
|
* @param array $p |
207
|
|
|
* @param array $q |
208
|
|
|
* |
209
|
|
|
* @return bool |
210
|
|
|
*/ |
211
|
|
|
private function intersect(array $p, array $q) |
212
|
|
|
{ |
213
|
|
|
return max($p["left"], $q["left"]) <= min($p["right"], $q["right"]) and max($p["top"], $q["top"]) <= min($p["bottom"], $q["bottom"]); |
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return array |
218
|
|
|
*/ |
219
|
|
|
public function boundaries() |
220
|
|
|
{ |
221
|
|
|
return $this->boundaries; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.