Skip to content

Commit e4729bf

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

2 files changed

Lines changed: 120 additions & 24 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: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,121 @@
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+
12+
@Test
13+
public void findAndrewConvexHull_WhenOnePoint_ThenEmpty()
14+
{
15+
// when
16+
List<Point2D> result = ConvexHull.findAndrewConvexHull(List.of(Point2D.of(3.0, 2.0)));
17+
18+
// then
19+
Assertions.assertThat(result).isEmpty();
20+
}
21+
22+
@Test
23+
public void findAndrewConvexHull_WhenTwoPoints_ThenEmpty()
24+
{
25+
// when
26+
List<Point2D> result = ConvexHull.findAndrewConvexHull(
27+
List.of(Point2D.of(2.0, 3.0), Point2D.of(3.0, 2.0)));
28+
29+
// then
30+
Assertions.assertThat(result).isEmpty();
31+
}
32+
33+
@Test
34+
public void findAndrewConvexHull_WhenThreePoints_ThenThesePointsInHull()
35+
{
36+
// given
37+
List<Point2D> points =
38+
List.of(Point2D.of(1.0, -1.0), Point2D.of(5.0, 1.0), Point2D.of(3.0, 4.0));
39+
40+
// when
41+
List<Point2D> result = ConvexHull.findAndrewConvexHull(points);
42+
43+
// then
44+
Assertions.assertThat(result).isEqualTo(points);
45+
}
46+
47+
@Test
48+
public void findAndrewConvexHull_ThenPointsInHull()
49+
{
50+
// when
51+
List<Point2D> result = ConvexHull.findAndrewConvexHull(
52+
List.of(Point2D.of(1, -3), Point2D.of(-4, 6), Point2D.of(-5, -7),
53+
Point2D.of(-8, -7), Point2D.of(-3, -4), Point2D.of(5, 9),
54+
Point2D.of(-1, -8), Point2D.of(-5, 10), Point2D.of(8, 0), Point2D.of(3, -6),
55+
Point2D.of(-2, 1), Point2D.of(-2, 8), Point2D.of(10, 2), Point2D.of(6, 3),
56+
Point2D.of(-7, 7), Point2D.of(6, -4)));
57+
58+
// then
59+
Assertions.assertThat(result)
60+
.containsExactly(Point2D.of(-8, -7), 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));
63+
}
64+
1065
@Test
11-
public void findConvexHull_WhenOnePoint_ThenEmpty()
66+
public void findAndrewConvexHull_WhenMultiplePointsAreCollinear_ThenInnerPointsOmitted()
1267
{
1368
// when
14-
List<Point2D> result = ConvexHull.findConvexHull(List.of(Point2D.of(3.0, 2.0)));
69+
List<Point2D> result = ConvexHull.findAndrewConvexHull(
70+
List.of(Point2D.of(-1, -3), Point2D.of(-3, -3), Point2D.of(-3, -1),
71+
Point2D.of(2, -3), Point2D.of(-3, 5), Point2D.of(0, -3), Point2D.of(7, -3),
72+
Point2D.of(-3, -2)));
73+
74+
// then
75+
Assertions.assertThat(result)
76+
.containsExactly(Point2D.of(-3, -3), Point2D.of(7, -3), Point2D.of(-3, 5));
77+
}
78+
79+
// endregion
80+
// region findGrahamConvexHull
81+
82+
@Test
83+
public void findGrahamConvexHull_WhenOnePoint_ThenEmpty()
84+
{
85+
// when
86+
List<Point2D> result = ConvexHull.findGrahamConvexHull(List.of(Point2D.of(3.0, 2.0)));
1587

1688
// then
1789
Assertions.assertThat(result).isEmpty();
1890
}
1991

2092
@Test
21-
public void findConvexHull_WhenTwoPoints_ThenEmpty()
93+
public void findGrahamConvexHull_WhenTwoPoints_ThenEmpty()
2294
{
2395
// when
24-
List<Point2D> result =
25-
ConvexHull.findConvexHull(List.of(Point2D.of(2.0, 3.0), Point2D.of(3.0, 2.0)));
96+
List<Point2D> result = ConvexHull.findGrahamConvexHull(
97+
List.of(Point2D.of(2.0, 3.0), Point2D.of(3.0, 2.0)));
2698

2799
// then
28100
Assertions.assertThat(result).isEmpty();
29101
}
30102

31103
@Test
32-
public void findConvexHull_WhenThreePoints_ThenThesePointsInHull()
104+
public void findGrahamConvexHull_WhenThreePoints_ThenThesePointsInHull()
33105
{
34106
// given
35107
List<Point2D> points =
36108
List.of(Point2D.of(1.0, -1.0), Point2D.of(5.0, 1.0), Point2D.of(3.0, 4.0));
37109

38110
// when
39-
List<Point2D> result = ConvexHull.findConvexHull(points);
111+
List<Point2D> result = ConvexHull.findGrahamConvexHull(points);
40112

41113
// then
42114
Assertions.assertThat(result).isEqualTo(points);
43115
}
44116

45117
@Test
46-
public void findConvexHull_ThenPointsInHull()
118+
public void findGrahamConvexHull_ThenPointsInHull()
47119
{
48120
// when
49-
List<Point2D> result = ConvexHull.findConvexHull(
121+
List<Point2D> result = ConvexHull.findGrahamConvexHull(
50122
List.of(Point2D.of(1, -3), Point2D.of(-4, 6), Point2D.of(-5, -7),
51123
Point2D.of(-8, -7), Point2D.of(-3, -4), Point2D.of(5, 9),
52124
Point2D.of(-1, -8), Point2D.of(-5, 10), Point2D.of(8, 0), Point2D.of(3, -6),
@@ -61,10 +133,10 @@ public void findConvexHull_ThenPointsInHull()
61133
}
62134

63135
@Test
64-
public void findConvexHull_WhenMultiplePointsAreCollinear_ThenInnerPointsOmitted()
136+
public void findGrahamConvexHull_WhenMultiplePointsAreCollinear_ThenInnerPointsOmitted()
65137
{
66138
// when
67-
List<Point2D> result = ConvexHull.findConvexHull(
139+
List<Point2D> result = ConvexHull.findGrahamConvexHull(
68140
List.of(Point2D.of(-1, -3), Point2D.of(-3, -3), Point2D.of(-3, -1),
69141
Point2D.of(2, -3), Point2D.of(-3, 5), Point2D.of(0, -3), Point2D.of(7, -3),
70142
Point2D.of(-3, -2)));
@@ -73,4 +145,6 @@ public void findConvexHull_WhenMultiplePointsAreCollinear_ThenInnerPointsOmitted
73145
Assertions.assertThat(result)
74146
.containsExactly(Point2D.of(-3, -3), Point2D.of(7, -3), Point2D.of(-3, 5));
75147
}
148+
149+
// endregion
76150
}

0 commit comments

Comments
 (0)