11package com .mindee .geometry ;
22
3- import java .util .Arrays ;
4- import java .util .Collections ;
53import java .util .List ;
64import java .util .stream .Collectors ;
75
@@ -19,25 +17,25 @@ private PolygonUtils() {
1917 public static Point getCentroid (List <Point > vertices ) {
2018 int verticesSum = vertices .size ();
2119
22- double xSum = vertices .stream ().map (Point ::getX ). mapToDouble ( Double :: doubleValue ).sum ();
23- double ySum = vertices .stream ().map (Point ::getY ). mapToDouble ( Double :: doubleValue ).sum ();
20+ double xSum = vertices .stream ().mapToDouble (Point ::getX ).sum ();
21+ double ySum = vertices .stream ().mapToDouble (Point ::getY ).sum ();
2422 return new Point (xSum / verticesSum , ySum / verticesSum );
2523 }
2624
2725 /**
2826 * Get the maximum and minimum Y coordinates in a given list of Points.
2927 */
3028 public static MinMax getMinMaxY (List <Point > vertices ) {
31- List < Double > points = vertices .stream ().map (Point ::getY ).collect ( Collectors . toList () );
32- return new MinMax (Collections . min ( points ), Collections . max ( points ));
29+ var stats = vertices .stream ().mapToDouble (Point ::getY ).summaryStatistics ( );
30+ return new MinMax (stats . getMin ( ), stats . getMax ( ));
3331 }
3432
3533 /**
3634 * Get the maximum and minimum X coordinates in a given list of Points.
3735 */
3836 public static MinMax getMinMaxX (List <Point > vertices ) {
39- List < Double > points = vertices .stream ().map (Point ::getX ).collect ( Collectors . toList () );
40- return new MinMax (Collections . min ( points ), Collections . max ( points ));
37+ var stats = vertices .stream ().mapToDouble (Point ::getX ).summaryStatistics ( );
38+ return new MinMax (stats . getMin ( ), stats . getMax ( ));
4139 }
4240
4341 /**
@@ -70,77 +68,20 @@ public static Polygon combine(Polygon base, Polygon target) {
7068 target = base ;
7169 }
7270
73- Double maxx = Math
74- .max (
75- target
76- .getCoordinates ()
77- .stream ()
78- .map (Point ::getX )
79- .max (Double ::compareTo )
80- .orElse (Double .MIN_VALUE ),
81- base
82- .getCoordinates ()
83- .stream ()
84- .map (Point ::getX )
85- .max (Double ::compareTo )
86- .orElse (Double .MIN_VALUE )
87- );
71+ var combinedCoords = java .util .stream .Stream
72+ .concat (base .getCoordinates ().stream (), target .getCoordinates ().stream ())
73+ .collect (Collectors .toList ());
8874
89- Double minx = Math
90- .min (
91- target
92- .getCoordinates ()
93- .stream ()
94- .map (Point ::getX )
95- .min (Double ::compareTo )
96- .orElse (Double .MAX_VALUE ),
97- base
98- .getCoordinates ()
99- .stream ()
100- .map (Point ::getX )
101- .min (Double ::compareTo )
102- .orElse (Double .MAX_VALUE )
103- );
104-
105- Double maxy = Math
106- .max (
107- target
108- .getCoordinates ()
109- .stream ()
110- .map (Point ::getY )
111- .max (Double ::compareTo )
112- .orElse (Double .MIN_VALUE ),
113- base
114- .getCoordinates ()
115- .stream ()
116- .map (Point ::getY )
117- .max (Double ::compareTo )
118- .orElse (Double .MIN_VALUE )
119- );
120-
121- Double miny = Math
122- .min (
123- target
124- .getCoordinates ()
125- .stream ()
126- .map (Point ::getY )
127- .min (Double ::compareTo )
128- .orElse (Double .MAX_VALUE ),
129- base
130- .getCoordinates ()
131- .stream ()
132- .map (Point ::getY )
133- .min (Double ::compareTo )
134- .orElse (Double .MAX_VALUE )
135- );
75+ var xStats = combinedCoords .stream ().mapToDouble (Point ::getX ).summaryStatistics ();
76+ var yStats = combinedCoords .stream ().mapToDouble (Point ::getY ).summaryStatistics ();
13677
13778 return new Polygon (
138- Arrays
139- .asList (
140- new Point (minx , miny ),
141- new Point (maxx , miny ),
142- new Point (maxx , maxy ),
143- new Point (minx , maxy )
79+ List
80+ .of (
81+ new Point (xStats . getMin (), yStats . getMin () ),
82+ new Point (xStats . getMax (), yStats . getMin () ),
83+ new Point (xStats . getMax (), yStats . getMax () ),
84+ new Point (xStats . getMin (), yStats . getMax () )
14485 )
14586 );
14687 }
0 commit comments