Skip to content

Commit a39d18f

Browse files
committed
feat: added rotating calipers
1 parent 5559bf8 commit a39d18f

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

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

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ private RotatingCalipers() {
1515
}
1616

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

21-
public record Rectangle(Point[] corners, double width, double height, double area) {
21+
public static record Rectangle(Point[] corners, double width, double height, double area) {
2222
}
2323

2424
// -------------------- Diameter --------------------
@@ -86,19 +86,13 @@ public static double width(List<Point> points) {
8686

8787
double minProjV = Double.MAX_VALUE;
8888
double maxProjV = -Double.MAX_VALUE;
89+
8990
for (Point p : hull) {
9091
double projV = p.x() * vx + p.y() * vy;
91-
if (projV < minProjV) {
92-
minProjV = projV;
93-
}
94-
if (projV > maxProjV) {
95-
maxProjV = projV;
96-
}
97-
}
98-
double width = maxProjV - minProjV;
99-
if (width < minWidth) {
100-
minWidth = width;
92+
minProjV = Math.min(minProjV, projV);
93+
maxProjV = Math.max(maxProjV, projV);
10194
}
95+
minWidth = Math.min(minWidth, maxProjV - minProjV);
10296
}
10397
return minWidth;
10498
}
@@ -138,18 +132,10 @@ public static Rectangle minAreaBoundingRectangle(List<Point> points) {
138132
for (Point p : hull) {
139133
double projU = p.x() * ux + p.y() * uy;
140134
double projV = p.x() * vx + p.y() * vy;
141-
if (projU < minU) {
142-
minU = projU;
143-
}
144-
if (projU > maxU) {
145-
maxU = projU;
146-
}
147-
if (projV < minV) {
148-
minV = projV;
149-
}
150-
if (projV > maxV) {
151-
maxV = projV;
152-
}
135+
minU = Math.min(minU, projU);
136+
maxU = Math.max(maxU, projU);
137+
minV = Math.min(minV, projV);
138+
maxV = Math.max(maxV, projV);
153139
}
154140

155141
double width = maxU - minU;
@@ -160,8 +146,12 @@ public static Rectangle minAreaBoundingRectangle(List<Point> points) {
160146
minArea = area;
161147
bestWidth = width;
162148
bestHeight = height;
163-
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)),
164-
new Point((int) (ux * minU + vx * maxV), (int) (uy * minU + vy * maxV))};
149+
bestCorners = new Point[] {
150+
new Point((int)(ux * minU + vx * minV), (int)(uy * minU + vy * minV)),
151+
new Point((int)(ux * maxU + vx * minV), (int)(uy * maxU + vy * minV)),
152+
new Point((int)(ux * maxU + vx * maxV), (int)(uy * maxU + vy * maxV)),
153+
new Point((int)(ux * minU + vx * maxV), (int)(uy * minU + vy * maxV))
154+
};
165155
}
166156
}
167157

0 commit comments

Comments
 (0)