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

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...

Files

file  ADS/geometry/ConvexHull.h
 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.

Classes

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

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 definitions, the invariants and the costs of the area are in Theory of geometry.

Function documentation

◆ convexHull()

std::vector< Point > ADS::convexHull ( const std::vector< Point > & P)
inline

the hull by the octagon of Akl and Toussaint, the radix sort and the monotone chain of Andrew; the vertices as graham() gives them

In pseudocode (the heuristic of Akl and Toussaint and the monotone chain of Andrew, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{convexHull}( P ) \cr 2 & \quad O \gets \text{the distinct points of } P \text{ of largest } ( -y , x - y , x , x + y , y , y - x , -x , -x - y ) \cr 3 & \quad K \gets \text{the keys } ( x , y ) \text{ of the points of } P \text{ not strictly inside } O \cr 4 & \quad \text{sort } K \text{ by radix and remove its repetitions} \cr 5 & \quad \textbf{if}\ |K| < 3\ \textbf{then return}\ K \cr 6 & \quad H \gets \text{an empty stack} \cr 7 & \quad \textbf{foreach}\ p \in K \text{ forward (the lower chain), } \text{then backward (the upper one)}\ \textbf{do} \cr 8 & \qquad \textbf{while}\ H \text{ has two points of this chain} \ \textbf{and}\ \mathrm{cross}( \text{the second of } H , \mathrm{top}( H ) , p ) \le 0\ \textbf{do} \cr 9 & \qquad\quad \mathrm{pop}( H ) \cr 10 & \qquad \mathrm{push}( H , p ) \cr 11 & \quad \text{drop the last point of } H \text{, the first one come } \text{back} \cr 12 & \quad \textbf{return}\ H \text{ rotated to start from its lowest } \text{point, the rightmost of the lowest ones} \end{array}} \]

Precondition
\( |x| \le 10^9 \) and \( |y| \le 10^9 \)
Postcondition
the hull of \( P \) (audit)

◆ convexHullBrute()

std::vector< Point > ADS::convexHullBrute ( std::vector< Point > P)
inline

the hull by checking every segment, \( O( n^3 ) \); the vertices in counterclockwise order from the lowest one

In pseudocode (section 16.1), a point strictly inside an edge of the hull being no vertex, which the second condition of line 6 ensures:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{convexHullBrute}( P )\quad \triangleright \ O( n^3 ) \cr 2 & \quad \text{drop the copies of the points of } P;\ \textbf{if}\ |P| < 3\ \textbf{then return}\ P \cr 3 & \quad H \gets \emptyset \cr 4 & \quad \textbf{foreach}\ \text{ordered pair } p_i \ne p_j \text{ of } P\ \textbf{do}\quad \triangleright \ \text{an edge: all the points on its left or on it} \cr 5 & \qquad \textbf{if}\ \text{every } p_k \text{ has } \mathrm{cross}( p_i , p_j , p_k ) \ge 0 \text{, and those with } 0 \text{ lie on the segment } p_i p_j\ \textbf{then} \cr 6 & \qquad\quad H \gets H \cup \{ p_i , p_j \} \cr 7 & \quad \textbf{return}\ H \text{ by angle around its lowest point} \end{array}} \]

Precondition
\( |x| \le 10^9 \) and \( |y| \le 10^9 \), so that the cross products fit a long long
Postcondition
the hull of \( P \) (audit)

◆ cross()

long long ADS::cross ( const Point & a,
const Point & b,
const Point & c )
inline

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

It is the determinant

\[ ( x_b - x_a ) ( y_c - y_a ) - ( y_b - y_a ) ( x_c - x_a ) , \]

twice the signed area of the triangle \( a b c \).

◆ graham()

std::vector< Point > ADS::graham ( std::vector< Point > P)
inline

the vertices of the hull in counterclockwise order from the lowest point, the rightmost of the lowest ones, by the scan of Graham

In pseudocode (section 16.1), the stack \( S \) holding at all times the hull of the points scanned so far:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{graham}( P ) \cr 2 & \quad \text{drop the copies of the points of } P;\ \textbf{if}\ |P| < 3\ \textbf{then return}\ P \cr 3 & \quad o \gets \text{the lowest point, the rightmost among the lowest} \cr 4 & \quad Q \gets \text{the other points by increasing angle around } o \text{, the nearest first on a tie} \cr 5 & \quad \text{keep in } Q \text{ only the farthest of the points on one ray from } o \cr 6 & \quad S \gets \text{a stack holding } o \cr 7 & \quad \textbf{foreach}\ p \in Q \text{, in order}\ \textbf{do}\quad \triangleright\ S \text{ turns left at each point} \cr 8 & \qquad \textbf{while}\ |S| \ge 2\ \textbf{and} \ \text{the point under the top, the top and } p \text{ make no left turn}\ \textbf{do}\ \mathrm{pop}( S ) \cr 9 & \qquad \mathrm{push}( S , p ) \cr 10 & \quad \textbf{return}\ \text{the points of } S \text{ from the bottom} \end{array}} \]

Precondition
\( |x| \le 10^9 \) and \( |y| \le 10^9 \), so that the cross products fit a long long
Postcondition
the hull of \( P \) (audit)

◆ pseudoAngle()

double ADS::pseudoAngle ( long dx,
long dy )
inline

an increasing function of the angle of the vector \( ( \mathit{dx} , \mathit{dy} ) \), in \( [ 0 , 4 ) \) and with no arctangent (exercise 16.1)

With \( t = \mathit{dy} / ( |\mathit{dx}| + |\mathit{dy}| ) \), which grows with the angle in the first and in the fourth quadrant, it is

\[ \begin{array}{ll} 0 & \mathit{dx} = \mathit{dy} = 0 , \cr t & \mathit{dx} \ge 0 , \mathit{dy} \ge 0 , \cr 2 - t & \mathit{dx} < 0 , \cr 4 + t & \mathit{dx} \ge 0 , \mathit{dy} < 0 , \end{array} \]

i.e., 1, 2 and 3 on the three axes after the positive one.

Postcondition
a value in \( [ 0 , 4 ) \), also for the extreme values of long (a direction just below the positive \( x \) axis may round to the largest double below 4, tying with the ones next to it)

◆ sameSide()

bool ADS::sameSide ( const Point & p1,
const Point & p2,
const Point & p,
const Point & q )
inline

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)

In pseudocode (section 16.1):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{sameSide}( p_1 , p_2 , p , q )\quad \triangleright\ \text{the line through } p_1 , p_2 \text{ included} \cr 2 & \quad \textbf{return}\ \mathrm{cross}( p_1 , p_2 , p ) \cdot \mathrm{cross}( p_1 , p_2 , q ) \ge 0 \text{, by the signs, with no product} \end{array}} \]