Code Library Of Others/sgtlaugh/Convex Hull.cpp
vector <Point> convex_hull (vector<Point> P){
    int i, t, k = 0, n = P.size();
    vector<Point> H(n << 1);
    sort(P.begin(), P.end()); /// Can be converted to O(n) using radix sort

    for (i = 0; i < n; i++) {
        while (k >= 2 && cross(H[k - 2], H[k - 1], P[i]) < 0) k--;
        H[k++] = P[i];
    }
    for (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 - 1); /// The last point is the same as the first in this implementation
    return H;
}
/// Convex hull using the monotone chain algorithm
Convex hull using the monotone chain algorithm
Convex hull using the monotone chain algorithm
vector <Point> convex_hull (vector<Point> P){
    int i, t, k = 0, n = P.size();
    vector<Point> H(n << 1);
    sort(P.begin(), P.end()); 
    for (i = 0; i < n; i++) {
        while (k >= 2 && cross(H[k - 2], H[k - 1], P[i]) < 0) k--;
        H[k++] = P[i];
    }
    for (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 - 1); 
    return H;
}
vector < Point > convex_hull ( vector < Point > P ) { int i , t , k = 0 , n = P . size ( ) ; vector < Point > H ( n << 1 ) ; sort ( P . begin ( ) , P . end ( ) ) ; for ( i = 0 ; i < n ; i ++ ) { while ( k >= 2 && cross ( H [ k - 2 ] , H [ k - 1 ] , P [ i ] ) < 0 ) k -- ; H [ k ++ ] = P [ i ] ; } for ( 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 - 1 ) ; return H ; }
Convex hull using the monotone chain algorithm
Convex hull using the monotone chain algorithm
/// Can be converted to O(n) using radix sort /// The last point is the same as the first in this implementation