Skip to content

Add Graham Scan in Java #459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
109 changes: 109 additions & 0 deletions contents/graham_scan/code/java/GrahamScan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;

public class GrahamScan {

static class Point {
public double x;
public double y;

public Point(double x, double y) {
this.x = x;
this.y = y;
}

}

static double ccw(Point a, Point b, Point c) {
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}

static double polarAngle(Point origin, Point p) {
return Math.atan2(p.y - origin.y, p.x - origin.x);
}

static List<Point> grahamScan(List<Point> gift) {
gift = new ArrayList<Point>(new HashSet<Point>(gift)); // remove duplicate points

// Sort the list so the point with the lowest y-coordinate comes first
Comparator<Point> yComparator = (a, b) -> {
if (b.y - a.y < 0)
return -1;
else if (b.y - a.y > 0) {
return 1;
} else
return 0;
};

gift.sort(yComparator);
Point pivot = gift.get(0);

// Sort the remaining Points based on the angle between the pivot and itself
List<Point> hull = gift.subList(1, gift.size());

Comparator<Point> angleComparator = (a, b) -> {
double angleA = polarAngle(a, pivot);
double angleB = polarAngle(b, pivot);
if (angleA - angleB < 0)
return -1;
else if (angleA - angleB > 0)
return 1;
else
return 0;
};

hull.sort(angleComparator);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code in this method up to this point could be written as:

        gift = gift
            .stream()
            .distinct()
            .sorted(Comparator.comparingDouble(point -> -point.y))
            .collect(Collectors.toList());

        Point pivot = gift.get(0);

        // Sort the remaining Points based on the angle between the pivot and itself
        List<Point> hull = gift.subList(1, gift.size());
        hull.sort(Comparator.comparingDouble(point -> polarAngle(point, pivot)));

which I think is a bit more readable.


// The pivot is always on the hull
hull.add(0, pivot);

int n = hull.size();
int m = 1;

for (int i = 2; i < n; i++) {
while (ccw(hull.get(m - 1), hull.get(m), hull.get(i)) <= 0) {
if (m > 1) {
m--;
} else if (m == 1) {
break;
} else {
i++;
}
}
m++;

Point temp = hull.get(i);
hull.set(i, hull.get(m));
hull.set(m, temp);

}
return hull.subList(0, m + 1);
}

public static void main(String[] args) {
ArrayList<Point> points = new ArrayList<Point>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good practice to not repeat type parameters when it's not necessary:

ArrayList<Point> points = new ArrayList<>();


points.add(new Point(-5, 2));
points.add(new Point(5, 7));
points.add(new Point(-6, -12));
points.add(new Point(-14, -14));
points.add(new Point(9, 9));
points.add(new Point(-1, -1));
points.add(new Point(-10, 11));
points.add(new Point(-6, 15));
points.add(new Point(-6, -8));
points.add(new Point(15, -9));
points.add(new Point(7, -7));
points.add(new Point(-2, -9));
points.add(new Point(6, -5));
points.add(new Point(0, 14));
points.add(new Point(2, 8));

List<Point> convexHull = grahamScan(points);

convexHull.stream().forEach(p -> System.out.printf("% 1.0f, % 1.0f\n", p.x, p.y));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need the .stream() here, List<T> already has a forEach method.


}
}
6 changes: 6 additions & 0 deletions contents/graham_scan/graham_scan.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ We can find whether a rotation is counter-clockwise with trigonometric functions
[import:36-38, lang:"javascript"](code/javascript/graham-scan.js)
{% sample lang="py" %}
[import:4-6, lang:"python"](code/python/grahamScan.py)
{% sample lang="java" %}
[import:19-21, lang:"java"](code/java/GrahamScan.java)
{% endmethod %}

If the output of this function is 0, the points are collinear.
Expand All @@ -46,6 +48,8 @@ In the end, the code should look something like this:
[import:1-30, lang:"javascript"](code/javascript/graham-scan.js)
{% sample lang="py" %}
[import:14-27, lang:"python"](code/python/grahamScan.py)
{% sample lang="java" %}
[import:27-83, lang:"java"](code/java/GrahamScan.java)
{% endmethod %}

### Bibliography
Expand All @@ -65,6 +69,8 @@ In the end, the code should look something like this:
[import, lang:"javascript"](code/javascript/graham-scan.js)
{% sample lang="py" %}
[import, lang:"python"](code/python/grahamScan.py)
{% sample lang="java" %}
[import, lang:"java"](code/java/GrahamScan.java)
{% endmethod %}

<script>
Expand Down