|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Klass Finn. |
|
5
|
|
|
* @author Niklas Dougherty |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Tips\Moduler\Kluster; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Klass Finn. |
|
14
|
|
|
*/ |
|
15
|
|
|
class Finn extends Generera { |
|
16
|
|
|
/** |
|
17
|
|
|
* Finn kluster. |
|
18
|
|
|
*/ |
|
19
|
1 |
|
public function finn_kluster(): void { |
|
20
|
|
|
/** |
|
21
|
|
|
* Måste nollställas. |
|
22
|
|
|
*/ |
|
23
|
1 |
|
$this->rektanglar = []; |
|
24
|
1 |
|
$this->klustrade_rader = 0; |
|
25
|
1 |
|
$this->area = 0; |
|
26
|
|
|
|
|
27
|
1 |
|
$koordinater = $this->hämta_koordinater(); |
|
28
|
1 |
|
$this->antal_rader = count($koordinater); |
|
29
|
|
|
|
|
30
|
1 |
|
$kluster = $this->generera_kluster($koordinater); |
|
31
|
|
|
|
|
32
|
1 |
|
foreach ($kluster as $grupp) { |
|
33
|
1 |
|
[$x_min, $y_min, $x_max, $y_max] = $this->minmaxkoordinater($grupp); |
|
34
|
|
|
|
|
35
|
1 |
|
if (count($grupp) >= $this->min_antal) { |
|
36
|
1 |
|
$this->area += ($x_max - $x_min) * ($y_max - $y_min); |
|
37
|
1 |
|
$this->rektanglar[] = [$x_min, $y_min, $x_max, $y_max]; |
|
38
|
1 |
|
$this->graf->sätt_rektangel($x_min, $y_min, $x_max, $y_max, $this->graf->blå); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
$this->rektanglar($koordinater); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Hitta min och max för koordinater. |
|
47
|
|
|
* @param mixed[] $grupp |
|
48
|
|
|
* @return int[] |
|
49
|
|
|
*/ |
|
50
|
1 |
|
private function minmaxkoordinater(array $grupp): array { |
|
51
|
1 |
|
[$x_min, $y_min, $x_max, $y_max] = [PHP_INT_MAX, PHP_INT_MAX, -1, -1]; |
|
52
|
|
|
|
|
53
|
1 |
|
foreach ($grupp as [$ukoord, $vkoord]) { |
|
54
|
1 |
|
if (is_int($ukoord) && is_int($vkoord)) { |
|
55
|
1 |
|
[$x_min, $y_min, $x_max, $y_max] = |
|
56
|
1 |
|
[min($ukoord, $x_min), min($vkoord, $y_min), max($ukoord, $x_max), max($vkoord, $y_max)]; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
return [ |
|
61
|
1 |
|
max(0, $x_min - 10), |
|
62
|
1 |
|
max(0, $y_min - 10), |
|
63
|
1 |
|
min($this->graf->bredd, $x_max + 10), |
|
64
|
1 |
|
min($this->graf->höjd, $y_max + 10) |
|
65
|
1 |
|
]; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|