Conditions | 4 |
Paths | 4 |
Total Lines | 28 |
Code Lines | 16 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
13 | public function distanceTo(CoordinateInterface $coordinate): ImmutableNumber |
||
14 | { |
||
15 | if (!($coordinate instanceof CartesianCoordinate)) { |
||
16 | throw new IntegrityConstraint( |
||
17 | 'Must be the same coordinate system', |
||
18 | 'Only attempt to get distance between coordinates that use the same coordinate system', |
||
19 | 'Distance cannot be calculated between coordinates that use different coordinate systems because the properties necessary to convert them cannot be assumed' |
||
20 | ); |
||
21 | } |
||
22 | |||
23 | if ($this->size() != $coordinate->size()) { |
||
24 | throw new IntegrityConstraint( |
||
25 | 'Must have same dimensionality', |
||
26 | 'Check dimensionality of both coordinates before getting distance', |
||
27 | 'Coordinates must share the same number of axes in order for a distance to be calculated' |
||
28 | ); |
||
29 | } |
||
30 | |||
31 | $n = Numbers::makeZero(); |
||
32 | |||
33 | foreach ($this->all() as $index => $value) { |
||
34 | $n = $n->add($coordinate->get($index)->subtract($value)->pow(2)); |
||
35 | } |
||
36 | |||
37 | $n = $n->sqrt(); |
||
38 | |||
39 | return $n; |
||
40 | } |
||
41 | |||
42 | } |