44import org .assertj .core .api .Assertions ;
55import org .junit .jupiter .api .Test ;
66
7- // Tests: Algorithm for convex hull in 2D (Graham's scan) .
7+ // Tests: Algorithms for convex hull in 2D.
88public class ConvexHullTest
99{
10+ // region findAndrewConvexHull
11+
1012 @ Test
11- public void findConvexHull_WhenOnePoint_ThenEmpty ()
13+ public void findAndrewConvexHull_WhenOnePoint_ThenEmpty ()
1214 {
1315 // when
14- List <Point2D > result = ConvexHull .findConvexHull (List .of (Point2D .of (3.0 , 2.0 )));
16+ List <Point2D > result = ConvexHull .findAndrewConvexHull (List .of (Point2D .of (3.0 , 2.0 )));
1517
1618 // then
1719 Assertions .assertThat (result ).isEmpty ();
1820 }
1921
2022 @ Test
21- public void findConvexHull_WhenTwoPoints_ThenEmpty ()
23+ public void findAndrewConvexHull_WhenTwoPoints_ThenEmpty ()
2224 {
2325 // when
24- List <Point2D > result =
25- ConvexHull . findConvexHull ( List .of (Point2D .of (2.0 , 3.0 ), Point2D .of (3.0 , 2.0 )));
26+ List <Point2D > result = ConvexHull . findAndrewConvexHull (
27+ List .of (Point2D .of (2.0 , 3.0 ), Point2D .of (3.0 , 2.0 )));
2628
2729 // then
2830 Assertions .assertThat (result ).isEmpty ();
2931 }
3032
3133 @ Test
32- public void findConvexHull_WhenThreePoints_ThenThesePointsInHull ()
34+ public void findAndrewConvexHull_WhenThreePoints_ThenThesePointsInHull ()
3335 {
3436 // given
3537 List <Point2D > points =
3638 List .of (Point2D .of (1.0 , -1.0 ), Point2D .of (5.0 , 1.0 ), Point2D .of (3.0 , 4.0 ));
3739
3840 // when
39- List <Point2D > result = ConvexHull .findConvexHull (points );
41+ List <Point2D > result = ConvexHull .findAndrewConvexHull (points );
4042
4143 // then
4244 Assertions .assertThat (result ).isEqualTo (points );
4345 }
4446
4547 @ Test
46- public void findConvexHull_ThenPointsInHull ()
48+ public void findAndrewConvexHull_ThenPointsInHull ()
4749 {
4850 // when
49- List <Point2D > result = ConvexHull .findConvexHull (
51+ List <Point2D > result = ConvexHull .findAndrewConvexHull (
5052 List .of (Point2D .of (1 , -3 ), Point2D .of (-4 , 6 ), Point2D .of (-5 , -7 ),
5153 Point2D .of (-8 , -7 ), Point2D .of (-3 , -4 ), Point2D .of (5 , 9 ),
5254 Point2D .of (-1 , -8 ), Point2D .of (-5 , 10 ), Point2D .of (8 , 0 ), Point2D .of (3 , -6 ),
@@ -55,22 +57,96 @@ public void findConvexHull_ThenPointsInHull()
5557
5658 // then
5759 Assertions .assertThat (result )
58- .containsExactly (Point2D .of (-1 , -8 ), Point2D .of (3 , -6 ), Point2D . of ( 6 , - 4 ),
59- Point2D .of (10 , 2 ), Point2D .of (5 , 9 ), Point2D .of (- 5 , 10 ),
60- Point2D .of (-7 , 7 ), Point2D .of (-8 , -7 ));
60+ .containsExactlyInAnyOrder (Point2D .of (-1 , -8 ), Point2D .of (3 , -6 ),
61+ Point2D .of (6 , - 4 ), Point2D .of (10 , 2 ), Point2D .of (5 , 9 ),
62+ Point2D .of (-5 , 10 ), Point2D . of (- 7 , 7 ), Point2D .of (-8 , -7 ));
6163 }
6264
6365 @ Test
64- public void findConvexHull_WhenMultiplePointsAreCollinear_ThenInnerPointsOmitted ()
66+ public void findAndrewConvexHull_WhenMultiplePointsAreCollinear_ThenInnerPointsOmitted ()
6567 {
6668 // when
67- List <Point2D > result = ConvexHull .findConvexHull (
69+ List <Point2D > result = ConvexHull .findAndrewConvexHull (
6870 List .of (Point2D .of (-1 , -3 ), Point2D .of (-3 , -3 ), Point2D .of (-3 , -1 ),
6971 Point2D .of (2 , -3 ), Point2D .of (-3 , 5 ), Point2D .of (0 , -3 ), Point2D .of (7 , -3 ),
7072 Point2D .of (-3 , -2 )));
7173
7274 // then
7375 Assertions .assertThat (result )
74- .containsExactly (Point2D .of (-3 , -3 ), Point2D .of (7 , -3 ), Point2D .of (-3 , 5 ));
76+ .containsExactlyInAnyOrder (Point2D .of (-3 , -3 ), Point2D .of (7 , -3 ),
77+ Point2D .of (-3 , 5 ));
78+ }
79+
80+ // endregion
81+ // region findGrahamConvexHull
82+
83+ @ Test
84+ public void findGrahamConvexHull_WhenOnePoint_ThenEmpty ()
85+ {
86+ // when
87+ List <Point2D > result = ConvexHull .findGrahamConvexHull (List .of (Point2D .of (3.0 , 2.0 )));
88+
89+ // then
90+ Assertions .assertThat (result ).isEmpty ();
91+ }
92+
93+ @ Test
94+ public void findGrahamConvexHull_WhenTwoPoints_ThenEmpty ()
95+ {
96+ // when
97+ List <Point2D > result = ConvexHull .findGrahamConvexHull (
98+ List .of (Point2D .of (2.0 , 3.0 ), Point2D .of (3.0 , 2.0 )));
99+
100+ // then
101+ Assertions .assertThat (result ).isEmpty ();
75102 }
103+
104+ @ Test
105+ public void findGrahamConvexHull_WhenThreePoints_ThenThesePointsInHull ()
106+ {
107+ // given
108+ List <Point2D > points =
109+ List .of (Point2D .of (1.0 , -1.0 ), Point2D .of (5.0 , 1.0 ), Point2D .of (3.0 , 4.0 ));
110+
111+ // when
112+ List <Point2D > result = ConvexHull .findGrahamConvexHull (points );
113+
114+ // then
115+ Assertions .assertThat (result ).isEqualTo (points );
116+ }
117+
118+ @ Test
119+ public void findGrahamConvexHull_ThenPointsInHull ()
120+ {
121+ // when
122+ List <Point2D > result = ConvexHull .findGrahamConvexHull (
123+ List .of (Point2D .of (1 , -3 ), Point2D .of (-4 , 6 ), Point2D .of (-5 , -7 ),
124+ Point2D .of (-8 , -7 ), Point2D .of (-3 , -4 ), Point2D .of (5 , 9 ),
125+ Point2D .of (-1 , -8 ), Point2D .of (-5 , 10 ), Point2D .of (8 , 0 ), Point2D .of (3 , -6 ),
126+ Point2D .of (-2 , 1 ), Point2D .of (-2 , 8 ), Point2D .of (10 , 2 ), Point2D .of (6 , 3 ),
127+ Point2D .of (-7 , 7 ), Point2D .of (6 , -4 )));
128+
129+ // then
130+ Assertions .assertThat (result )
131+ .containsExactlyInAnyOrder (Point2D .of (-1 , -8 ), Point2D .of (3 , -6 ),
132+ Point2D .of (6 , -4 ), Point2D .of (10 , 2 ), Point2D .of (5 , 9 ),
133+ Point2D .of (-5 , 10 ), Point2D .of (-7 , 7 ), Point2D .of (-8 , -7 ));
134+ }
135+
136+ @ Test
137+ public void findGrahamConvexHull_WhenMultiplePointsAreCollinear_ThenInnerPointsOmitted ()
138+ {
139+ // when
140+ List <Point2D > result = ConvexHull .findGrahamConvexHull (
141+ List .of (Point2D .of (-1 , -3 ), Point2D .of (-3 , -3 ), Point2D .of (-3 , -1 ),
142+ Point2D .of (2 , -3 ), Point2D .of (-3 , 5 ), Point2D .of (0 , -3 ), Point2D .of (7 , -3 ),
143+ Point2D .of (-3 , -2 )));
144+
145+ // then
146+ Assertions .assertThat (result )
147+ .containsExactlyInAnyOrder (Point2D .of (-3 , -3 ), Point2D .of (7 , -3 ),
148+ Point2D .of (-3 , 5 ));
149+ }
150+
151+ // endregion
76152}
0 commit comments