-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPolygon.php
More file actions
170 lines (153 loc) · 3.68 KB
/
Copy pathPolygon.php
File metadata and controls
170 lines (153 loc) · 3.68 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?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;
/**
* @var MinMax Min and max Y values of the polygon.
*/
private MinMax $minMaxY;
/**
* @var MinMax Min and max X values of the polygon.
*/
private MinMax $minMaxX;
/**
* @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
{
if (!isset($this->minMaxY)) {
$this->minMaxY = MinMaxUtils::getMinMaxY($this->coordinates);
}
return $this->minMaxY;
}
/**
* Retrieves the upper and lower bounds of the x-axis.
*
* @return MinMax
*/
public function getMinMaxX(): MinMax
{
if (!isset($this->minMaxX)) {
$this->minMaxX = MinMaxUtils::getMinMaxX($this->coordinates);
}
return $this->minMaxX;
}
/**
* 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());
}
/**
* Retrieves the minimum X coordinate.
*
* @return float
*/
public function getMinX(): float
{
return $this->getMinMaxX()->getMin();
}
/**
* Retrieves the maximum X coordinate.
*
* @return float
*/
public function getMaxX(): float
{
return $this->getMinMaxX()->getMax();
}
/**
* Retrieves the minimum Y coordinate.
*
* @return float
*/
public function getMinY(): float
{
return $this->getMinMaxY()->getMin();
}
/**
* Retrieves the maximum Y coordinate.
*
* @return float
*/
public function getMaxY(): float
{
return $this->getMinMaxY()->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 '';
}
}