protected function assertFoundSameLength($syntax, AlgorithmShortestDistance $algorithm) { $converter = new ASCIISyntax(); $distance = new Distances\Euclidean(); $matrix = $converter->convertToMatrix($syntax); $source = $converter->findAndCreateNode($matrix, ASCIISyntax::IN); $target = $converter->findAndCreateNode($matrix, ASCIISyntax::OUT); $expectedPath = $converter->generateNodePath($matrix); $expectedLength = $expectedPath->computeLength($distance); $length = $algorithm->computeLength($source, $target); $this->assertSame($expectedLength, $length); }
protected function computeGraph($syntax) { $converter = new ASCIISyntax(); $grid = $converter->convertToGrid($syntax); $distance = new Distances\Euclidean(); $algorithmShortestDistance = new Algorithms\ShortestDistance\Dijkstra($distance); $algorithmShortestDistance->setGrid($grid); $matrix = $converter->convertToMatrix($syntax); $nodes = $converter->findAndCreateNodes($matrix, 'o'); $graph = new NodeGraph($nodes); if (count($nodes) < 2) { return $graph; } $pairs = new Pairs($nodes); foreach ($pairs as $pair) { list($source, $target) = $pair; $length = $algorithmShortestDistance->computeLength($source, $target); $graph->createEdgeBetween($source, $target, $length); } return $graph; }