Convex Hull Implementation in Java
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Convex Hull runs in O(n log n) time and O(n) space, in Java as in every other language.
- ✓No hull anywhere in the JDK. java.awt.geom.Point2D.Double gives double-precision points, java.awt.Polygon is int-only and meant for rendering, and java.awt.geom.Path2D.Double is what you build the finished ring into for drawing.
- ✓java.util.Stack is rejected on sight because it extends the synchronised Vector; use ArrayDeque, or an ArrayList treated as a stack so you can index two from the top.
- ✓The Java trap: Java is the one runtime here that actively detects a broken comparator and throws. Write the tempting (a, b) -> (int) (a.getX() - b.getX()) and two things go wrong at once: the cast truncates toward zero, so any two points less than 1.0 apart compare equal while a third point orders them apart, and that alone destroys transitivity (for huge coordinates the narrowing conversion saturates to Integer.MAX_VALUE rather than wrapping, JLS 5.1.3, so the sign survives -- the truncation is what kills you).
- ✓Every Point2D.Double is a separate heap object with a header, so an n-point sort chases n pointers around memory; the standard rewrite when this gets hot is two double[] arrays sorted by an index permutation, because Arrays.sort(double[]) is dual-pivot quicksort with no comparator call at all.
What Convex Hull actually is
Convex Hull in Java: what you actually reach for
Java implementation
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public final class ConvexHull {
private ConvexHull() {}
private static double cross(Point2D o, Point2D a, Point2D b) {
return (a.getX() - o.getX()) * (b.getY() - o.getY())
- (a.getY() - o.getY()) * (b.getX() - o.getX());
}
private static List<Point2D> chain(List<Point2D> pts) {
List<Point2D> h = new ArrayList<>();
for (Point2D p : pts) {
while (h.size() >= 2
&& cross(h.get(h.size() - 2), h.get(h.size() - 1), p) <= 0) {
h.remove(h.size() - 1);
}
h.add(p);
}
h.remove(h.size() - 1);
return h;
}
public static List<Point2D> of(List<Point2D> input) {
List<Point2D> pts = new ArrayList<>(input);
// Double.compare inside comparingDouble: never (int)(a - b).
pts.sort(Comparator.comparingDouble(Point2D::getX)
.thenComparingDouble(Point2D::getY));
if (pts.size() < 3) return List.copyOf(pts);
List<Point2D> hull = new ArrayList<>(chain(pts));
Collections.reverse(pts);
hull.addAll(chain(pts));
return List.copyOf(hull);
}
}
Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Java-specific trap
Performance in Java, beyond the O(n log n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Java type or library should I use for Convex Hull?
No hull anywhere in the JDK. java.awt.geom.Point2D.Double gives double-precision points, java.awt.Polygon is int-only and meant for rendering, and java.awt.geom.Path2D.Double is what you build the finished ring into for drawing. The sort is List.sort or Arrays.sort with Comparator.comparingDouble(Point2D::getX).thenComparingDouble(Point2D::getY). For anything geospatial the ecosystem standard is JTS: org.locationtech.jts.algorithm.ConvexHull, constructed from a Geometry or a Coordinate[], whose getConvexHull() returns a Geometry -- usually a Polygon, but a LineString or Point for degenerate inputs, which callers forget to handle.
What is the most common Java mistake when implementing Convex Hull?
Java is the one runtime here that actively detects a broken comparator and throws. Write the tempting (a, b) -> (int) (a.getX() - b.getX()) and two things go wrong at once: the cast truncates toward zero, so any two points less than 1.0 apart compare equal while a third point orders them apart, and that alone destroys transitivity (for huge coordinates the narrowing conversion saturates to Integer.MAX_VALUE rather than wrapping, JLS 5.1.3, so the sign survives -- the truncation is what kills you). Arrays.sort on objects is TimSort, and TimSort verifies its merge invariants, so instead of a quietly wrong hull you get IllegalArgumentException: Comparison method violates its general contract! thrown from deep inside the JDK -- on some inputs only, usually in production, never in the unit test. Use Double.compare or comparingDouble.
How does Convex Hull actually perform in Java?
Every Point2D.Double is a separate heap object with a header, so an n-point sort chases n pointers around memory; the standard rewrite when this gets hot is two double[] arrays sorted by an index permutation, because Arrays.sort(double[]) is dual-pivot quicksort with no comparator call at all. ArrayList.get is a checked array read, and remove(size - 1) is O(1) but nulls the slot. Escape analysis rarely scalarises points that escape into a list. Expect C2 to need a few thousand iterations before the cross call inlines and the bounds checks fold. Time complexity is O(n log n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialBefore paying for GitHub Copilot, check its student and free-tier options on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.