Skip to content

Commit 34994ed

Browse files
committed
feat: added rotating calipers
1 parent 41dc1b3 commit 34994ed

File tree

2 files changed

+22
-96
lines changed

2 files changed

+22
-96
lines changed

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

Lines changed: 4 additions & 10 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) {
@@ -160,12 +158,8 @@ public static Rectangle minAreaBoundingRectangle(List<Point> points) {
160158
minArea = area;
161159
bestWidth = width;
162160
bestHeight = height;
163-
bestCorners = new Point[] {
164-
new Point((int) (ux * minU + vx * minV), (int) (uy * minU + vy * minV)),
165-
new Point((int) (ux * maxU + vx * minV), (int) (uy * maxU + vy * minV)),
166-
new Point((int) (ux * maxU + vx * maxV), (int) (uy * maxU + vy * maxV)),
167-
new Point((int) (ux * minU + vx * maxV), (int) (uy * minU + vy * maxV))
168-
};
161+
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)),
162+
new Point((int) (ux * minU + vx * maxV), (int) (uy * minU + vy * maxV))};
169163
}
170164
}
171165

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

Lines changed: 18 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
package com.thealgorithms.geometry;
22

3-
import java.util.Arrays;
4-
import java.util.List;
5-
63
import static org.junit.jupiter.api.Assertions.assertEquals;
74
import static org.junit.jupiter.api.Assertions.assertNotNull;
85
import static org.junit.jupiter.api.Assertions.assertThrows;
96

7+
import java.util.Arrays;
8+
import java.util.List;
109
import org.junit.jupiter.api.Test;
1110

11+
1212
public class RotatingCalipersTest {
1313

1414
@Test
1515
void testDiameterSimpleTriangle() {
16-
List<Point> convexHull = Arrays.asList(
17-
new Point(0, 0),
18-
new Point(4, 0),
19-
new Point(2, 3)
20-
);
16+
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(4, 0), new Point(2, 3));
2117
RotatingCalipers.PointPair result = RotatingCalipers.diameter(convexHull);
2218

2319
assertNotNull(result);
@@ -26,12 +22,7 @@ void testDiameterSimpleTriangle() {
2622

2723
@Test
2824
void testDiameterSquare() {
29-
List<Point> convexHull = Arrays.asList(
30-
new Point(0, 0),
31-
new Point(3, 0),
32-
new Point(3, 3),
33-
new Point(0, 3)
34-
);
25+
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(3, 0), new Point(3, 3), new Point(0, 3));
3526
RotatingCalipers.PointPair result = RotatingCalipers.diameter(convexHull);
3627

3728
assertNotNull(result);
@@ -40,12 +31,7 @@ void testDiameterSquare() {
4031

4132
@Test
4233
void testDiameterComplexPolygon() {
43-
List<Point> convexHull = Arrays.asList(
44-
new Point(0, 0),
45-
new Point(3, 0),
46-
new Point(3, 3),
47-
new Point(0, 3)
48-
);
34+
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(3, 0), new Point(3, 3), new Point(0, 3));
4935
RotatingCalipers.PointPair result = RotatingCalipers.diameter(convexHull);
5036

5137
assertNotNull(result);
@@ -54,10 +40,7 @@ void testDiameterComplexPolygon() {
5440

5541
@Test
5642
void testDiameterTwoPoints() {
57-
List<Point> convexHull = Arrays.asList(
58-
new Point(0, 0),
59-
new Point(5, 0)
60-
);
43+
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(5, 0));
6144
RotatingCalipers.PointPair result = RotatingCalipers.diameter(convexHull);
6245

6346
assertNotNull(result);
@@ -75,37 +58,23 @@ void testDiameterInvalidInput() {
7558

7659
@Test
7760
void testWidthSimpleTriangle() {
78-
List<Point> convexHull = Arrays.asList(
79-
new Point(0, 0),
80-
new Point(4, 0),
81-
new Point(2, 3)
82-
);
61+
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(4, 0), new Point(2, 3));
8362
double result = RotatingCalipers.width(convexHull);
8463

8564
assertEquals(2.4, result, 0.1);
8665
}
8766

8867
@Test
8968
void testWidthSquare() {
90-
List<Point> convexHull = Arrays.asList(
91-
new Point(0, 0),
92-
new Point(3, 0),
93-
new Point(3, 3),
94-
new Point(0, 3)
95-
);
69+
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(3, 0), new Point(3, 3), new Point(0, 3));
9670
double result = RotatingCalipers.width(convexHull);
9771

9872
assertEquals(3.0, result, 0.001);
9973
}
10074

10175
@Test
10276
void testWidthRectangle() {
103-
List<Point> convexHull = Arrays.asList(
104-
new Point(0, 0),
105-
new Point(5, 0),
106-
new Point(5, 2),
107-
new Point(0, 2)
108-
);
77+
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(5, 0), new Point(5, 2), new Point(0, 2));
10978
double result = RotatingCalipers.width(convexHull);
11079

11180
assertEquals(2.0, result, 0.001);
@@ -115,20 +84,12 @@ void testWidthRectangle() {
11584
void testWidthInvalidInput() {
11685
assertThrows(IllegalArgumentException.class, () -> RotatingCalipers.width(null));
11786
assertThrows(IllegalArgumentException.class, () -> RotatingCalipers.width(Arrays.asList()));
118-
assertThrows(IllegalArgumentException.class, () -> RotatingCalipers.width(Arrays.asList(
119-
new Point(0, 0),
120-
new Point(1, 1)
121-
)));
87+
assertThrows(IllegalArgumentException.class, () -> RotatingCalipers.width(Arrays.asList(new Point(0, 0), new Point(1, 1))));
12288
}
12389

12490
@Test
12591
void testMinAreaBoundingRectangleSquare() {
126-
List<Point> convexHull = Arrays.asList(
127-
new Point(0, 0),
128-
new Point(3, 0),
129-
new Point(3, 3),
130-
new Point(0, 3)
131-
);
92+
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(3, 0), new Point(3, 3), new Point(0, 3));
13293
RotatingCalipers.Rectangle result = RotatingCalipers.minAreaBoundingRectangle(convexHull);
13394

13495
assertNotNull(result);
@@ -139,11 +100,7 @@ void testMinAreaBoundingRectangleSquare() {
139100

140101
@Test
141102
void testMinAreaBoundingRectangleTriangle() {
142-
List<Point> convexHull = Arrays.asList(
143-
new Point(0, 0),
144-
new Point(4, 0),
145-
new Point(2, 3)
146-
);
103+
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(4, 0), new Point(2, 3));
147104
RotatingCalipers.Rectangle result = RotatingCalipers.minAreaBoundingRectangle(convexHull);
148105

149106
assertNotNull(result);
@@ -153,12 +110,7 @@ void testMinAreaBoundingRectangleTriangle() {
153110

154111
@Test
155112
void testMinAreaBoundingRectangleRectangle() {
156-
List<Point> convexHull = Arrays.asList(
157-
new Point(0, 0),
158-
new Point(5, 0),
159-
new Point(5, 2),
160-
new Point(0, 2)
161-
);
113+
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(5, 0), new Point(5, 2), new Point(0, 2));
162114
RotatingCalipers.Rectangle result = RotatingCalipers.minAreaBoundingRectangle(convexHull);
163115

164116
assertNotNull(result);
@@ -169,48 +121,28 @@ void testMinAreaBoundingRectangleRectangle() {
169121
void testMinAreaBoundingRectangleInvalidInput() {
170122
assertThrows(IllegalArgumentException.class, () -> RotatingCalipers.minAreaBoundingRectangle(null));
171123
assertThrows(IllegalArgumentException.class, () -> RotatingCalipers.minAreaBoundingRectangle(Arrays.asList()));
172-
assertThrows(IllegalArgumentException.class, () -> RotatingCalipers.minAreaBoundingRectangle(Arrays.asList(
173-
new Point(0, 0),
174-
new Point(1, 1)
175-
)));
124+
assertThrows(IllegalArgumentException.class, () -> RotatingCalipers.minAreaBoundingRectangle(Arrays.asList(new Point(0, 0), new Point(1, 1))));
176125
}
177126

178127
@Test
179128
void testDiameterWithLargeConvexHull() {
180-
List<Point> convexHull = Arrays.asList(
181-
new Point(0, 0),
182-
new Point(3, 0),
183-
new Point(3, 3),
184-
new Point(0, 3),
185-
new Point(2, -4),
186-
new Point(1, -3)
187-
);
129+
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(3, 0), new Point(3, 3), new Point(0, 3), new Point(2, -4), new Point(1, -3));
188130
RotatingCalipers.PointPair result = RotatingCalipers.diameter(convexHull);
189131

190132
assertNotNull(result);
191133
}
192134

193135
@Test
194136
void testWidthWithLargeConvexHull() {
195-
List<Point> convexHull = Arrays.asList(
196-
new Point(0, 0),
197-
new Point(3, 0),
198-
new Point(3, 3),
199-
new Point(0, 3)
200-
);
137+
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(3, 0), new Point(3, 3), new Point(0, 3));
201138
double result = RotatingCalipers.width(convexHull);
202139

203140
assertEquals(3.0, result, 0.001);
204141
}
205142

206143
@Test
207144
void testMinAreaBoundingRectangleWithLargeConvexHull() {
208-
List<Point> convexHull = Arrays.asList(
209-
new Point(0, 0),
210-
new Point(10, 0),
211-
new Point(10, 5),
212-
new Point(0, 5)
213-
);
145+
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(10, 0), new Point(10, 5), new Point(0, 5));
214146
RotatingCalipers.Rectangle result = RotatingCalipers.minAreaBoundingRectangle(convexHull);
215147

216148
assertNotNull(result);

0 commit comments

Comments
 (0)