Skip to content

Commit a37b053

Browse files
Sushant NadavadeSushant Nadavade
authored andcommitted
issue is resolved
1 parent 8daa8a4 commit a37b053

File tree

1 file changed

+47
-27
lines changed

1 file changed

+47
-27
lines changed

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

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import java.util.ArrayList;
44
import java.util.Collection;
55
import java.util.Collections;
6+
import java.util.HashSet;
67
import java.util.List;
8+
import java.util.Set;
79

810
/**
911
* A class implementing the Rotating Calipers algorithm for geometric computations on convex polygons.
@@ -138,43 +140,61 @@ public static double computeWidth(Collection<Point> points) {
138140
int n = hull.size();
139141
double minWidth = Double.MAX_VALUE;
140142

141-
// Check all orientations defined by pairs of hull points
142-
// This ensures we find the true minimum width
143+
// Generate all critical orientations for rotating calipers
144+
Set<Double> angles = new HashSet<>();
145+
146+
// Add orientations perpendicular to each edge
147+
for (int i = 0; i < n; i++) {
148+
Point p1 = hull.get(i);
149+
Point p2 = hull.get((i + 1) % n);
150+
151+
double dx = p2.x() - p1.x();
152+
double dy = p2.y() - p1.y();
153+
154+
if (dx != 0 || dy != 0) {
155+
// Angle of the perpendicular to this edge
156+
double angle = Math.atan2(-dx, dy); // perpendicular to (dx,dy) is (-dy,dx)
157+
angles.add(angle);
158+
}
159+
}
160+
161+
// Add orientations defined by vertex pairs (for antipodal cases)
143162
for (int i = 0; i < n; i++) {
144163
for (int j = i + 1; j < n; j++) {
145164
Point p1 = hull.get(i);
146165
Point p2 = hull.get(j);
147-
148-
// Calculate direction vector
166+
149167
double dx = p2.x() - p1.x();
150168
double dy = p2.y() - p1.y();
151-
double len = Math.sqrt(dx * dx + dy * dy);
152-
153-
if (len == 0) continue;
154-
155-
// Unit direction vector
156-
double unitX = dx / len;
157-
double unitY = dy / len;
158-
159-
// Perpendicular unit vector
160-
double perpX = -unitY;
161-
double perpY = unitX;
162-
163-
// Project all points onto the perpendicular direction
164-
double minProj = Double.MAX_VALUE;
165-
double maxProj = Double.MIN_VALUE;
166-
167-
for (Point p : hull) {
168-
double proj = p.x() * perpX + p.y() * perpY;
169-
minProj = Math.min(minProj, proj);
170-
maxProj = Math.max(maxProj, proj);
169+
170+
if (dx != 0 || dy != 0) {
171+
// Angle perpendicular to the line from p1 to p2
172+
double angle = Math.atan2(-dx, dy);
173+
angles.add(angle);
171174
}
172-
173-
double width = maxProj - minProj;
174-
minWidth = Math.min(minWidth, width);
175175
}
176176
}
177177

178+
// Test each critical orientation
179+
for (double angle : angles) {
180+
// Unit vector in the measurement direction
181+
double dirX = Math.sin(angle);
182+
double dirY = Math.cos(angle);
183+
184+
// Project all points onto this direction
185+
double minProj = Double.MAX_VALUE;
186+
double maxProj = Double.MIN_VALUE;
187+
188+
for (Point p : hull) {
189+
double proj = p.x() * dirX + p.y() * dirY;
190+
minProj = Math.min(minProj, proj);
191+
maxProj = Math.max(maxProj, proj);
192+
}
193+
194+
double width = maxProj - minProj;
195+
minWidth = Math.min(minWidth, width);
196+
}
197+
178198
return minWidth;
179199
}
180200

0 commit comments

Comments
 (0)