33import java .util .Arrays ;
44import java .util .Collections ;
55import java .util .List ;
6- import java .util .OptionalDouble ;
76import java .util .stream .Collectors ;
87
98/**
109 * Methods for working with Polygons.
10+ * Used internally, use at your own risk.
1111 */
1212public final class PolygonUtils {
1313 private PolygonUtils () {
1414 }
1515
16- /**
17- * Create a Polygon from a list of a list of floats.
18- */
19- public static Polygon getFrom (List <List <Double >> polygon ) {
20- List <Point > coordinates = polygon
21- .stream ()
22- .map (coordinate -> new Point (coordinate .get (0 ), coordinate .get (1 )))
23- .collect (Collectors .toList ());
24- return new Polygon (coordinates );
25- }
26-
2716 /**
2817 * Get the central coordinates (centroid) of a list of Points.
2918 */
@@ -35,30 +24,6 @@ public static Point getCentroid(List<Point> vertices) {
3524 return new Point (xSum / verticesSum , ySum / verticesSum );
3625 }
3726
38- /**
39- * Compare two polygons based on their Y coordinates.
40- * Useful for sorting lists.
41- */
42- public static int CompareOnY (Polygon polygon1 , Polygon polygon2 ) {
43- double sort = getMinYCoordinate (polygon1 ) - getMinYCoordinate (polygon2 );
44- if (sort == 0 ) {
45- return 0 ;
46- }
47- return sort < 0 ? -1 : 1 ;
48- }
49-
50- /**
51- * Compare two polygons based on their X coordinates.
52- * Useful for sorting lists.
53- */
54- public static int CompareOnX (Polygon polygon1 , Polygon polygon2 ) {
55- double sort = getMinXCoordinate (polygon1 ) - getMinXCoordinate (polygon2 );
56- if (sort == 0 ) {
57- return 0 ;
58- }
59- return sort < 0 ? -1 : 1 ;
60- }
61-
6227 /**
6328 * Get the maximum and minimum Y coordinates in a given list of Points.
6429 */
@@ -90,75 +55,6 @@ public static boolean isPointInPolygonY(Point centroid, Polygon polygon) {
9055 return isPointInY (centroid , yCoords .getMin (), yCoords .getMax ());
9156 }
9257
93- public static Double getMinYCoordinate (Polygon polygon ) {
94- OptionalDouble min = polygon
95- .getCoordinates ()
96- .stream ()
97- .map (Point ::getY )
98- .mapToDouble (Double ::doubleValue )
99- .min ();
100-
101- if (min .isPresent ()) {
102- return min .getAsDouble ();
103- }
104-
105- throw new IllegalStateException (
106- "The min Y could not be found "
107- + "because it seems that there is no coordinates in the current polygon."
108- );
109- }
110-
111- public static Double getMaxYCoordinate (Polygon polygon ) {
112- OptionalDouble max = polygon
113- .getCoordinates ()
114- .stream ()
115- .map (Point ::getY )
116- .mapToDouble (Double ::doubleValue )
117- .max ();
118- if (max .isPresent ()) {
119- return max .getAsDouble ();
120- }
121-
122- throw new IllegalStateException (
123- "The max Y could not be found "
124- + "because it seems that there is no coordinates in the current polygon."
125- );
126- }
127-
128- public static Double getMinXCoordinate (Polygon polygon ) {
129- OptionalDouble min = polygon
130- .getCoordinates ()
131- .stream ()
132- .map (Point ::getX )
133- .mapToDouble (Double ::doubleValue )
134- .min ();
135- if (min .isPresent ()) {
136- return min .getAsDouble ();
137- }
138-
139- throw new IllegalStateException (
140- "The min X could not be found "
141- + "because it seems that there is no coordinates in the current polygon."
142- );
143- }
144-
145- public static Double getMaxXCoordinate (Polygon polygon ) {
146- OptionalDouble max = polygon
147- .getCoordinates ()
148- .stream ()
149- .map (Point ::getX )
150- .mapToDouble (Double ::doubleValue )
151- .max ();
152- if (max .isPresent ()) {
153- return max .getAsDouble ();
154- }
155-
156- throw new IllegalStateException (
157- "The max X could not be found "
158- + "because it seems that there is no coordinates in the current polygon."
159- );
160- }
161-
16258 /**
16359 * Merge the coordinates of the two polygons.
16460 */
0 commit comments