Passed
Pull Request — master (#34)
by Benjamin
26:50 queued 11:50
created

Algorithm::getDistanceBetween()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Kmeans\Euclidean;
4
5
use Kmeans\Algorithm as BaseAlgorithm;
6
use Kmeans\Interfaces\PointCollectionInterface;
7
use Kmeans\Interfaces\PointInterface;
8
use Kmeans\Math;
9
10
class Algorithm extends BaseAlgorithm
11
{
12
    public function getDistanceBetween(PointInterface $pointA, PointInterface $pointB): float
13
    {
14
        if (! $pointA instanceof Point || ! $pointB instanceof Point) {
15
            throw new \InvalidArgumentException(
16
                "Euclidean Algorithm can only calculate distance between euclidean points"
17
            );
18
        }
19
20
        return Math::euclideanDist($pointA->getCoordinates(), $pointB->getCoordinates());
21
    }
22
23
    public function findCentroid(PointCollectionInterface $points): PointInterface
24
    {
25
        if (! $points->getSpace() instanceof Space) {
26
            throw new \InvalidArgumentException(
27
                "Point collection should consist of Euclidean points"
28
            );
29
        }
30
31
        return $points->getSpace()->makePoint(Math::centroid(
32
            array_map(fn (PointInterface $point) => $point->getCoordinates(), iterator_to_array($points))
33
        ));
34
    }
35
}
36