# Algorithm Implementation/Geometry/Convex hull/Monotone chain

From Wikibooks, open books for an open world

**Andrew's monotone chain convex hull algorithm** constructs the convex hull of a set of 2-dimensional points in time.

It does so by first sorting the points lexicographically (first by *x*-coordinate, and in case of a tie, by *y*-coordinate), and then constructing upper and lower hulls of the points in time.

An upper hull is the part of the convex hull, which is visible from the above. It runs from its rightmost point to the leftmost point in counterclockwise order. Lower hull is the remaining part of the convex hull.

## Contents

## Pseudo-code[edit]

Input: a list P of points in the plane. Sort the points of P by x-coordinate (in case of a tie, sort by y-coordinate). Initialize U and L as empty lists. The lists will hold the vertices of upper and lower hulls respectively. for i = 1, 2, ..., n: while L contains at least two points and the sequence of last two points of L and the point P[i] does not make a counter-clockwise turn: remove the last point from L append P[i] to L for i = n, n-1, ..., 1: while U contains at least two points and the sequence of last two points of U and the point P[i] does not make a counter-clockwise turn: remove the last point from U append P[i] to U Remove the last point of each list (it's the same as the first point of the other list). Concatenate L and U to obtain the convex hull of P. Points in the result will be listed in counter-clockwise order.

## Python[edit]

def convex_hull(points): """Computes the convex hull of a set of 2D points. Input: an iterable sequence of (x, y) pairs representing the points. Output: a list of vertices of the convex hull in counter-clockwise order, starting from the vertex with the lexicographically smallest coordinates. Implements Andrew's monotone chain algorithm. O(n log n) complexity. """ # Sort the points lexicographically (tuples are compared lexicographically). # Remove duplicates to detect the case we have just one unique point. points = sorted(set(points)) # Boring case: no points or a single point, possibly repeated multiple times. if len(points) <= 1: return points # 2D cross product of OA and OB vectors, i.e. z-component of their 3D cross product. # Returns a positive value, if OAB makes a counter-clockwise turn, # negative for clockwise turn, and zero if the points are collinear. def cross(o, a, b): return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]) # Build lower hull lower = [] for p in points: while len(lower) >= 2 and cross(lower[-2], lower[-1], p) <= 0: lower.pop() lower.append(p) # Build upper hull upper = [] for p in reversed(points): while len(upper) >= 2 and cross(upper[-2], upper[-1], p) <= 0: upper.pop() upper.append(p) # Concatenation of the lower and upper hulls gives the convex hull. # Last point of each list is omitted because it is repeated at the beginning of the other list. return lower[:-1] + upper[:-1] # Example: convex hull of a 10-by-10 grid. assert convex_hull([(i/10, i%10) for i in range(100)]) == [(0, 0), (9, 0), (9, 9), (0, 9)]

## Ruby[edit]

# the python code converted to ruby syntax def convex_hull(points) points.sort!.uniq! return points if points.length < 3 def cross(o, a, b) (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]) end lower = Array.new points.each{|p| while lower.length > 1 and cross(lower[-2], lower[-1], p) <= 0 do lower.pop end lower.push(p) } upper = Array.new points.reverse_each{|p| while upper.length > 1 and cross(upper[-2], upper[-1], p) <= 0 do upper.pop end upper.push(p) } return lower[0...-1] + upper[0...-1] end fail unless convex_hull((0..9).to_a.repeated_permutation(2).to_a) == [[0, 0], [9, 0], [9, 9], [0, 9]]

## C++[edit]

// Implementation of Andrew's monotone chain 2D convex hull algorithm. // Asymptotic complexity: O(n log n). // Practical performance: 0.5-1.0 seconds for n=1000000 on a 1GHz machine. #include <algorithm> #include <vector> using namespace std; typedef double coord_t; // coordinate type typedef double coord2_t; // must be big enough to hold 2*max(|coordinate|)^2 struct Point { coord_t x, y; bool operator <(const Point &p) const { return x < p.x || (x == p.x && y < p.y); } }; // 2D cross product of OA and OB vectors, i.e. z-component of their 3D cross product. // Returns a positive value, if OAB makes a counter-clockwise turn, // negative for clockwise turn, and zero if the points are collinear. coord2_t cross(const Point &O, const Point &A, const Point &B) { return (A.x - O.x) * (B.y - O.y) - (A.y - O.y) * (B.x - O.x); } // Returns a list of points on the convex hull in counter-clockwise order. // Note: the last point in the returned list is the same as the first one. vector<Point> convex_hull(vector<Point> P) { int n = P.size(), k = 0; vector<Point> H(2*n); // Sort points lexicographically sort(P.begin(), P.end()); // Build lower hull for (int i = 0; i < n; ++i) { while (k >= 2 && cross(H[k-2], H[k-1], P[i]) <= 0) k--; H[k++] = P[i]; } // Build upper hull for (int i = n-2, t = k+1; i >= 0; i--) { while (k >= t && cross(H[k-2], H[k-1], P[i]) <= 0) k--; H[k++] = P[i]; } H.resize(k); return H; }

## Perl[edit]

use Sort::Key::Radix qw(nsortkey); # use radix sort for an O(n) algorithm sub convex_hull { return @_ if @_ < 2; my @p = nkeysort { $_->[0] } @_; my (@u, @l); my $i = 0; while ($i < @p) { my $iu = my $il = $i; my ($x, $yu) = @{$p[$i]}; my $yl = $yu; # search for upper and lower Y for the current X while (++$i < @p and $p[$i][0] == $x) { my $y = $p[$i][1]; if ($y < $yl) { $il = $i; $yl = $y; } elsif ($y > $yu) { $iu = $i; $yu = $y; } } while (@l >= 2) { my ($ox, $oy) = @{$l[-2]}; last if ($l[-1][1] - $oy) * ($x - $ox) < ($yl - $oy) * ($l[-1][0] - $ox); pop @l; } push @l, $p[$il]; while (@u >= 2) { my ($ox, $oy) = @{$u[-2]}; last if ($u[-1][1] - $oy) * ($x - $ox) > ($yu - $oy) * ($u[-1][0] - $ox); pop @u; } push @u, $p[$iu]; } # remove points from the upper hull extremes when they are already # on the lower hull: shift @u if $u[0][1] == $l[0][1]; pop @u if @u and $u[-1][1] == $l[-1][1]; return (@l, reverse @u); }

## C[edit]

C sources taken from the Math::ConvexHull::MonotoneChain Perl module. Note that this implementation works on sorted input points. Otherwise, it's rather similar to the C++ implementation above.

typedef struct { double x; double y; } point_t; typedef point_t* point_ptr_t; /* Three points are a counter-clockwise turn if ccw > 0, clockwise if * ccw < 0, and collinear if ccw = 0 because ccw is a determinant that * gives the signed area of the triangle formed by p1, p2 and p3. */ static double ccw(point_t* p1, point_t* p2, point_t* p3) { return (p2->x - p1->x)*(p3->y - p1->y) - (p2->y - p1->y)*(p3->x - p1->x); } /* Returns a list of points on the convex hull in counter-clockwise order. * Note: the last point in the returned list is the same as the first one. */ void convex_hull(point_t* points, ssize_t npoints, point_ptr_t** out_hull, ssize_t* out_hullsize) { point_ptr_t* hull; ssize_t i, t, k = 0; hull = *out_hull; /* lower hull */ for (i = 0; i < npoints; ++i) { while (k >= 2 && ccw(hull[k-2], hull[k-1], &points[i]) <= 0) --k; hull[k++] = &points[i]; } /* upper hull */ for (i = npoints-2, t = k+1; i >= 0; --i) { while (k >= t && ccw(hull[k-2], hull[k-1], &points[i]) <= 0) --k; hull[k++] = &points[i]; } *out_hull = hull; *out_hullsize = k; }

## Haskell[edit]

import Data.List (sort) -- Coordinate type type R = Double -- Vector / point type type R2 = (R, R) -- Checks if it's shortest to rotate from the OA to the OB vector in a clockwise -- direction. clockwise :: R2 -> R2 -> R2 -> Bool clockwise o a b = (a `sub` o) `cross` (b `sub` o) <= 0 -- 2D cross product. cross :: R2 -> R2 -> R cross (x1, y1) (x2, y2) = x1 * y2 - x2 * y1 -- Subtract two vectors. sub :: R2 -> R2 -> R2 sub (x1, y1) (x2, y2) = (x1 - x2, y1 - y2) -- Implements the monotone chain algorithm convexHull :: [R2] -> [R2] convexHull [] = [] convexHull [p] = [p] convexHull points = lower ++ upper where sorted = sort points lower = chain sorted upper = chain (reverse sorted) chain :: [R2] -> [R2] chain = go [] where -- The first parameter accumulates a monotone chain where the most recently -- added element is at the front of the list. go :: [R2] -> [R2] -> [R2] go acc@(r1:r2:rs) (x:xs) = if clockwise r2 r1 x -- Made a clockwise turn - remove the most recent part of the chain. then go (r2:rs) (x:xs) -- Made a counter-clockwise turn - append to the chain. else go (x:acc) xs -- If there's only one point in the chain, just add the next visited point. go acc (x:xs) = go (x:acc) xs -- No more points to consume - finished! Note: the reverse here causes the -- result to be consistent with the other examples (a ccw hull), but -- removing that and using (upper ++ lower) above will make it cw. go acc [] = reverse $ tail acc main :: IO () main = if result == expected then putStrLn "convexHull worked!" else fail $ "convexHull broken, got " ++ show result where result = convexHull [(x, y) | x <- [0..9], y <- [0..9]] expected = [(0, 0), (9, 0), (9, 9), (0, 9)]

## References[edit]

- De Berg, van Kreveld, Overmars, Schwarzkopf. Computational Geometry: Algorithms and Applications. 2nd edition, Springer-Verlag. ISBN 3540656200.
- Dan Sunday. The Convex Hull of a 2D Point Set or Polygon. (archived copy by webcite)
- A. M. Andrew, "Another Efficient Algorithm for Convex Hulls in Two Dimensions", Info. Proc. Letters 9, 216-219 (1979).