Chebyshev   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 8
c 1
b 0
f 0
dl 0
loc 26
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A distance() 0 3 1
A inDimensions() 0 3 1
A __construct() 0 3 1
A distanceBetween() 0 7 2
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Pathfinder\Distance;
4
5
use function abs;
6
use function max;
7
use Stratadox\Pathfinder\Metric;
8
use Stratadox\Pathfinder\Position;
9
10
final class Chebyshev implements Metric
11
{
12
    private $dimensions;
13
14
    public function __construct(int $dimensions)
15
    {
16
        $this->dimensions = $dimensions;
17
    }
18
19
    public static function distance(): Metric
20
    {
21
        return new self(2);
22
    }
23
24
    public static function inDimensions(int $amount): Metric
25
    {
26
        return new self($amount);
27
    }
28
29
    public function distanceBetween(Position $start, Position $goal): float
30
    {
31
        $sum = [];
32
        for ($i = $this->dimensions - 1; $i >= 0; --$i) {
33
            $sum[] = abs($start[$i] - $goal[$i]);
34
        }
35
        return max($sum);
36
    }
37
}
38