|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.Comparator; |
| 3 | +import java.util.List; |
| 4 | +import java.util.stream.Collectors; |
| 5 | + |
| 6 | +public class GrahamScan { |
| 7 | + |
| 8 | + static class Point { |
| 9 | + public double x; |
| 10 | + public double y; |
| 11 | + |
| 12 | + public Point(double x, double y) { |
| 13 | + this.x = x; |
| 14 | + this.y = y; |
| 15 | + } |
| 16 | + |
| 17 | + @Override |
| 18 | + public boolean equals(Object o) { |
| 19 | + if (o == null) return false; |
| 20 | + if (o == this) return true; |
| 21 | + if (!(o instanceof Point)) return false; |
| 22 | + Point p = (Point)o; |
| 23 | + return p.x == this.x && p.y == this.y; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + static double ccw(Point a, Point b, Point c) { |
| 28 | + return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); |
| 29 | + } |
| 30 | + |
| 31 | + static double polarAngle(Point origin, Point p) { |
| 32 | + return Math.atan2(p.y - origin.y, p.x - origin.x); |
| 33 | + } |
| 34 | + |
| 35 | + static List<Point> grahamScan(List<Point> gift) { |
| 36 | + gift = gift.stream() |
| 37 | + .distinct() |
| 38 | + .sorted(Comparator.comparingDouble(point -> -point.y)) |
| 39 | + .collect(Collectors.toList()); |
| 40 | + |
| 41 | + Point pivot = gift.get(0); |
| 42 | + |
| 43 | + // Sort the remaining Points based on the angle between the pivot and itself |
| 44 | + List<Point> hull = gift.subList(1, gift.size()); |
| 45 | + hull.sort(Comparator.comparingDouble(point -> polarAngle(point, pivot))); |
| 46 | + |
| 47 | + // The pivot is always on the hull |
| 48 | + hull.add(0, pivot); |
| 49 | + |
| 50 | + int n = hull.size(); |
| 51 | + int m = 1; |
| 52 | + |
| 53 | + for (int i = 2; i < n; i++) { |
| 54 | + while (ccw(hull.get(m - 1), hull.get(m), hull.get(i)) <= 0) { |
| 55 | + if (m > 1) { |
| 56 | + m--; |
| 57 | + } else if (m == 1) { |
| 58 | + break; |
| 59 | + } else { |
| 60 | + i++; |
| 61 | + } |
| 62 | + } |
| 63 | + m++; |
| 64 | + |
| 65 | + Point temp = hull.get(i); |
| 66 | + hull.set(i, hull.get(m)); |
| 67 | + hull.set(m, temp); |
| 68 | + } |
| 69 | + return hull.subList(0, m + 1); |
| 70 | + } |
| 71 | + |
| 72 | + public static void main(String[] args) { |
| 73 | + ArrayList<Point> points = new ArrayList<>(); |
| 74 | + |
| 75 | + points.add(new Point(-5, 2)); |
| 76 | + points.add(new Point(5, 7)); |
| 77 | + points.add(new Point(-6, -12)); |
| 78 | + points.add(new Point(-14, -14)); |
| 79 | + points.add(new Point(9, 9)); |
| 80 | + points.add(new Point(-1, -1)); |
| 81 | + points.add(new Point(-10, 11)); |
| 82 | + points.add(new Point(-6, 15)); |
| 83 | + points.add(new Point(-6, -8)); |
| 84 | + points.add(new Point(15, -9)); |
| 85 | + points.add(new Point(7, -7)); |
| 86 | + points.add(new Point(-2, -9)); |
| 87 | + points.add(new Point(6, -5)); |
| 88 | + points.add(new Point(0, 14)); |
| 89 | + points.add(new Point(2, 8)); |
| 90 | + |
| 91 | + List<Point> convexHull = grahamScan(points); |
| 92 | + |
| 93 | + convexHull.forEach(p -> System.out.printf("% 1.0f, % 1.0f\n", p.x, p.y)); |
| 94 | + } |
| 95 | +} |
0 commit comments