Chebyshev::distance()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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