|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Bmichotte\Dijkstra\Point; |
|
4
|
|
|
use Bmichotte\Dijkstra\Dijkstra; |
|
5
|
|
|
|
|
6
|
|
|
function findLink(int $minDistance, array &$positions): void |
|
7
|
|
|
{ |
|
8
|
|
|
foreach ($positions as $point) { |
|
9
|
|
|
findLinkBetween($minDistance, $point, $positions); |
|
10
|
|
|
} |
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
function findLinkBetween(int $minDistance, Point &$point1, array &$positions): void |
|
14
|
|
|
{ |
|
15
|
|
|
foreach ($positions as $point2) { |
|
16
|
|
|
if ($point1->equals($point2)) { |
|
17
|
|
|
continue; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
$distance = Dijkstra::distance($point1, $point2); |
|
21
|
|
|
if ($distance < $minDistance) { |
|
22
|
|
|
$point1->addPoint($point2); |
|
23
|
|
|
} |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
if (0 === count($point1->points)) { |
|
27
|
|
|
findLinkBetween($minDistance * 2, $point1, $positions); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
function findFromTo(array $positions): array |
|
32
|
|
|
{ |
|
33
|
|
|
$from = null; |
|
34
|
|
|
$to = null; |
|
35
|
|
|
foreach ($positions as $point) { |
|
36
|
|
|
$from = $from ?: $point; |
|
37
|
|
|
$to = $to ?: $point; |
|
38
|
|
|
|
|
39
|
|
|
if ($point->x < $from->x && $point->y < $from->y) { |
|
40
|
|
|
$from = $point; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if ($point->x > $to->x && $point->y > $to->y) { |
|
44
|
|
|
$to = $point; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return [$from, $to]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
function drawPaths(int $max, array $positions, Point $from, Point $to, array $shortestPath, string $filename): void |
|
52
|
|
|
{ |
|
53
|
|
|
// open background |
|
54
|
|
|
$image = imagecreatetruecolor($max, $max); |
|
55
|
|
|
if ($image === false) { |
|
56
|
|
|
throw new Exception('Can not create image'); |
|
57
|
|
|
} |
|
58
|
|
|
$color = imagecolorallocate($image, 255, 255, 255); |
|
59
|
|
|
imagefill($image, 0, 0, $color); |
|
60
|
|
|
|
|
61
|
|
|
// first run, draw lines |
|
62
|
|
|
$color = imagecolorallocate($image, 32, 230, 200); |
|
63
|
|
|
foreach ($positions as $point) { |
|
64
|
|
|
foreach ($point->points as $link) { |
|
65
|
|
|
drawLine($image, $point, $link, $color); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// then, draw the points |
|
70
|
|
|
$color = imagecolorallocate($image, 32, 230, 36); |
|
71
|
|
|
foreach ($positions as $point) { |
|
72
|
|
|
imagefilledellipse($image, $point->x, $point->y, 10, 10, $color); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// draw the shortest path |
|
76
|
|
|
$color = imagecolorallocate($image, 255, 0, 255); |
|
77
|
|
|
$shortestPathLength = count($shortestPath); |
|
78
|
|
|
for ($i = 0; $i < $shortestPathLength; $i++) { |
|
79
|
|
|
$p = $shortestPath[$i]; |
|
80
|
|
|
if (isset($shortestPath[$i + 1])) { |
|
81
|
|
|
$d = $shortestPath[$i + 1]; |
|
82
|
|
|
drawLine($image, $p, $d, $color, 3); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// and finally, draw the from and to points |
|
87
|
|
|
$color = imagecolorallocate($image, 255, 0, 255); |
|
88
|
|
|
imagefilledellipse($image, $from->x, $from->y, 10, 10, $color); |
|
89
|
|
|
imagefilledellipse($image, $to->x, $to->y, 10, 10, $color); |
|
90
|
|
|
|
|
91
|
|
|
imagepng($image, $filename); |
|
92
|
|
|
imagedestroy($image); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
function drawLine($image, Point $point1, Point $point2, $color, int $thick = 1): void |
|
96
|
|
|
{ |
|
97
|
|
|
if (null === $point1 || null === $point2) { |
|
98
|
|
|
return; |
|
99
|
|
|
} |
|
100
|
|
|
if (null === $point1->x || null === $point1->y) { |
|
101
|
|
|
return; |
|
102
|
|
|
} |
|
103
|
|
|
if (null === $point2->x || null === $point2->y) { |
|
104
|
|
|
return; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if ($point1->x === $point2->x) { |
|
108
|
|
|
$from = $point1->y < $point2->y ? $point1 : $point2; |
|
109
|
|
|
$to = $point1->y > $point2->y ? $point1 : $point2; |
|
110
|
|
|
} else { |
|
111
|
|
|
$from = $point1->x < $point2->x ? $point1 : $point2; |
|
112
|
|
|
$to = $point1->x > $point2->x ? $point1 : $point2; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
imagesetthickness($image, $thick); |
|
116
|
|
|
|
|
117
|
|
|
imageline($image, $from->x, $from->y, $to->x, $to->y, $color); |
|
118
|
|
|
} |
|
119
|
|
|
|