Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 15 additions & 23 deletions src/main/java/algolib/geometry/dim2/ConvexHull.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package algolib.geometry.dim2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/** Algorithm for convex hull in 2D (monotone chain). */
/** Algorithm for convex hull in 2D (Graham's scan). */
public final class ConvexHull
{
/**
Expand All @@ -17,38 +18,29 @@ public static List<Point2D> findConvexHull(List<Point2D> points)
if(points.size() < 3)
return List.of();

List<Point2D> sorted = new ArrayList<>(points);
Point2D minPoint = points.stream()
.min(Comparator.comparingDouble((Point2D pt) -> pt.y)
.thenComparingDouble(pt -> pt.x))
.orElseThrow();
Vector2D moving = Vector2D.between(Point2D.of(0, 0), minPoint);
List<Point2D> anglePoints = points.stream()
.map(pt -> Geometry2D.translate(pt, moving.negate()))
.collect(Collectors.toCollection(ArrayList::new));

Geometry2D.sortByX(sorted);
Geometry2D.sortByAngle(anglePoints);

List<Point2D> lowerHull = createHalfHull(sorted);

Collections.reverse(sorted);

List<Point2D> upperHull = createHalfHull(sorted);

lowerHull.remove(lowerHull.size() - 1);
upperHull.remove(upperHull.size() - 1);
lowerHull.addAll(upperHull);
return lowerHull;
}

// Computes half of convex hull for given points.
private static List<Point2D> createHalfHull(List<Point2D> points)
{
List<Point2D> hull = new ArrayList<>();

for(Point2D pt : points)
for(Point2D pt : anglePoints)
{
while(hull.size() > 1
&& crossProduct(hull.get(hull.size() - 2), hull.get(hull.size() - 1), pt)
>= 0)
&& crossProduct(hull.get(hull.size() - 2), hull.get(hull.size() - 1), pt) >= 0)
hull.remove(hull.size() - 1);

hull.add(pt);
}

return hull;
return hull.stream().map(pt -> Geometry2D.translate(pt, moving)).toList();
}

private static double crossProduct(Point2D pt1, Point2D pt2, Point2D pt3)
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/algolib/geometry/dim2/ConvexHullTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

// Tests: Algorithm for convex hull in 2D (monotone chain).
// Tests: Algorithm for convex hull in 2D (Graham's scan).
public class ConvexHullTest
{
@Test
Expand Down Expand Up @@ -55,9 +55,9 @@ public void findConvexHull_ThenPointsInHull()

// then
Assertions.assertThat(result)
.containsExactly(Point2D.of(-8, -7), Point2D.of(-1, -8), Point2D.of(3, -6),
Point2D.of(6, -4), Point2D.of(10, 2), Point2D.of(5, 9),
Point2D.of(-5, 10), Point2D.of(-7, 7));
.containsExactly(Point2D.of(-1, -8), Point2D.of(3, -6), Point2D.of(6, -4),
Point2D.of(10, 2), Point2D.of(5, 9), Point2D.of(-5, 10),
Point2D.of(-7, 7), Point2D.of(-8, -7));
}

@Test
Expand Down