-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMinMaxUtils.php
More file actions
64 lines (58 loc) · 1.89 KB
/
Copy pathMinMaxUtils.php
File metadata and controls
64 lines (58 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
declare(strict_types=1);
namespace Mindee\Geometry;
use Mindee\Error\ErrorCode;
use Mindee\Error\MindeeGeometryException;
use function count;
/**
* Utility class for MinMax.
*/
class MinMaxUtils
{
/**
* Retrieves the upper and lower bounds of the y-axis from an array of points.
*
* @param array<Point>|Polygon $points An array of points.
* @throws MindeeGeometryException Throws if the provided array is too small.
*/
public static function getMinMaxY(array|Polygon $points): MinMax
{
if ($points instanceof Polygon) {
$points = $points->getCoordinates();
}
if (count($points) < 1) {
throw new MindeeGeometryException(
'The provided point array must have at least 1 point to calculate the Y bounds.',
ErrorCode::GEOMETRIC_OPERATION_FAILED
);
}
$yCoords = [];
foreach ($points as $point) {
$yCoords[] = $point->getY();
}
return new MinMax(min($yCoords), max($yCoords));
}
/**
* Retrieves the upper and lower bounds of the x-axis from an array of points.
*
* @param array<Point>|Polygon $points An array of points.
* @throws MindeeGeometryException Throws if the provided array is too small.
*/
public static function getMinMaxX(array|Polygon $points): MinMax
{
if ($points instanceof Polygon) {
$points = $points->getCoordinates();
}
if (count($points) < 1) {
throw new MindeeGeometryException(
'The provided point array must have at least 1 point to calculate the X bounds.',
ErrorCode::GEOMETRIC_OPERATION_FAILED
);
}
$xCoords = [];
foreach ($points as $point) {
$xCoords[] = $point->getX();
}
return new MinMax(min($xCoords), max($xCoords));
}
}