Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
Heuristic
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
7
100.00% covered (success)
100.00%
1 / 1
 metric
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Algorithm\PathFinding
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\Algorithm\PathFinding;
16
17use phpOMS\Math\Topology\Metrics2D;
18
19/**
20 * Node on grid.
21 *
22 * @package phpOMS\Algorithm\PathFinding
23 * @license OMS License 2.0
24 * @link    https://jingga.app
25 * @since   1.0.0
26 */
27final class Heuristic
28{
29    /**
30     * Calculate metric/distance between two nodes.
31     *
32     * @param array<string, int|float> $node1     Array with 'x' and 'y' coordinate
33     * @param array<string, int|float> $node2     Array with 'x' and 'y' coordinate
34     * @param int                      $heuristic Heuristic to use for calculation
35     *
36     * @return float
37     *
38     * @since 1.0.0
39     */
40    public static function metric(array $node1, array $node2, int $heuristic) : float
41    {
42        if ($heuristic === HeuristicType::MANHATTAN) {
43            return Metrics2D::manhattan($node1, $node2);
44        } elseif ($heuristic === HeuristicType::EUCLIDEAN) {
45            return Metrics2D::euclidean($node1, $node2);
46        } elseif ($heuristic === HeuristicType::OCTILE) {
47            return Metrics2D::octile($node1, $node2);
48        } elseif ($heuristic === HeuristicType::MINKOWSKI) {
49            return Metrics2D::minkowski($node1, $node2, 1);
50        } elseif ($heuristic === HeuristicType::CANBERRA) {
51            return Metrics2D::canberra($node1, $node2);
52        } elseif ($heuristic === HeuristicType::BRAY_CURTIS) {
53            return Metrics2D::brayCurtis($node1, $node2);
54        }
55
56        return Metrics2D::chebyshev($node1, $node2);
57    }
58}