|
1 | 1 | package com.thealgorithms.geometry; |
2 | 2 |
|
3 | | -import static org.junit.jupiter.api.Assertions.*; |
4 | | - |
5 | 3 | import java.util.Arrays; |
6 | 4 | import java.util.List; |
| 5 | + |
| 6 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 8 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 9 | + |
7 | 10 | import org.junit.jupiter.api.Test; |
8 | 11 |
|
9 | 12 | public class RotatingCalipersTest { |
10 | 13 |
|
11 | 14 | @Test |
12 | 15 | void testDiameterSimpleTriangle() { |
13 | | - List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(4, 0), new Point(2, 3)); |
| 16 | + List<Point> convexHull = Arrays.asList( |
| 17 | + new Point(0, 0), |
| 18 | + new Point(4, 0), |
| 19 | + new Point(2, 3) |
| 20 | + ); |
14 | 21 | RotatingCalipers.PointPair result = RotatingCalipers.diameter(convexHull); |
15 | 22 |
|
16 | 23 | assertNotNull(result); |
17 | | - assertEquals(4.0, result.distance(), 0.001); |
| 24 | + assertEquals(5.0, result.distance(), 0.001); |
18 | 25 | } |
19 | 26 |
|
20 | 27 | @Test |
21 | 28 | void testDiameterSquare() { |
22 | | - List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(3, 0), new Point(3, 3), new Point(0, 3)); |
| 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 | + ); |
23 | 35 | RotatingCalipers.PointPair result = RotatingCalipers.diameter(convexHull); |
24 | 36 |
|
25 | 37 | assertNotNull(result); |
26 | 38 | assertEquals(Math.sqrt(18), result.distance(), 0.001); |
27 | 39 | } |
28 | 40 |
|
| 41 | + @Test |
| 42 | + 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 | + ); |
| 49 | + RotatingCalipers.PointPair result = RotatingCalipers.diameter(convexHull); |
| 50 | + |
| 51 | + assertNotNull(result); |
| 52 | + assertEquals(Math.sqrt(18), result.distance(), 0.001); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void testDiameterTwoPoints() { |
| 57 | + List<Point> convexHull = Arrays.asList( |
| 58 | + new Point(0, 0), |
| 59 | + new Point(5, 0) |
| 60 | + ); |
| 61 | + RotatingCalipers.PointPair result = RotatingCalipers.diameter(convexHull); |
| 62 | + |
| 63 | + assertNotNull(result); |
| 64 | + assertEquals(5.0, result.distance(), 0.001); |
| 65 | + assertEquals(new Point(0, 0), result.p1()); |
| 66 | + assertEquals(new Point(5, 0), result.p2()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void testDiameterInvalidInput() { |
| 71 | + assertThrows(IllegalArgumentException.class, () -> RotatingCalipers.diameter(null)); |
| 72 | + assertThrows(IllegalArgumentException.class, () -> RotatingCalipers.diameter(Arrays.asList())); |
| 73 | + assertThrows(IllegalArgumentException.class, () -> RotatingCalipers.diameter(Arrays.asList(new Point(0, 0)))); |
| 74 | + } |
| 75 | + |
29 | 76 | @Test |
30 | 77 | void testWidthSimpleTriangle() { |
31 | | - List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(4, 0), new Point(2, 3)); |
| 78 | + List<Point> convexHull = Arrays.asList( |
| 79 | + new Point(0, 0), |
| 80 | + new Point(4, 0), |
| 81 | + new Point(2, 3) |
| 82 | + ); |
32 | 83 | double result = RotatingCalipers.width(convexHull); |
33 | 84 |
|
34 | | - // Updated expected width based on correct projection |
35 | | - assertEquals(3, result, 0.1); |
| 85 | + assertEquals(2.4, result, 0.1); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + 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 | + ); |
| 96 | + double result = RotatingCalipers.width(convexHull); |
| 97 | + |
| 98 | + assertEquals(3.0, result, 0.001); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + 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 | + ); |
| 109 | + double result = RotatingCalipers.width(convexHull); |
| 110 | + |
| 111 | + assertEquals(2.0, result, 0.001); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void testWidthInvalidInput() { |
| 116 | + assertThrows(IllegalArgumentException.class, () -> RotatingCalipers.width(null)); |
| 117 | + 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 | + ))); |
36 | 122 | } |
37 | 123 |
|
38 | 124 | @Test |
39 | 125 | void testMinAreaBoundingRectangleSquare() { |
40 | | - List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(3, 0), new Point(3, 3), new Point(0, 3)); |
| 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 | + ); |
41 | 132 | RotatingCalipers.Rectangle result = RotatingCalipers.minAreaBoundingRectangle(convexHull); |
42 | 133 |
|
43 | 134 | assertNotNull(result); |
44 | 135 | assertEquals(9.0, result.area(), 0.1); |
45 | 136 | assertEquals(3.0, result.width(), 0.1); |
46 | 137 | assertEquals(3.0, result.height(), 0.1); |
47 | | - |
48 | | - // Check corners are PointD and not null |
49 | | - for (RotatingCalipers.PointD corner : result.corners()) { |
50 | | - assertNotNull(corner); |
51 | | - } |
52 | 138 | } |
53 | 139 |
|
54 | 140 | @Test |
55 | 141 | void testMinAreaBoundingRectangleTriangle() { |
56 | | - List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(4, 0), new Point(2, 3)); |
| 142 | + List<Point> convexHull = Arrays.asList( |
| 143 | + new Point(0, 0), |
| 144 | + new Point(4, 0), |
| 145 | + new Point(2, 3) |
| 146 | + ); |
57 | 147 | RotatingCalipers.Rectangle result = RotatingCalipers.minAreaBoundingRectangle(convexHull); |
58 | 148 |
|
59 | 149 | assertNotNull(result); |
| 150 | + assertNotNull(result.corners()); |
60 | 151 | assertEquals(4, result.corners().length); |
61 | 152 | } |
62 | 153 |
|
| 154 | + @Test |
| 155 | + 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 | + ); |
| 162 | + RotatingCalipers.Rectangle result = RotatingCalipers.minAreaBoundingRectangle(convexHull); |
| 163 | + |
| 164 | + assertNotNull(result); |
| 165 | + assertEquals(10.0, result.area(), 0.1); |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + void testMinAreaBoundingRectangleInvalidInput() { |
| 170 | + assertThrows(IllegalArgumentException.class, () -> RotatingCalipers.minAreaBoundingRectangle(null)); |
| 171 | + 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 | + ))); |
| 176 | + } |
| 177 | + |
63 | 178 | @Test |
64 | 179 | void testDiameterWithLargeConvexHull() { |
65 | | - 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)); |
| 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 | + ); |
66 | 188 | RotatingCalipers.PointPair result = RotatingCalipers.diameter(convexHull); |
67 | 189 |
|
68 | 190 | assertNotNull(result); |
69 | 191 | } |
70 | 192 |
|
71 | 193 | @Test |
72 | 194 | void testWidthWithLargeConvexHull() { |
73 | | - List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(3, 0), new Point(3, 3), new Point(0, 3)); |
| 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 | + ); |
74 | 201 | double result = RotatingCalipers.width(convexHull); |
75 | 202 |
|
76 | 203 | assertEquals(3.0, result, 0.001); |
77 | 204 | } |
78 | 205 |
|
79 | 206 | @Test |
80 | 207 | void testMinAreaBoundingRectangleWithLargeConvexHull() { |
81 | | - List<Point> convexHull = Arrays.asList(new Point(0, 0), new Point(10, 0), new Point(10, 5), new Point(0, 5)); |
| 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 | + ); |
82 | 214 | RotatingCalipers.Rectangle result = RotatingCalipers.minAreaBoundingRectangle(convexHull); |
83 | 215 |
|
84 | 216 | assertNotNull(result); |
|
0 commit comments