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) {
135
135
int n = hull .size ();
136
136
double minWidth = Double .MAX_VALUE ;
137
137
138
- int j = 1 ;
138
+ // Use rotating calipers to find minimum width
139
139
for (int i = 0 ; i < n ; i ++) {
140
140
Point p1 = hull .get (i );
141
141
Point p2 = hull .get ((i + 1 ) % n );
142
142
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 );
154
145
146
+ // Compute width as distance between parallel lines
155
147
double width = distanceToLine (p1 , p2 , hull .get (j ));
156
148
minWidth = Math .min (minWidth , width );
157
149
}
Original file line number Diff line number Diff line change @@ -151,7 +151,7 @@ void testComputeWidthWithRectangle() {
151
151
);
152
152
double width = RotatingCalipers .computeWidth (points );
153
153
154
- assertEquals (Math . sqrt ( 5 ) , width , 1e-9 ); // Width of rectangle
154
+ assertEquals (2.0 , width , 1e-9 ); // Width of rectangle (height)
155
155
}
156
156
157
157
@ Test
You can’t perform that action at this time.
0 commit comments