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

Algorithm   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getDistanceBetween() 0 9 3
A findCentroid() 0 10 2
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