Euclidean::distanceBetween()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 2
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Pathfinder\Distance;
4
5
use function sqrt;
6
use Stratadox\Pathfinder\Metric;
7
use Stratadox\Pathfinder\Position;
8
9
final class Euclidean 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 += ($start[$i] - $goal[$i]) ** 2;
33
        }
34
        return sqrt($sum);
35
    }
36
}
37