@@ -18,7 +18,7 @@ |
||
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 |
@@ -68,12 +68,12 @@ discard block |
||
68 | 68 | $smallest = INF; |
69 | 69 | $nextParent = null; |
70 | 70 | foreach ($this->weights as $weight) { |
71 | - if ($weight['weight'] < $smallest && $weight['weight'] !== -1 && ! $weight['passed']) { |
|
71 | + if ($weight['weight'] < $smallest && $weight['weight'] !== -1 && !$weight['passed']) { |
|
72 | 72 | $smallest = $weight['weight']; |
73 | 73 | $nextParent = $weight['position']; |
74 | 74 | } |
75 | 75 | } |
76 | - if (! is_null($nextParent)) { |
|
76 | + if (!is_null($nextParent)) { |
|
77 | 77 | return $this->run($nextParent); |
78 | 78 | } |
79 | 79 | |
@@ -123,7 +123,7 @@ discard block |
||
123 | 123 | * PREDECESSOR(child-node) = parent-node |
124 | 124 | * ENDIF |
125 | 125 | */ |
126 | - if (! $this->weights[$child->ref]['passed'] |
|
126 | + if (!$this->weights[$child->ref]['passed'] |
|
127 | 127 | && ($this->weights[$parent->ref]['weight'] + self::distance($parent, $child) < $this->weights[$child->ref]['weight'] |
128 | 128 | || $this->weights[$child->ref]['weight'] === -1)) { |
129 | 129 | $this->weights[$child->ref]['weight'] = $this->weights[$parent->ref]['weight'] + self::distance($parent, $child); |
@@ -4,116 +4,116 @@ discard block |
||
4 | 4 | |
5 | 5 | class Dijkstra |
6 | 6 | { |
7 | - private $positions; |
|
8 | - private $from; |
|
9 | - private $to; |
|
10 | - private $weights; |
|
11 | - private $predecessors; |
|
12 | - |
|
13 | - public function __construct(array $positions, Point $from, Point $to) |
|
14 | - { |
|
15 | - $this->positions = $positions; |
|
16 | - $this->from = $from; |
|
17 | - $this->to = $to; |
|
18 | - } |
|
19 | - |
|
20 | - public function findShortestPath(): array |
|
21 | - { |
|
22 | - $this->weights = []; |
|
23 | - $this->predecessors = []; |
|
24 | - |
|
25 | - foreach ($this->positions as $position) { |
|
26 | - $weight = -1; |
|
27 | - $passed = false; |
|
28 | - |
|
29 | - if ($position->equals($this->from)) { |
|
30 | - $weight = 0; |
|
31 | - $passed = true; |
|
32 | - } |
|
33 | - |
|
34 | - // init weights array |
|
35 | - $this->weights[$position->ref] = [ |
|
36 | - 'position' => $position, |
|
37 | - 'weight' => $weight, |
|
38 | - 'passed' => $passed, |
|
39 | - ]; |
|
40 | - |
|
41 | - // init predecessors array |
|
42 | - $this->predecessors[$position->ref] = [ |
|
43 | - 'position' => $position, |
|
44 | - 'previous' => null, |
|
45 | - ]; |
|
46 | - } |
|
47 | - |
|
48 | - return $this->run($this->from)->getPath(); |
|
49 | - } |
|
50 | - |
|
51 | - protected function run(Point $parent): self |
|
52 | - { |
|
53 | - // we reached the final point ! |
|
54 | - if ($parent->equals($this->to)) { |
|
55 | - return $this; |
|
56 | - } |
|
57 | - |
|
58 | - // we can set this node has been passed by |
|
59 | - $this->weights[$parent->ref]['passed'] = true; |
|
60 | - |
|
61 | - // search for weight between $parent and its children |
|
62 | - foreach ($parent->points as $child) { |
|
63 | - $this->calculateWeight($parent, $child); |
|
64 | - } |
|
65 | - |
|
66 | - // search for the next parent (smallest weight) |
|
67 | - // we have to find the smallest weight for a node we didn't passed by atm |
|
68 | - $smallest = INF; |
|
69 | - $nextParent = null; |
|
70 | - foreach ($this->weights as $weight) { |
|
71 | - if ($weight['weight'] < $smallest && $weight['weight'] !== -1 && ! $weight['passed']) { |
|
72 | - $smallest = $weight['weight']; |
|
73 | - $nextParent = $weight['position']; |
|
74 | - } |
|
75 | - } |
|
76 | - if (! is_null($nextParent)) { |
|
77 | - return $this->run($nextParent); |
|
78 | - } |
|
79 | - |
|
80 | - return $this; |
|
81 | - } |
|
82 | - |
|
83 | - protected function getPath(): array |
|
84 | - { |
|
85 | - $path = [$this->to]; |
|
86 | - |
|
87 | - $point = $this->to; |
|
88 | - while (true) { |
|
89 | - foreach ($this->predecessors as $predecessor) { |
|
90 | - if ($predecessor['position']->equals($point)) { |
|
91 | - $point = $predecessor['previous']; |
|
92 | - $path[] = $point; |
|
93 | - |
|
94 | - break; |
|
95 | - } |
|
96 | - } |
|
97 | - |
|
98 | - // $point is null -> path impossible |
|
99 | - if (is_null($point)) { |
|
100 | - unset($path[count($path) - 1]); |
|
101 | - break; |
|
102 | - } |
|
103 | - |
|
104 | - if ($point->equals($this->from)) { |
|
105 | - break; |
|
106 | - } |
|
107 | - } |
|
108 | - |
|
109 | - $path = array_reverse($path); |
|
110 | - |
|
111 | - return $path; |
|
112 | - } |
|
113 | - |
|
114 | - protected function calculateWeight(Point $parent, Point $child): void |
|
115 | - { |
|
116 | - /* |
|
7 | + private $positions; |
|
8 | + private $from; |
|
9 | + private $to; |
|
10 | + private $weights; |
|
11 | + private $predecessors; |
|
12 | + |
|
13 | + public function __construct(array $positions, Point $from, Point $to) |
|
14 | + { |
|
15 | + $this->positions = $positions; |
|
16 | + $this->from = $from; |
|
17 | + $this->to = $to; |
|
18 | + } |
|
19 | + |
|
20 | + public function findShortestPath(): array |
|
21 | + { |
|
22 | + $this->weights = []; |
|
23 | + $this->predecessors = []; |
|
24 | + |
|
25 | + foreach ($this->positions as $position) { |
|
26 | + $weight = -1; |
|
27 | + $passed = false; |
|
28 | + |
|
29 | + if ($position->equals($this->from)) { |
|
30 | + $weight = 0; |
|
31 | + $passed = true; |
|
32 | + } |
|
33 | + |
|
34 | + // init weights array |
|
35 | + $this->weights[$position->ref] = [ |
|
36 | + 'position' => $position, |
|
37 | + 'weight' => $weight, |
|
38 | + 'passed' => $passed, |
|
39 | + ]; |
|
40 | + |
|
41 | + // init predecessors array |
|
42 | + $this->predecessors[$position->ref] = [ |
|
43 | + 'position' => $position, |
|
44 | + 'previous' => null, |
|
45 | + ]; |
|
46 | + } |
|
47 | + |
|
48 | + return $this->run($this->from)->getPath(); |
|
49 | + } |
|
50 | + |
|
51 | + protected function run(Point $parent): self |
|
52 | + { |
|
53 | + // we reached the final point ! |
|
54 | + if ($parent->equals($this->to)) { |
|
55 | + return $this; |
|
56 | + } |
|
57 | + |
|
58 | + // we can set this node has been passed by |
|
59 | + $this->weights[$parent->ref]['passed'] = true; |
|
60 | + |
|
61 | + // search for weight between $parent and its children |
|
62 | + foreach ($parent->points as $child) { |
|
63 | + $this->calculateWeight($parent, $child); |
|
64 | + } |
|
65 | + |
|
66 | + // search for the next parent (smallest weight) |
|
67 | + // we have to find the smallest weight for a node we didn't passed by atm |
|
68 | + $smallest = INF; |
|
69 | + $nextParent = null; |
|
70 | + foreach ($this->weights as $weight) { |
|
71 | + if ($weight['weight'] < $smallest && $weight['weight'] !== -1 && ! $weight['passed']) { |
|
72 | + $smallest = $weight['weight']; |
|
73 | + $nextParent = $weight['position']; |
|
74 | + } |
|
75 | + } |
|
76 | + if (! is_null($nextParent)) { |
|
77 | + return $this->run($nextParent); |
|
78 | + } |
|
79 | + |
|
80 | + return $this; |
|
81 | + } |
|
82 | + |
|
83 | + protected function getPath(): array |
|
84 | + { |
|
85 | + $path = [$this->to]; |
|
86 | + |
|
87 | + $point = $this->to; |
|
88 | + while (true) { |
|
89 | + foreach ($this->predecessors as $predecessor) { |
|
90 | + if ($predecessor['position']->equals($point)) { |
|
91 | + $point = $predecessor['previous']; |
|
92 | + $path[] = $point; |
|
93 | + |
|
94 | + break; |
|
95 | + } |
|
96 | + } |
|
97 | + |
|
98 | + // $point is null -> path impossible |
|
99 | + if (is_null($point)) { |
|
100 | + unset($path[count($path) - 1]); |
|
101 | + break; |
|
102 | + } |
|
103 | + |
|
104 | + if ($point->equals($this->from)) { |
|
105 | + break; |
|
106 | + } |
|
107 | + } |
|
108 | + |
|
109 | + $path = array_reverse($path); |
|
110 | + |
|
111 | + return $path; |
|
112 | + } |
|
113 | + |
|
114 | + protected function calculateWeight(Point $parent, Point $child): void |
|
115 | + { |
|
116 | + /* |
|
117 | 117 | * Dijkstra algo says : |
118 | 118 | * |
119 | 119 | * IF (child-node is not traversed yet) AND |
@@ -123,17 +123,17 @@ discard block |
||
123 | 123 | * PREDECESSOR(child-node) = parent-node |
124 | 124 | * ENDIF |
125 | 125 | */ |
126 | - if (! $this->weights[$child->ref]['passed'] |
|
127 | - && ($this->weights[$parent->ref]['weight'] + self::distance($parent, $child) < $this->weights[$child->ref]['weight'] |
|
128 | - || $this->weights[$child->ref]['weight'] === -1)) { |
|
129 | - $this->weights[$child->ref]['weight'] = $this->weights[$parent->ref]['weight'] + self::distance($parent, $child); |
|
130 | - $this->predecessors[$child->ref]['previous'] = $parent; |
|
131 | - } |
|
132 | - } |
|
133 | - |
|
134 | - public static function distance(Point $p1, Point $p2): float |
|
135 | - { |
|
136 | - // distance = square root of ((x2 - x1)^2 + (y2 - y1)^2) |
|
137 | - return sqrt(bcpow($p2->x - $p1->x, 2) + bcpow($p2->y - $p1->y, 2)); |
|
138 | - } |
|
126 | + if (! $this->weights[$child->ref]['passed'] |
|
127 | + && ($this->weights[$parent->ref]['weight'] + self::distance($parent, $child) < $this->weights[$child->ref]['weight'] |
|
128 | + || $this->weights[$child->ref]['weight'] === -1)) { |
|
129 | + $this->weights[$child->ref]['weight'] = $this->weights[$parent->ref]['weight'] + self::distance($parent, $child); |
|
130 | + $this->predecessors[$child->ref]['previous'] = $parent; |
|
131 | + } |
|
132 | + } |
|
133 | + |
|
134 | + public static function distance(Point $p1, Point $p2): float |
|
135 | + { |
|
136 | + // distance = square root of ((x2 - x1)^2 + (y2 - y1)^2) |
|
137 | + return sqrt(bcpow($p2->x - $p1->x, 2) + bcpow($p2->y - $p1->y, 2)); |
|
138 | + } |
|
139 | 139 | } |
@@ -4,109 +4,109 @@ |
||
4 | 4 | |
5 | 5 | function findLink(int $minDistance, array &$positions): void |
6 | 6 | { |
7 | - foreach ($positions as $point) { |
|
8 | - findLinkBetween($minDistance, $point, $positions); |
|
9 | - } |
|
7 | + foreach ($positions as $point) { |
|
8 | + findLinkBetween($minDistance, $point, $positions); |
|
9 | + } |
|
10 | 10 | } |
11 | 11 | |
12 | 12 | function findLinkBetween(int $minDistance, Point &$point1, array &$positions): void |
13 | 13 | { |
14 | - foreach ($positions as $point2) { |
|
15 | - if ($point1->equals($point2)) { |
|
16 | - continue; |
|
17 | - } |
|
18 | - |
|
19 | - $distance = Dijkstra::distance($point1, $point2); |
|
20 | - if ($distance < $minDistance) { |
|
21 | - $point1->addPoint($point2); |
|
22 | - } |
|
23 | - } |
|
24 | - |
|
25 | - if (0 === count($point1->points)) { |
|
26 | - findLinkBetween($minDistance * 2, $point1, $positions); |
|
27 | - } |
|
14 | + foreach ($positions as $point2) { |
|
15 | + if ($point1->equals($point2)) { |
|
16 | + continue; |
|
17 | + } |
|
18 | + |
|
19 | + $distance = Dijkstra::distance($point1, $point2); |
|
20 | + if ($distance < $minDistance) { |
|
21 | + $point1->addPoint($point2); |
|
22 | + } |
|
23 | + } |
|
24 | + |
|
25 | + if (0 === count($point1->points)) { |
|
26 | + findLinkBetween($minDistance * 2, $point1, $positions); |
|
27 | + } |
|
28 | 28 | } |
29 | 29 | |
30 | 30 | function findFromTo(array $positions): array |
31 | 31 | { |
32 | - $from = null; |
|
33 | - $to = null; |
|
34 | - foreach ($positions as $point) { |
|
35 | - $from = $from ?: $point; |
|
36 | - $to = $to ?: $point; |
|
37 | - |
|
38 | - if ($point->x < $from->x && $point->y < $from->y) { |
|
39 | - $from = $point; |
|
40 | - } |
|
41 | - |
|
42 | - if ($point->x > $to->x && $point->y > $to->y) { |
|
43 | - $to = $point; |
|
44 | - } |
|
45 | - } |
|
46 | - |
|
47 | - return [$from, $to]; |
|
32 | + $from = null; |
|
33 | + $to = null; |
|
34 | + foreach ($positions as $point) { |
|
35 | + $from = $from ?: $point; |
|
36 | + $to = $to ?: $point; |
|
37 | + |
|
38 | + if ($point->x < $from->x && $point->y < $from->y) { |
|
39 | + $from = $point; |
|
40 | + } |
|
41 | + |
|
42 | + if ($point->x > $to->x && $point->y > $to->y) { |
|
43 | + $to = $point; |
|
44 | + } |
|
45 | + } |
|
46 | + |
|
47 | + return [$from, $to]; |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | function drawPaths(int $max, array $positions, Point $from, Point $to, array $shortestPath, string $filename): void |
51 | 51 | { |
52 | - // open background |
|
53 | - $image = imagecreatetruecolor($max, $max); |
|
54 | - $color = imagecolorallocate($image, 255, 255, 255); |
|
55 | - imagefill($image, 0, 0, $color); |
|
56 | - |
|
57 | - // first run, draw lines |
|
58 | - $color = imagecolorallocate($image, 32, 230, 200); |
|
59 | - foreach ($positions as $point) { |
|
60 | - foreach ($point->points as $link) { |
|
61 | - drawLine($image, $point, $link, $color); |
|
62 | - } |
|
63 | - } |
|
64 | - |
|
65 | - // then, draw the points |
|
66 | - $color = imagecolorallocate($image, 32, 230, 36); |
|
67 | - foreach ($positions as $point) { |
|
68 | - imagefilledellipse($image, $point->x, $point->y, 10, 10, $color); |
|
69 | - } |
|
70 | - |
|
71 | - // draw the shortest path |
|
72 | - $color = imagecolorallocate($image, 255, 0, 255); |
|
73 | - for ($i = 0; $i < count($shortestPath); $i++) { |
|
74 | - $p = $shortestPath[$i]; |
|
75 | - if (isset($shortestPath[$i + 1])) { |
|
76 | - $d = $shortestPath[$i + 1]; |
|
77 | - drawLine($image, $p, $d, $color, 3); |
|
78 | - } |
|
79 | - } |
|
80 | - |
|
81 | - // and finally, draw the from and to points |
|
82 | - $color = imagecolorallocate($image, 255, 0, 255); |
|
83 | - imagefilledellipse($image, $from->x, $from->y, 10, 10, $color); |
|
84 | - imagefilledellipse($image, $to->x, $to->y, 10, 10, $color); |
|
85 | - |
|
86 | - imagepng($image, $filename); |
|
52 | + // open background |
|
53 | + $image = imagecreatetruecolor($max, $max); |
|
54 | + $color = imagecolorallocate($image, 255, 255, 255); |
|
55 | + imagefill($image, 0, 0, $color); |
|
56 | + |
|
57 | + // first run, draw lines |
|
58 | + $color = imagecolorallocate($image, 32, 230, 200); |
|
59 | + foreach ($positions as $point) { |
|
60 | + foreach ($point->points as $link) { |
|
61 | + drawLine($image, $point, $link, $color); |
|
62 | + } |
|
63 | + } |
|
64 | + |
|
65 | + // then, draw the points |
|
66 | + $color = imagecolorallocate($image, 32, 230, 36); |
|
67 | + foreach ($positions as $point) { |
|
68 | + imagefilledellipse($image, $point->x, $point->y, 10, 10, $color); |
|
69 | + } |
|
70 | + |
|
71 | + // draw the shortest path |
|
72 | + $color = imagecolorallocate($image, 255, 0, 255); |
|
73 | + for ($i = 0; $i < count($shortestPath); $i++) { |
|
74 | + $p = $shortestPath[$i]; |
|
75 | + if (isset($shortestPath[$i + 1])) { |
|
76 | + $d = $shortestPath[$i + 1]; |
|
77 | + drawLine($image, $p, $d, $color, 3); |
|
78 | + } |
|
79 | + } |
|
80 | + |
|
81 | + // and finally, draw the from and to points |
|
82 | + $color = imagecolorallocate($image, 255, 0, 255); |
|
83 | + imagefilledellipse($image, $from->x, $from->y, 10, 10, $color); |
|
84 | + imagefilledellipse($image, $to->x, $to->y, 10, 10, $color); |
|
85 | + |
|
86 | + imagepng($image, $filename); |
|
87 | 87 | } |
88 | 88 | |
89 | 89 | function drawLine($image, Point $point1, Point $point2, $color, int $thick = 1): void |
90 | 90 | { |
91 | - if (null === $point1 || null === $point2) { |
|
92 | - return; |
|
93 | - } |
|
94 | - if (null === $point1->x || null === $point1->y) { |
|
95 | - return; |
|
96 | - } |
|
97 | - if (null === $point2->x || null === $point2->y) { |
|
98 | - return; |
|
99 | - } |
|
100 | - |
|
101 | - if ($point1->x === $point2->x) { |
|
102 | - $from = $point1->y < $point2->y ? $point1 : $point2; |
|
103 | - $to = $point1->y > $point2->y ? $point1 : $point2; |
|
104 | - } else { |
|
105 | - $from = $point1->x < $point2->x ? $point1 : $point2; |
|
106 | - $to = $point1->x > $point2->x ? $point1 : $point2; |
|
107 | - } |
|
108 | - |
|
109 | - imagesetthickness($image, $thick); |
|
110 | - |
|
111 | - imageline($image, $from->x, $from->y, $to->x, $to->y, $color); |
|
91 | + if (null === $point1 || null === $point2) { |
|
92 | + return; |
|
93 | + } |
|
94 | + if (null === $point1->x || null === $point1->y) { |
|
95 | + return; |
|
96 | + } |
|
97 | + if (null === $point2->x || null === $point2->y) { |
|
98 | + return; |
|
99 | + } |
|
100 | + |
|
101 | + if ($point1->x === $point2->x) { |
|
102 | + $from = $point1->y < $point2->y ? $point1 : $point2; |
|
103 | + $to = $point1->y > $point2->y ? $point1 : $point2; |
|
104 | + } else { |
|
105 | + $from = $point1->x < $point2->x ? $point1 : $point2; |
|
106 | + $to = $point1->x > $point2->x ? $point1 : $point2; |
|
107 | + } |
|
108 | + |
|
109 | + imagesetthickness($image, $thick); |
|
110 | + |
|
111 | + imageline($image, $from->x, $from->y, $to->x, $to->y, $color); |
|
112 | 112 | } |
@@ -4,35 +4,35 @@ |
||
4 | 4 | |
5 | 5 | class Point |
6 | 6 | { |
7 | - public $x; |
|
8 | - public $y; |
|
9 | - public $points; |
|
10 | - public $ref; |
|
7 | + public $x; |
|
8 | + public $y; |
|
9 | + public $points; |
|
10 | + public $ref; |
|
11 | 11 | |
12 | - public function __construct($x, $y) |
|
13 | - { |
|
14 | - $this->x = $x; |
|
15 | - $this->y = $y; |
|
16 | - $this->points = []; |
|
17 | - $this->ref = "{$x}-{$y}"; |
|
18 | - } |
|
12 | + public function __construct($x, $y) |
|
13 | + { |
|
14 | + $this->x = $x; |
|
15 | + $this->y = $y; |
|
16 | + $this->points = []; |
|
17 | + $this->ref = "{$x}-{$y}"; |
|
18 | + } |
|
19 | 19 | |
20 | - public function addPoint(self $point): self |
|
21 | - { |
|
22 | - if (! in_array($point, $this->points)) { |
|
23 | - $this->points[] = $point; |
|
24 | - } |
|
20 | + public function addPoint(self $point): self |
|
21 | + { |
|
22 | + if (! in_array($point, $this->points)) { |
|
23 | + $this->points[] = $point; |
|
24 | + } |
|
25 | 25 | |
26 | - // add the reverse point |
|
27 | - if (! in_array($this, $point->points)) { |
|
28 | - $point->points[] = $this; |
|
29 | - } |
|
26 | + // add the reverse point |
|
27 | + if (! in_array($this, $point->points)) { |
|
28 | + $point->points[] = $this; |
|
29 | + } |
|
30 | 30 | |
31 | - return $this; |
|
32 | - } |
|
31 | + return $this; |
|
32 | + } |
|
33 | 33 | |
34 | - public function equals(self $point): bool |
|
35 | - { |
|
36 | - return $this->ref === $point->ref; |
|
37 | - } |
|
34 | + public function equals(self $point): bool |
|
35 | + { |
|
36 | + return $this->ref === $point->ref; |
|
37 | + } |
|
38 | 38 | } |
@@ -19,12 +19,12 @@ |
||
19 | 19 | |
20 | 20 | public function addPoint(self $point): self |
21 | 21 | { |
22 | - if (! in_array($point, $this->points)) { |
|
22 | + if (!in_array($point, $this->points)) { |
|
23 | 23 | $this->points[] = $point; |
24 | 24 | } |
25 | 25 | |
26 | 26 | // add the reverse point |
27 | - if (! in_array($this, $point->points)) { |
|
27 | + if (!in_array($this, $point->points)) { |
|
28 | 28 | $point->points[] = $this; |
29 | 29 | } |
30 | 30 |