-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPolygon.php
More file actions
114 lines (103 loc) · 2.59 KB
/
Copy pathPolygon.php
File metadata and controls
114 lines (103 loc) · 2.59 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
namespace Mindee\Geometry;
/**
* Polygon represented as a set of coordinates (vertices/points).
*/
class Polygon
{
/**
* @var Point[]|null Vertices of the polygon.
*/
public ?array $coordinates;
/**
* @param array|null $coordinates Coordinates of the polygon as a set of Points.
*/
public function __construct(?array $coordinates = null)
{
if (!is_null($coordinates)) {
$this->coordinates = [];
foreach ($coordinates as $point) {
$this->coordinates[] = new Point($point[0], $point[1]);
}
} else {
$this->coordinates = null;
}
}
/**
* Retrieves the centroid of the polygon.
*
* @return Point
*/
public function getCentroid(): Point
{
return PolygonUtils::getCentroid($this->coordinates);
}
/**
* Retrieves the upper and lower bounds of the y-axis.
*
* @return MinMax
*/
public function getMinMaxY(): MinMax
{
return MinMaxUtils::getMinMaxY($this->coordinates);
}
/**
* Retrieves the upper and lower bounds of the x-axis.
*
* @return MinMax
*/
public function getMinMaxX(): MinMax
{
return MinMaxUtils::getMinMaxX($this->coordinates);
}
/**
* Checks whether a point is located within the polygon's y-axis.
*
* @param Point $point Point to check.
* @return boolean
*/
public function isPointInY(Point $point): bool
{
$minMax = $this->getMinMaxY();
return PolygonUtils::isPointInY($point, $minMax->getMin(), $minMax->getMax());
}
/**
* Checks whether a point is located within the polygon's x-axis.
*
* @param Point $point Point to check.
* @return boolean
*/
public function isPointInX(Point $point): bool
{
$minMax = $this->getMinMaxX();
return PolygonUtils::isPointInX($point, $minMax->getMin(), $minMax->getMax());
}
/**
* Checks whether the Polygon has coordinates.
*
* @return boolean
*/
public function isEmpty(): bool
{
return count($this->coordinates) == 0;
}
/**
* Retrieves the coordinates of the polygon.
*
* @return array|null
*/
public function getCoordinates(): ?array
{
return $this->coordinates;
}
/**
* @return string String representation.
*/
public function __toString()
{
if (!$this->isEmpty()) {
return 'Polygon with ' . count($this->getCoordinates()) . ' points.';
}
return '';
}
}