Skip to content

Commit d844680

Browse files
committed
Convex hull algorithms
1 parent 06e3f33 commit d844680

2 files changed

Lines changed: 126 additions & 28 deletions

File tree

src/main/java/com/github/refhumbold/algolib/geometry/dim2/ConvexHull.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,36 @@
33
import java.util.ArrayList;
44
import java.util.Comparator;
55
import java.util.List;
6-
import java.util.stream.Collectors;
6+
import java.util.stream.Stream;
77

8-
/** Algorithm for convex hull in 2D (Graham's scan). */
8+
/** Algorithms for convex hull in 2D. */
99
public final class ConvexHull
1010
{
1111
/**
12-
* Computes convex hull of given points.
12+
* Computes convex hull of given points using Andrew's monotone chain.
1313
* @param points the points
1414
* @return the points in the convex hull
1515
*/
16-
public static List<Point2D> findConvexHull(List<Point2D> points)
16+
public static List<Point2D> findAndrewConvexHull(List<Point2D> points)
17+
{
18+
if(points.size() < 3)
19+
return List.of();
20+
21+
List<Point2D> sorted = Geometry2D.sortByX(points);
22+
List<Point2D> lowerHull = collectHull(sorted);
23+
List<Point2D> upperHull = collectHull(sorted.reversed());
24+
25+
lowerHull.removeLast();
26+
upperHull.removeLast();
27+
return Stream.concat(lowerHull.stream(), upperHull.stream()).toList();
28+
}
29+
30+
/**
31+
* Computes convex hull of given points using Graham's scan.
32+
* @param points the points
33+
* @return the points in the convex hull
34+
*/
35+
public static List<Point2D> findGrahamConvexHull(List<Point2D> points)
1736
{
1837
if(points.size() < 3)
1938
return List.of();
@@ -22,25 +41,28 @@ public static List<Point2D> findConvexHull(List<Point2D> points)
2241
.min(Comparator.comparingDouble((Point2D pt) -> pt.y)
2342
.thenComparingDouble(pt -> pt.x))
2443
.orElseThrow();
25-
Vector2D moving = Vector2D.between(Point2D.of(0, 0), minPoint);
26-
List<Point2D> anglePoints = points.stream()
27-
.map(pt -> Geometry2D.translate(pt, moving.negate()))
28-
.collect(Collectors.toCollection(ArrayList::new));
44+
Vector2D moving = Vector2D.between(minPoint, Point2D.ZERO);
45+
List<Point2D> anglePoints = Geometry2D.sortByAngle(
46+
points.stream().map(pt -> Geometry2D.translate(pt, moving)).toList());
2947

30-
Geometry2D.sortByAngle(anglePoints);
48+
List<Point2D> hull = collectHull(anglePoints);
3149

50+
return hull.stream().map(pt -> Geometry2D.translate(pt, moving.negate())).toList();
51+
}
52+
53+
private static List<Point2D> collectHull(List<Point2D> points)
54+
{
3255
List<Point2D> hull = new ArrayList<>();
3356

34-
for(Point2D pt : anglePoints)
57+
for(Point2D pt : points)
3558
{
3659
while(hull.size() > 1
3760
&& crossProduct(hull.get(hull.size() - 2), hull.getLast(), pt) >= 0)
3861
hull.removeLast();
3962

4063
hull.add(pt);
4164
}
42-
43-
return hull.stream().map(pt -> Geometry2D.translate(pt, moving)).toList();
65+
return hull;
4466
}
4567

4668
private static double crossProduct(Point2D pt1, Point2D pt2, Point2D pt3)

src/test/java/com/github/refhumbold/algolib/geometry/dim2/ConvexHullTest.java

Lines changed: 92 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,51 @@
44
import org.assertj.core.api.Assertions;
55
import 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.
88
public 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

Comments
 (0)