ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ConvexHull.h File Reference

The convex hull of a set \( P \) of \( n \) points of the plane (section 16.1), the smallest convex polygon that contains them, as its vertices in counterclockwise order. More...

#include "ADS/Contract.h"
#include "ADS/sort/Sort.h"
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <vector>
#include "ADS/geometry/impl/ConvexHull.tpp"

Go to the source code of this file.

Classes

struct  ADS::Point
 a point of the plane, of integer coordinates More...

Namespaces

namespace  ADS

Functions

long long ADS::cross (const Point &a, const Point &b, const Point &c)
 the cross product \( ( b - a ) \times ( c - a ) \): positive if \( c \) is to the left of the line from \( a \) to \( b \), 0 if on it; in long long, as long may have 32 bits
bool ADS::sameSide (const Point &p1, const Point &p2, const Point &p, const Point &q)
 true if \( p \) and \( q \) are on the same side of the line through \( p_1 \) and \( p_2 \), or on it (the stessaparte() of the book)
double ADS::pseudoAngle (long dx, long dy)
 an increasing function of the angle of the vector \( ( \mathit{dx} , \mathit{dy} ) \), in \( [ 0 , 4 ) \) and with no arctangent (exercise 16.1)
std::vector< Point > ADS::convexHullBrute (std::vector< Point > P)
 the hull by checking every segment, \( O( n^3 ) \); the vertices in counterclockwise order from the lowest one
std::vector< Point > ADS::graham (std::vector< Point > P)
 the vertices of the hull in counterclockwise order from the lowest point, the rightmost of the lowest ones, by the scan of Graham
std::vector< Point > ADS::convexHull (const std::vector< Point > &P)
 the hull by the octagon of Akl and Toussaint, the radix sort and the monotone chain of Andrew; the vertices as graham() gives them

Detailed description

The convex hull of a set \( P \) of \( n \) points of the plane (section 16.1), the smallest convex polygon that contains them, as its vertices in counterclockwise order.

The side of a point \( q \) with respect to the line through \( p_1 \) and \( p_2 \) is the sign of the cross product \( ( x_2 - x_1 ) ( y_q - y_1 ) - ( y_2 - y_1 ) ( x_q - x_1 ) \), and sameSide(), the stessaparte() of the book, is the product of two such signs not being negative; with integer coordinates it is exact.

  • convexHullBrute(): a segment \( p_i p_j \) is an edge of the hull if all the other points are on one side of its line, \( O( n^3 ) \);
  • graham(): the lowest point (the rightmost of the lowest ones) is a vertex; the others by increasing angle around it, of the collinear ones only the farthest kept, and each one pushed on a stack after popping the vertices that it makes not convex, a backtrack; each point is pushed and popped at most once, \( O( n ) \) after the sort, \( O( n \log n ) \) in all. The angle is compared by the cross product, or by pseudoAngle(), a quantity of the same order as the angle that needs no arctangent (exercise 16.1);
  • convexHull(), beyond the book, the engineered hull: the extreme points in the eight directions of the axes and of the diagonals (the minimum and the maximum of \( x \), \( y \), \( x + y \) and \( x - y \)) lie on the boundary of the hull, in counterclockwise order (each is a vertex unless a tie leaves it inside an edge), and a point strictly inside their octagon (on the left of each of its edges, 8 cross products at most) is no vertex: one pass drops it (the heuristic of Akl and Toussaint). The \( k \) points left, each made a key of 64 bits \( ( x + 2^{31} ) 2^{32} + ( y + 2^{31} ) \), whose order is the one by \( x \) and then \( y \), are sorted by sort() [see Sort.h], \( O( k \log k ) \), \( O( k ) \) when it takes the radix sort (at most 8 passes of bytes), and the lower and the upper chain of the monotone chain of Andrew pop the points that make a turn not to the left, each point pushed and popped at most once, \( O( k ) \): in all \( O( n + k \log k ) \), \( k \le n \); for points uniform in a square the octagon leaves a vanishing fraction of them, and the expected time is \( O( n ) \) (Devroye and Toussaint), whereas in a disk about \( 1 - 2 \sqrt{2} / \pi \approx 10\% \) of them survive. The vertices come in the order and from the point of graham().

The contracts [see Contract.h]: the pseudo-angle is in \( [ 0 , 4 ) \), and the hulls are convex, counterclockwise, made of input points and contain all of them (checked at level 3). The operations are defined in ConvexHull.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 16.1, https://algoritmicamente.it/materiali/libro/capitoli/cap16.pdf
R. L. Graham, An efficient algorithm for determining the convex hull of a finite planar set, Information Processing Letters 1(4), 132-133, 1972, doi:10.1016/0020-0190(72)90045-2
S. G. Akl, G. T. Toussaint, A fast convex hull algorithm, Information Processing Letters 7(5), 219-222, 1978, doi:10.1016/0020-0190(78)90003-0
A. M. Andrew, Another efficient algorithm for convex hulls in two dimensions, Information Processing Letters 9(5), 216-219, 1979, doi:10.1016/0020-0190(79)90072-3
L. Devroye, G. T. Toussaint, A note on linear expected time algorithms for finding convex hulls, Computing 26(4), 361-366, 1981, doi:10.1007/BF02237955
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa