コード例 #1
0
ファイル: Octile.php プロジェクト: letournel/path-finder
 public function compute(Node $a, Node $b)
 {
     $f = sqrt(2) - 1;
     $dx = abs($a->getX() - $b->getX());
     $dy = abs($a->getY() - $b->getY());
     return $dx < $dy ? $f * $dx + $dy : $f * $dy + $dx;
 }
コード例 #2
0
ファイル: Dijkstra.php プロジェクト: letournel/path-finder
 public function computePath(Node $source, Node $target)
 {
     if (!$this->grid instanceof NodeGrid) {
         throw new \RuntimeException('Invalid Grid');
     }
     $priorityQueue = new NodePriorityQueueMin();
     $distanceMap = new NodeMap();
     $previousMap = new NodeMap();
     $priorityQueue->insert($source, 0);
     $distanceMap->insert($source, 0);
     while (!$priorityQueue->isEmpty()) {
         $node = $priorityQueue->extract();
         if ($node->getId() === $target->getId()) {
             return new NodePath($previousMap->lookupFrom($node));
         }
         $neighbors = $this->grid->getWalkableNeighbors($node);
         foreach ($neighbors as $neighbor) {
             $alternativeDistance = $distanceMap->lookup($node) + $this->distance->compute($node, $neighbor);
             if (!$distanceMap->exists($neighbor) || $alternativeDistance < $distanceMap->lookup($neighbor)) {
                 $priorityQueue->insert($neighbor, $alternativeDistance);
                 $distanceMap->insert($neighbor, $alternativeDistance);
                 $previousMap->insert($neighbor, $node);
             }
         }
     }
     return array();
     // no path found
 }
コード例 #3
0
ファイル: AStar.php プロジェクト: letournel/path-finder
 public function computePath(Node $source, Node $target)
 {
     if (!$this->grid instanceof NodeGrid) {
         throw new \RuntimeException('Invalid Grid');
     }
     $fScorePriorityQueue = new NodePriorityQueueMin();
     $gScoreMap = new NodeMap();
     $openedMap = new NodeMap();
     $closedMap = new NodeMap();
     $previousMap = new NodeMap();
     $fScorePriorityQueue->insert($source, 0);
     $gScoreMap->insert($source, 0);
     $openedMap->insert($source);
     while (!$fScorePriorityQueue->isEmpty()) {
         $node = $fScorePriorityQueue->extract();
         $closedMap->insert($node);
         if ($node->getId() === $target->getId()) {
             return new NodePath($previousMap->lookupFrom($node));
         }
         $neighbors = $this->grid->getWalkableNeighbors($node);
         foreach ($neighbors as $neighbor) {
             if ($closedMap->exists($neighbor)) {
                 continue;
             }
             $gScore = $gScoreMap->lookup($node) + $this->distance->compute($node, $neighbor);
             if (!$openedMap->exists($neighbor) || $gScore < $gScoreMap->lookup($neighbor)) {
                 $fScore = $gScore + $this->heuristic->compute($node, $target);
                 $fScorePriorityQueue->insert($neighbor, $fScore);
                 $gScoreMap->insert($neighbor, $gScore);
                 $openedMap->insert($neighbor);
                 $previousMap->insert($neighbor, $node);
             }
         }
     }
     // no path found
     return array();
 }
コード例 #4
0
ファイル: Chebyshev.php プロジェクト: letournel/path-finder
 public function compute(Node $a, Node $b)
 {
     $dx = abs($a->getX() - $b->getX());
     $dy = abs($a->getY() - $b->getY());
     return max($dx, $dy);
 }
コード例 #5
0
ファイル: Euclidean.php プロジェクト: letournel/path-finder
 public function compute(Node $a, Node $b)
 {
     $dx = abs($a->getX() - $b->getX());
     $dy = abs($a->getY() - $b->getY());
     return sqrt($dx * $dx + $dy * $dy);
 }