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 pathRotatingCalipersTest.java
More file actions
150 lines (118 loc) · 5.8 KB
/
RotatingCalipersTest.java
File metadata and controls
150 lines (118 loc) · 5.8 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
package com.thealgorithms.geometry;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
public class RotatingCalipersTest {
@Test
void testDiameterSimpleTriangle() {
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(4, 0), new Point(2, 3));
RotatingCalipers.PointPair result = RotatingCalipers.diameter(convexHull);
assertNotNull(result);
assertEquals(4.0, result.distance(), 0.001);
}
@Test
void testDiameterSquare() {
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(3, 0), new Point(3, 3), new Point(0, 3));
RotatingCalipers.PointPair result = RotatingCalipers.diameter(convexHull);
assertNotNull(result);
assertEquals(Math.sqrt(18), result.distance(), 0.001);
}
@Test
void testDiameterComplexPolygon() {
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(3, 0), new Point(3, 3), new Point(0, 3));
RotatingCalipers.PointPair result = RotatingCalipers.diameter(convexHull);
assertNotNull(result);
assertEquals(Math.sqrt(18), result.distance(), 0.001);
}
@Test
void testDiameterTwoPoints() {
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(5, 0));
RotatingCalipers.PointPair result = RotatingCalipers.diameter(convexHull);
assertNotNull(result);
assertEquals(5.0, result.distance(), 0.001);
assertEquals(new Point(0, 0), result.p1());
assertEquals(new Point(5, 0), result.p2());
}
@Test
void testDiameterInvalidInput() {
assertThrows(IllegalArgumentException.class, () -> RotatingCalipers.diameter(null));
assertThrows(IllegalArgumentException.class, () -> RotatingCalipers.diameter(Arrays.asList()));
assertThrows(IllegalArgumentException.class, () -> RotatingCalipers.diameter(Arrays.asList(new Point(0, 0))));
}
@Test
void testWidthSimpleTriangle() {
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(4, 0), new Point(2, 3));
double result = RotatingCalipers.width(convexHull);
assertEquals(3, result, 0.1);
}
@Test
void testWidthSquare() {
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(3, 0), new Point(3, 3), new Point(0, 3));
double result = RotatingCalipers.width(convexHull);
assertEquals(3.0, result, 0.001);
}
@Test
void testWidthRectangle() {
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(5, 0), new Point(5, 2), new Point(0, 2));
double result = RotatingCalipers.width(convexHull);
assertEquals(2.0, result, 0.001);
}
@Test
void testWidthInvalidInput() {
assertThrows(IllegalArgumentException.class, () -> RotatingCalipers.width(null));
assertThrows(IllegalArgumentException.class, () -> RotatingCalipers.width(Arrays.asList()));
assertThrows(IllegalArgumentException.class, () -> RotatingCalipers.width(Arrays.asList(new Point(0, 0), new Point(1, 1))));
}
@Test
void testMinAreaBoundingRectangleSquare() {
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(3, 0), new Point(3, 3), new Point(0, 3));
RotatingCalipers.Rectangle result = RotatingCalipers.minAreaBoundingRectangle(convexHull);
assertNotNull(result);
assertEquals(9.0, result.area(), 0.1);
assertEquals(3.0, result.width(), 0.1);
assertEquals(3.0, result.height(), 0.1);
}
@Test
void testMinAreaBoundingRectangleTriangle() {
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(4, 0), new Point(2, 3));
RotatingCalipers.Rectangle result = RotatingCalipers.minAreaBoundingRectangle(convexHull);
assertNotNull(result);
assertNotNull(result.corners());
assertEquals(4, result.corners().length);
}
@Test
void testMinAreaBoundingRectangleRectangle() {
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(5, 0), new Point(5, 2), new Point(0, 2));
RotatingCalipers.Rectangle result = RotatingCalipers.minAreaBoundingRectangle(convexHull);
assertNotNull(result);
assertEquals(10.0, result.area(), 0.1);
}
@Test
void testMinAreaBoundingRectangleInvalidInput() {
assertThrows(IllegalArgumentException.class, () -> RotatingCalipers.minAreaBoundingRectangle(null));
assertThrows(IllegalArgumentException.class, () -> RotatingCalipers.minAreaBoundingRectangle(Arrays.asList()));
assertThrows(IllegalArgumentException.class, () -> RotatingCalipers.minAreaBoundingRectangle(Arrays.asList(new Point(0, 0), new Point(1, 1))));
}
@Test
void testDiameterWithLargeConvexHull() {
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));
RotatingCalipers.PointPair result = RotatingCalipers.diameter(convexHull);
assertNotNull(result);
}
@Test
void testWidthWithLargeConvexHull() {
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(3, 0), new Point(3, 3), new Point(0, 3));
double result = RotatingCalipers.width(convexHull);
assertEquals(3.0, result, 0.001);
}
@Test
void testMinAreaBoundingRectangleWithLargeConvexHull() {
List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(10, 0), new Point(10, 5), new Point(0, 5));
RotatingCalipers.Rectangle result = RotatingCalipers.minAreaBoundingRectangle(convexHull);
assertNotNull(result);
assertEquals(50.0, result.area(), 0.1);
}
}