Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
12 / 12 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| CoordinateUtils | |
100.00% |
12 / 12 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| getCenter | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| getDistance | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Apps\Oev\Utils; |
| 4 | |
| 5 | use Olz\Utils\WithUtilsTrait; |
| 6 | |
| 7 | class CoordinateUtils { |
| 8 | use WithUtilsTrait; |
| 9 | |
| 10 | /** |
| 11 | * @param array<array{x: int|float, y: int|float}> $points |
| 12 | * |
| 13 | * @return array{x: int|float, y: int|float} |
| 14 | */ |
| 15 | public function getCenter(array $points): array { |
| 16 | $sum_x = 0; |
| 17 | $sum_y = 0; |
| 18 | foreach ($points as $point) { |
| 19 | $sum_x += $point['x']; |
| 20 | $sum_y += $point['y']; |
| 21 | } |
| 22 | return [ |
| 23 | 'x' => $sum_x / count($points), |
| 24 | 'y' => $sum_y / count($points), |
| 25 | ]; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @param array{x: int|float, y: int|float} $point_a |
| 30 | * @param array{x: int|float, y: int|float} $point_b |
| 31 | */ |
| 32 | public function getDistance(array $point_a, array $point_b): float { |
| 33 | $x_diff = $point_a['x'] - $point_b['x']; |
| 34 | $y_diff = $point_a['y'] - $point_b['y']; |
| 35 | return sqrt($x_diff * $x_diff + $y_diff * $y_diff); |
| 36 | } |
| 37 | } |