File tree Expand file tree Collapse file tree 2 files changed +5
-13
lines changed
main/java/com/thealgorithms/geometry
test/java/com/thealgorithms/geometry Expand file tree Collapse file tree 2 files changed +5
-13
lines changed Original file line number Diff line number Diff line change @@ -135,23 +135,15 @@ public static double computeWidth(List<Point> points) {
135135 int n = hull .size ();
136136 double minWidth = Double .MAX_VALUE ;
137137
138- int j = 1 ;
138+ // Use rotating calipers to find minimum width
139139 for (int i = 0 ; i < n ; i ++) {
140140 Point p1 = hull .get (i );
141141 Point p2 = hull .get ((i + 1 ) % n );
142142
143- while (true ) {
144- Point next = hull .get ((j + 1 ) % n );
145- double dist1 = distanceToLine (p1 , p2 , hull .get (j ));
146- double dist2 = distanceToLine (p1 , p2 , next );
147-
148- if (dist2 > dist1 ) {
149- j = (j + 1 ) % n ;
150- } else {
151- break ;
152- }
153- }
143+ // Find the antipodal point for this edge
144+ int j = findAntipodalPoint (hull , i );
154145
146+ // Compute width as distance between parallel lines
155147 double width = distanceToLine (p1 , p2 , hull .get (j ));
156148 minWidth = Math .min (minWidth , width );
157149 }
Original file line number Diff line number Diff line change @@ -151,7 +151,7 @@ void testComputeWidthWithRectangle() {
151151 );
152152 double width = RotatingCalipers .computeWidth (points );
153153
154- assertEquals (Math . sqrt ( 5 ) , width , 1e-9 ); // Width of rectangle
154+ assertEquals (2.0 , width , 1e-9 ); // Width of rectangle (height)
155155 }
156156
157157 @ Test
You can’t perform that action at this time.
0 commit comments