Skip to content

Commit 2f46d1f

Browse files
Sushant NadavadeSushant Nadavade
authored andcommitted
improvement in the code - (added the RotatingCalipers to geometry)
1 parent 830e499 commit 2f46d1f

File tree

2 files changed

+5
-13
lines changed

2 files changed

+5
-13
lines changed

src/main/java/com/thealgorithms/geometry/RotatingCalipers.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff 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
}

src/test/java/com/thealgorithms/geometry/RotatingCalipersTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)