This repository was archived by the owner on Mar 22, 2026. It is now read-only.
forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotatingCalipers.java
More file actions
184 lines (154 loc) · 5.85 KB
/
RotatingCalipers.java
File metadata and controls
184 lines (154 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.thealgorithms.geometry;
import java.util.Collections;
import java.util.List;
/**
* Rotating Calipers algorithm to compute:
* - Diameter of a convex polygon
* - Width of a convex polygon
* - Minimum-area bounding rectangle
*/
public final class RotatingCalipers {
private RotatingCalipers() {
}
// -------------------- Inner Classes --------------------
public record PointPair(Point p1, Point p2, double distance) {
}
public record Rectangle(Point[] corners, double width, double height, double area) {
}
// -------------------- Diameter --------------------
public static PointPair diameter(List<Point> points) {
if (points == null || points.size() < 2) {
throw new IllegalArgumentException("At least two points required for diameter");
}
List<Point> hull = ConvexHull.convexHullRecursive(points);
orderCounterClockwise(hull);
double maxDist = 0;
Point bestA = hull.get(0);
Point bestB = hull.get(0);
int n = hull.size();
int j = 1;
for (Point a : hull) {
while (true) {
Point b1 = hull.get(j);
Point b2 = hull.get((j + 1) % n);
double d1 = distanceSquared(a, b1);
double d2 = distanceSquared(a, b2);
if (d2 > d1) {
j = (j + 1) % n;
} else {
break;
}
}
double d = distanceSquared(a, hull.get(j));
if (d > maxDist) {
maxDist = d;
bestA = a;
bestB = hull.get(j);
}
}
return new PointPair(bestA, bestB, Math.sqrt(maxDist));
}
// -------------------- Width --------------------
public static double width(List<Point> points) {
if (points == null || points.size() < 3) {
throw new IllegalArgumentException("At least three points required for width");
}
List<Point> hull = ConvexHull.convexHullRecursive(points);
orderCounterClockwise(hull);
double minWidth = Double.MAX_VALUE;
int n = hull.size();
for (int i = 0; i < n; i++) {
Point a = hull.get(i);
Point b = hull.get((i + 1) % n);
double ux = b.x() - a.x();
double uy = b.y() - a.y();
double len = Math.hypot(ux, uy);
ux /= len;
uy /= len;
double vx = -uy;
double vy = ux;
double minProjV = Double.MAX_VALUE;
double maxProjV = -Double.MAX_VALUE;
for (Point p : hull) {
double projV = p.x() * vx + p.y() * vy;
minProjV = Math.min(minProjV, projV);
maxProjV = Math.max(maxProjV, projV);
}
minWidth = Math.min(minWidth, maxProjV - minProjV);
}
return minWidth;
}
// -------------------- Minimum-Area Bounding Rectangle --------------------
public static Rectangle minAreaBoundingRectangle(List<Point> points) {
if (points == null || points.size() < 3) {
throw new IllegalArgumentException("At least three points required");
}
List<Point> hull = ConvexHull.convexHullRecursive(points);
orderCounterClockwise(hull);
double minArea = Double.MAX_VALUE;
Point[] bestCorners = null;
double bestWidth = 0;
double bestHeight = 0;
int n = hull.size();
for (int i = 0; i < n; i++) {
Point a = hull.get(i);
Point b = hull.get((i + 1) % n);
double edgeDx = b.x() - a.x();
double edgeDy = b.y() - a.y();
double edgeLen = Math.hypot(edgeDx, edgeDy);
double ux = edgeDx / edgeLen;
double uy = edgeDy / edgeLen;
double vx = -uy;
double vy = ux;
double minU = Double.MAX_VALUE;
double maxU = -Double.MAX_VALUE;
double minV = Double.MAX_VALUE;
double maxV = -Double.MAX_VALUE;
for (Point p : hull) {
double projU = p.x() * ux + p.y() * uy;
double projV = p.x() * vx + p.y() * vy;
if (projU < minU) {
minU = projU;
}
if (projU > maxU) {
maxU = projU;
}
if (projV < minV) {
minV = projV;
}
if (projV > maxV) {
maxV = projV;
}
}
double width = maxU - minU;
double height = maxV - minV;
double area = width * height;
if (area < minArea) {
minArea = area;
bestWidth = width;
bestHeight = height;
bestCorners = new Point[] {new Point((int) (ux * minU + vx * minV), (int) (uy * minU + vy * minV)), new Point((int) (ux * maxU + vx * minV), (int) (uy * maxU + vy * minV)), new Point((int) (ux * maxU + vx * maxV), (int) (uy * maxU + vy * maxV)),
new Point((int) (ux * minU + vx * maxV), (int) (uy * minU + vy * maxV))};
}
}
return new Rectangle(bestCorners, bestWidth, bestHeight, minArea);
}
// -------------------- Helper Methods --------------------
private static void orderCounterClockwise(List<Point> points) {
double area = 0.0;
int n = points.size();
for (int i = 0; i < n; i++) {
Point a = points.get(i);
Point b = points.get((i + 1) % n);
area += (a.x() * b.y()) - (b.x() * a.y());
}
if (area < 0) {
Collections.reverse(points);
}
}
private static double distanceSquared(Point a, Point b) {
double dx = a.x() - b.x();
double dy = a.y() - b.y();
return dx * dx + dy * dy;
}
}