Conditions | 3 |
Paths | 3 |
Total Lines | 20 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
10 | public function getDistanceBetween(PointInterface $pointA, PointInterface $pointB): float |
||
11 | { |
||
12 | if ($pointA->getSpace()->getDimention() !== $pointB->getSpace()->getDimention()) { |
||
13 | throw new \LogicException( |
||
14 | "Cannot calculate euclidian distance between point of different dimentions" |
||
15 | ); |
||
16 | } |
||
17 | |||
18 | $distance = 0; |
||
19 | $dimention = $pointA->getSpace()->getDimention(); |
||
20 | $coordinatesA = $pointA->getCoordinates(); |
||
21 | $coordinatesB = $pointB->getCoordinates(); |
||
22 | |||
23 | for ($n = 0; $n < $dimention; $n++) { |
||
24 | $difference = $coordinatesA[$n] - $coordinatesB[$n]; |
||
25 | $distance += $difference ** 2; |
||
26 | } |
||
27 | |||
28 | return sqrt($distance); |
||
29 | } |
||
30 | } |
||
31 |