Taxicab::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 Stratadox\Pathfinder\Metric;
7
use Stratadox\Pathfinder\Position;
8
9
final class Taxicab implements Metric
10
{
11
    private $dimensions;
12
13
    public function __construct(int $dimensions)
14
    {
15
        $this->dimensions = $dimensions;
16
    }
17
18
    public static function distance(): Metric
19
    {
20
        return new self(2);
21
    }
22
23
    public static function inDimensions(int $amount): Metric
24
    {
25
        return new self($amount);
26
    }
27
28
    public function distanceBetween(Position $start, Position $goal): float
29
    {
30
        $sum = 0;
31
        for ($i = $this->dimensions - 1; $i >= 0; --$i) {
32
            $sum += abs($start[$i] - $goal[$i]);
33
        }
34
        return $sum;
35
    }
36
}
37