22
33namespace Mindee \Geometry ;
44
5+ use Mindee \Error \MindeeGeometryException ;
6+
57/**
68 * Polygon represented as a set of coordinates (vertices/points).
79 */
@@ -12,6 +14,16 @@ class Polygon
1214 */
1315 public ?array $ coordinates ;
1416
17+ /**
18+ * @var MinMax Min and max Y values of the polygon.
19+ */
20+ private MinMax $ minMaxY ;
21+
22+ /**
23+ * @var MinMax Min and max X values of the polygon.
24+ */
25+ private MinMax $ minMaxX ;
26+
1527 /**
1628 * @param array|null $coordinates Coordinates of the polygon as a set of Points.
1729 */
@@ -44,7 +56,10 @@ public function getCentroid(): Point
4456 */
4557 public function getMinMaxY (): MinMax
4658 {
47- return MinMaxUtils::getMinMaxY ($ this ->coordinates );
59+ if (!isset ($ this ->minMaxY )) {
60+ $ this ->minMaxY = MinMaxUtils::getMinMaxY ($ this ->coordinates );
61+ }
62+ return $ this ->minMaxY ;
4863 }
4964
5065 /**
@@ -54,7 +69,10 @@ public function getMinMaxY(): MinMax
5469 */
5570 public function getMinMaxX (): MinMax
5671 {
57- return MinMaxUtils::getMinMaxX ($ this ->coordinates );
72+ if (!isset ($ this ->minMaxX )) {
73+ $ this ->minMaxX = MinMaxUtils::getMinMaxX ($ this ->coordinates );
74+ }
75+ return $ this ->minMaxX ;
5876 }
5977
6078 /**
@@ -81,6 +99,46 @@ public function isPointInX(Point $point): bool
8199 return PolygonUtils::isPointInX ($ point , $ minMax ->getMin (), $ minMax ->getMax ());
82100 }
83101
102+ /**
103+ * Retrieves the minimum X coordinate.
104+ *
105+ * @return float
106+ */
107+ public function getMinX (): float
108+ {
109+ return $ this ->getMinMaxX ()->getMin ();
110+ }
111+
112+ /**
113+ * Retrieves the maximum X coordinate.
114+ *
115+ * @return float
116+ */
117+ public function getMaxX (): float
118+ {
119+ return $ this ->getMinMaxX ()->getMax ();
120+ }
121+
122+ /**
123+ * Retrieves the minimum Y coordinate.
124+ *
125+ * @return float
126+ */
127+ public function getMinY (): float
128+ {
129+ return $ this ->getMinMaxY ()->getMin ();
130+ }
131+
132+ /**
133+ * Retrieves the maximum Y coordinate.
134+ *
135+ * @return float
136+ */
137+ public function getMaxY (): float
138+ {
139+ return $ this ->getMinMaxY ()->getMax ();
140+ }
141+
84142 /**
85143 * Checks whether the Polygon has coordinates.
86144 *
0 commit comments