/** * Calculates the cost between 2 nodes * @param zibo\library\diagram\path\Node $node1 The first node * @param zibo\library\diagram\path\Node $node2 The second node * @param integer $orthogonalCost Cost for a non-diagonal move * @param integer $diagonalCost Cost for a diagonal move * @return integer */ public function calculateCost(Node $node1, Node $node2, $orthogonalCost, $diagonalCost) { $p1 = $node1->getPoint(); $x1 = $p1->getX(); $y1 = $p1->getY(); $p2 = $node2->getPoint(); $x2 = $p2->getX(); $y2 = $p2->getY(); if ($x1 != $x2 && $y1 != $y2) { return $diagonalCost; } return $orthogonalCost; }
/** * Calculates the cost between 2 nodes * @param zibo\library\diagram\path\Node $node1 The first node * @param zibo\library\diagram\path\Node $node2 The second node * @param integer $orthogonalCost Cost for a non-diagonal move * @param integer $diagonalCost Cost for a diagonal move * @return integer */ public function calculateCost(Node $node1, Node $node2, $orthogonalCost, $diagonalCost) { $p1 = $node1->getPoint(); $x1 = $p1->getX(); $y1 = $p1->getY(); $p2 = $node2->getPoint(); $x2 = $p2->getX(); $y2 = $p2->getY(); $cost = 0; if ($x1 == $x2 && $y1 == $y2) { return $cost; } $diffX = $this->getDifference($x1, $x2); $diffY = $this->getDifference($y1, $y2); $min = min($diffX, $diffY); $max = max($diffX, $diffY); $cost += $min * $diagonalCost; $cost += ($max - $min) * $orthogonalCost; return $cost; }
/** * Calculates the cost between 2 nodes * @param zibo\library\diagram\path\Node $node1 The first node * @param zibo\library\diagram\path\Node $node2 The second node * @param integer $orthogonalCost Cost for a non-diagonal move * @param integer $diagonalCost Cost for a diagonal move * @return integer */ public function calculateCost(Node $node1, Node $node2, $orthogonalCost, $diagonalCost) { $p1 = $node1->getPoint(); $x1 = $p1->getX(); $y1 = $p1->getY(); $p2 = $node2->getPoint(); $x2 = $p2->getX(); $y2 = $p2->getY(); $cost = 0; if ($x1 == $x2 && $y1 == $y2) { return $cost; } $cost += $this->getDifference($x1, $x2); $cost += $this->getDifference($y1, $y2); $cost *= $orthogonalCost; if ($x1 == $x2 || $y1 == $y2) { $cost -= round($orthogonalCost / 2); } return $cost; }
/** * Gets the surrounding nodes of the provided Node. * @param Node $node * @return array Surrounding nodeswhich are free in the grid and not on the closed list */ protected function getSurroundingNodes(Node $node) { $thresholdCost = 3 * $this->orthogonalCost; $movementCost = $node->getMovementCost(); $estimatedMovementCost = $node->getEstimatedMovementCost(); if ($movementCost < $thresholdCost || $estimatedMovementCost < $thresholdCost) { $threshold = 0; } elseif ($movementCost < $thresholdCost * 5 || $estimatedMovementCost < $thresholdCost * 5) { $threshold = 1; } else { $threshold = 2; } $x = $node->getPoint()->getX(); $y = $node->getPoint()->getY(); $minX = min(max($this->gridMinX, $x - 1), $this->gridMaxX); $maxX = min(max($this->gridMinX, $x + 1), $this->gridMaxX); $minY = min(max($this->gridMinY, $y - 1), $this->gridMaxY); $maxY = min(max($this->gridMinY, $y + 1), $this->gridMaxY); $surroundingNodes = array(); for ($i = $minX; $i <= $maxX; $i++) { for ($j = $minY; $j <= $maxY; $j++) { if ($i == $x && $j == $y) { continue; } $point = new Point($i, $j); $node = new Node($point); $id = $node->getId(); if (array_key_exists($id, $this->closed)) { continue; } if (!$this->grid->isFree($point, $threshold)) { continue; } $surroundingNodes[$id] = $node; } } return $surroundingNodes; }