Skip to content

Commit 72ad48a

Browse files
committed
feat: added rotating calipers
1 parent 5175429 commit 72ad48a

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

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

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ private RotatingCalipers() {
1515
}
1616

1717
// -------------------- Inner Classes --------------------
18-
public record PointPair(Point p1, Point p2, double distance) {
19-
}
18+
public record PointPair(Point p1, Point p2, double distance) {}
2019

21-
public record Rectangle(Point[] corners, double width, double height, double area) {
22-
}
20+
public record Rectangle(Point[] corners, double width, double height, double area) {}
2321

2422
// -------------------- Diameter --------------------
2523
public static PointPair diameter(List<Point> points) {
@@ -29,15 +27,14 @@ public static PointPair diameter(List<Point> points) {
2927

3028
List<Point> hull = ConvexHull.convexHullRecursive(points);
3129
orderCounterClockwise(hull);
32-
int n = hull.size();
3330

3431
double maxDist = 0;
3532
Point bestA = hull.get(0);
3633
Point bestB = hull.get(0);
37-
34+
int n = hull.size();
3835
int j = 1;
39-
for (int i = 0; i < n; i++) {
40-
Point a = hull.get(i);
36+
37+
for (Point a : hull) {
4138
while (true) {
4239
Point b1 = hull.get(j);
4340
Point b2 = hull.get((j + 1) % n);
@@ -56,6 +53,7 @@ public static PointPair diameter(List<Point> points) {
5653
bestB = hull.get(j);
5754
}
5855
}
56+
5957
return new PointPair(bestA, bestB, Math.sqrt(maxDist));
6058
}
6159

@@ -67,9 +65,9 @@ public static double width(List<Point> points) {
6765

6866
List<Point> hull = ConvexHull.convexHullRecursive(points);
6967
orderCounterClockwise(hull);
70-
int n = hull.size();
7168

7269
double minWidth = Double.MAX_VALUE;
70+
int n = hull.size();
7371

7472
for (int i = 0; i < n; i++) {
7573
Point a = hull.get(i);
@@ -86,14 +84,14 @@ public static double width(List<Point> points) {
8684

8785
double minProjV = Double.MAX_VALUE;
8886
double maxProjV = -Double.MAX_VALUE;
89-
9087
for (Point p : hull) {
9188
double projV = p.x() * vx + p.y() * vy;
9289
minProjV = Math.min(minProjV, projV);
9390
maxProjV = Math.max(maxProjV, projV);
9491
}
9592
minWidth = Math.min(minWidth, maxProjV - minProjV);
9693
}
94+
9795
return minWidth;
9896
}
9997

@@ -105,12 +103,12 @@ public static Rectangle minAreaBoundingRectangle(List<Point> points) {
105103

106104
List<Point> hull = ConvexHull.convexHullRecursive(points);
107105
orderCounterClockwise(hull);
108-
int n = hull.size();
109106

110107
double minArea = Double.MAX_VALUE;
111108
Point[] bestCorners = null;
112109
double bestWidth = 0;
113110
double bestHeight = 0;
111+
int n = hull.size();
114112

115113
for (int i = 0; i < n; i++) {
116114
Point a = hull.get(i);
@@ -132,10 +130,18 @@ public static Rectangle minAreaBoundingRectangle(List<Point> points) {
132130
for (Point p : hull) {
133131
double projU = p.x() * ux + p.y() * uy;
134132
double projV = p.x() * vx + p.y() * vy;
135-
minU = Math.min(minU, projU);
136-
maxU = Math.max(maxU, projU);
137-
minV = Math.min(minV, projV);
138-
maxV = Math.max(maxV, projV);
133+
if (projU < minU) {
134+
minU = projU;
135+
}
136+
if (projU > maxU) {
137+
maxU = projU;
138+
}
139+
if (projV < minV) {
140+
minV = projV;
141+
}
142+
if (projV > maxV) {
143+
maxV = projV;
144+
}
139145
}
140146

141147
double width = maxU - minU;
@@ -157,9 +163,10 @@ public static Rectangle minAreaBoundingRectangle(List<Point> points) {
157163
// -------------------- Helper Methods --------------------
158164
private static void orderCounterClockwise(List<Point> points) {
159165
double area = 0.0;
160-
for (int i = 0; i < points.size(); i++) {
166+
int n = points.size();
167+
for (int i = 0; i < n; i++) {
161168
Point a = points.get(i);
162-
Point b = points.get((i + 1) % points.size());
169+
Point b = points.get((i + 1) % n);
163170
area += (a.x() * b.y()) - (b.x() * a.y());
164171
}
165172
if (area < 0) {

0 commit comments

Comments
 (0)