Algorithm
Hull size h
Orientation tests
Area
Points
Playback
View

More information about convex hull algorithms

Wrap the points, one turn at a time

The convex hull is the shape a rubber band takes when it snaps round a set of nails. Finding it is the first problem of computational geometry, and its classic algorithms show how one simple test, does the path turn left or right, can be organised in very different ways. Watch gift wrapping swing its line, Graham scan push and pop its stack, the monotone chain build two halves, QuickHull throw away whole triangles and the incremental algorithm grow the hull point by point, and count what each one pays. Find the convex hull of a set of points, one orientation test at a time. Pick an algorithm and press Play: gift wrapping swings a candidate line round each vertex, Graham scan sorts the points by angle and keeps a stack of left turns, the monotone chain builds a lower and an upper hull, QuickHull keeps the farthest point from each edge and shades the triangles it discards, and the incremental algorithm adds the points one by one. The information panel shows every test: orient(a, b, c) = (b − a) × (c − a), positive for a left turn, negative for a right turn, zero for collinear points, which never count as a turn, so the hull keeps only its corners. Click on the field to add a point, drag points, double-click one to delete it; the examples give random points, a circle (every point on the hull, the worst case for gift wrapping), a Gaussian cluster, a collinear trap, onion layers and a race of 1000 points. Step and Back move by the step size or a whole phase; turn on the chart to compare the operations of all five algorithms on point sets of the same kind from 10 to 1000 points, on log scales. Save image, record a video with the clicks of the tests, or open the picture in the photo editor with the pencil, where the points, the lines of the step, the stack, the hull, the labels and the chart are layers of their own.

Як це працює

  1. Pick an algorithm and press Play: every step is one orientation test, shown in the information panel as orient(a, b, c) with its sign, and the lines on the picture show which turn is being checked.
  2. Click on the field to add points, drag them about and double-click to delete one; or pick an example: a circle where every point is on the hull, a Gaussian cluster, a collinear trap or onion layers.
  3. Use Step and Back by 1, 10 or 100 steps or a whole phase, and Speed for up to 5000 steps a second; Record films it with a click per test, higher for left turns.
  4. Read n, h, the orientation tests and the area in the tiles, and turn on the chart to race all five algorithms on 10 to 1000 points of the same kind on log scales.

The rubber band and the left turn

Stretch a rubber band round nails in a board and let go: it snaps to the convex hull, the smallest convex polygon that holds every nail. Every hull algorithm rests on one question about three points a, b and c: walking from a to b, is c on the left or on the right? The sign of the cross product (b − a) × (c − a) = (bx − ax)(cy − ay) − (by − ay)(cx − ax) answers it: positive for a counterclockwise (left) turn, negative for a clockwise (right) turn, zero when the three points are collinear. Walking round a convex polygon counterclockwise, every turn is a left turn.

Gift wrapping, Graham scan and the monotone chain

Gift wrapping, or the Jarvis march (1973), starts at the leftmost point, which must be on the hull, and swings a line round it: every point to the right of the line becomes the new candidate, and the last one is the next vertex. It costs one test per point for every vertex, O(nh): fast when the hull is small, slow when every point is on it, as on a circle. Graham scan (1972) sorts the points by angle round the lowest point and walks round with a stack, pushing on a left turn and popping on a right turn; after the O(n log n) sort it needs fewer than 2n tests. Andrew's monotone chain (1979) sorts by x instead and builds the lower and the upper hull the same way.

QuickHull and the incremental algorithm

QuickHull splits the points by the line through the leftmost and rightmost points. On each side, the point farthest from the line must be on the hull; the triangle it makes with the line holds only points that cannot be, so they are discarded at once, and the two new edges are treated the same way. Like quicksort, it is O(n log n) on typical inputs and O(n²) in the worst case. The incremental algorithm adds the points one at a time: a point inside the hull so far is dropped, and a point outside replaces the edges it can see by two edges to itself.

Exact tests and collinear points

On a computer the cross product is usually computed in floating point, and when three points are nearly collinear the rounding error can flip its sign, so a hull program may produce a polygon that is not convex, or even crash. This app gives every point whole-number coordinates small enough for the product to be exact, so every sign is right. Collinear points are treated one way everywhere: zero is not a left turn, so a point in the middle of an edge is not a corner, and the collinear trap shows every algorithm agreeing on the four corners of a square full of points along its edges.

Поширені запитання

What is the convex hull of a set of points?

The smallest convex polygon that contains all the points. Its corners are some of the points themselves, and it is the shape a rubber band takes when it is stretched round nails at those points and let go. A set is convex when the segment between any two of its points lies inside it, and the hull is the intersection of all the convex sets that contain the points.

Which convex hull algorithm is fastest?

For n points and h hull vertices: gift wrapping takes O(nh) and wins when the hull is tiny; Graham scan and the monotone chain take O(n log n), dominated by the sort; QuickHull is usually the fastest in practice because it throws whole triangles of points away early, but it can degrade to O(n²). Chan's algorithm (1996) combines Graham scan and gift wrapping into an optimal O(n log h). The chart counts the tests and comparisons of each on the same points.

What is an orientation test?

A test of three points a, b and c that tells whether the path a → b → c turns left, turns right or goes straight on. It is the sign of the cross product (b − a) × (c − a), the same determinant that gives twice the signed area of the triangle abc. Every algorithm here is built from it, and the information panel shows each test with its value.

Why is the circle the worst case for gift wrapping?

Gift wrapping makes a full pass over all n points for every hull vertex it finds, so it costs about n·h tests. When the points lie on a circle every point is a vertex, h = n, and the cost becomes n²: a million tests for 1000 points, while Graham scan still needs only a few thousand. With most points inside, as in a Gaussian cluster, h is small and gift wrapping is quick.

What are onion layers?

Take the convex hull, remove its vertices, take the hull of what is left and repeat until no points remain. The nested polygons are the convex layers, and the process is called onion peeling. The layer a point lies on measures how deep it is in the set, which statisticians use for robust estimates of the centre of a cloud of data (convex hull peeling depth).

Can I use the picture?

Yes. Save gives a PNG at the size you choose, Save JSON writes the points, the hull, the counts, the area and the perimeter, Record films the algorithm with the clicks of its tests, and the pencil opens the picture in the PictorX photo editor with the background, the points, the discarded region, the candidate lines, the stack or chains, the hull, the labels and the chart on layers of their own. In the editor itself it is under Apps ▸ Science ▸ Computing ▸ Convex Hull Algorithms. Everything runs in your browser.

Convex Hull Algorithms: як записати відео?

Натисніть червону кнопку «Запис» на панелі відтворення: вона знімає те, що показує сцена. Натисніть її ще раз, щоб зупинити, і відео збережеться на вашому пристрої. Запис безкоштовний, потрібно лише увійти. Без підписки на відео в куті буде невеликий логотип PictorX. Кнопка поруч із «Запис» відкриває параметри відео: формат (як показано, квадрат, сторіз 9:16 або широкий 16:9), тривалість (доки ви не зупините, або 5, 10, 15 чи 30 секунд для соцмереж) і файл: MP4, WebM або анімований GIF. Для звуку виберіть власні звуки застосунку або одну з трьох музичних доріжок: спокійну, яскраву чи ритмічну.

Готові спробувати?

Відкрийте PictorX у браузері — без встановлення і без кредитної картки.

Перейти до редактора