@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | // we add "$nodes" dynamic nodes |
| 19 | 19 | $positions = []; |
| 20 | 20 | foreach (range(0, $nodes) as $value) { |
| 21 | - $positions[] = new Point(rand(0, $max), rand(0, $max)); |
|
| 21 | + $positions[] = new Point(rand(0, $max), rand(0, $max)); |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | // add random links |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | |
| 33 | 33 | // draw the result |
| 34 | 34 | try { |
| 35 | - drawPaths($max, $positions, $from, $to, $shortestPath, 'image.png'); |
|
| 35 | + drawPaths($max, $positions, $from, $to, $shortestPath, 'image.png'); |
|
| 36 | 36 | } catch (Exception $e) { |
| 37 | - echo $e->getMessage(); |
|
| 37 | + echo $e->getMessage(); |
|
| 38 | 38 | } |
@@ -5,114 +5,114 @@ |
||
| 5 | 5 | |
| 6 | 6 | function findLink(int $minDistance, array &$positions): void |
| 7 | 7 | { |
| 8 | - foreach ($positions as $point) { |
|
| 9 | - findLinkBetween($minDistance, $point, $positions); |
|
| 10 | - } |
|
| 8 | + foreach ($positions as $point) { |
|
| 9 | + findLinkBetween($minDistance, $point, $positions); |
|
| 10 | + } |
|
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | function findLinkBetween(int $minDistance, Point &$point1, array &$positions): void |
| 14 | 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 | - } |
|
| 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 | 29 | } |
| 30 | 30 | |
| 31 | 31 | function findFromTo(array $positions): array |
| 32 | 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]; |
|
| 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 | 49 | } |
| 50 | 50 | |
| 51 | 51 | function drawPaths(int $max, array $positions, Point $from, Point $to, array $shortestPath, string $filename): void |
| 52 | 52 | { |
| 53 | - // open background |
|
| 54 | - $image = imagecreatetruecolor($max, $max); |
|
| 55 | - if (! $image) { |
|
| 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); |
|
| 53 | + // open background |
|
| 54 | + $image = imagecreatetruecolor($max, $max); |
|
| 55 | + if (! $image) { |
|
| 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 | 93 | } |
| 94 | 94 | |
| 95 | 95 | function drawLine($image, Point $point1, Point $point2, $color, int $thick = 1): void |
| 96 | 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); |
|
| 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 | 118 | } |
@@ -52,7 +52,7 @@ |
||
| 52 | 52 | { |
| 53 | 53 | // open background |
| 54 | 54 | $image = imagecreatetruecolor($max, $max); |
| 55 | - if (! $image) { |
|
| 55 | + if (!$image) { |
|
| 56 | 56 | throw new Exception('Can not create image'); |
| 57 | 57 | } |
| 58 | 58 | $color = imagecolorallocate($image, 255, 255, 255); |