Convex Hull
2D Convex Hull
Definition
Convex Polygon
A convex polygon is a simple polygon where all interior angles are in the range \([0,\pi]\).
Convex Hull
The smallest convex polygon that can contain all given points in a plane is called the convex hull.
Formally, for a given set \(X\), the intersection \(S\) of all convex sets containing \(X\) is called the convex hull of \(X\).
Intuitively, it can be thought of as the shape formed by a rubber band stretched around all given points.
The convex hull encloses all given points with the smallest possible perimeter. If a concave polygon encloses all points, its perimeter is certainly not minimal, as shown in the figure below. By the triangle inequality, a convex polygon is optimal in terms of perimeter.

Andrew's Algorithm for Convex Hull
Common methods for computing the convex hull include Graham's scan and Andrew's algorithm. Here we mainly introduce Andrew's algorithm.
Properties
The time complexity of this algorithm is \(O(n\log n)\), where \(n\) is the size of the point set for which the convex hull is to be computed. The bottleneck lies in sorting all points by two keys (coordinates).
Process
First, sort all points with the x-coordinate as the primary key and the y-coordinate as the secondary key.
Clearly, the smallest and largest elements after sorting must lie on the convex hull. Moreover, because it is a convex polygon, if we travel counterclockwise from a point, the trajectory always "turns left". Once a right turn occurs, it indicates that this segment is not part of the convex hull. Therefore, we can use a monotonic stack to maintain the upper and lower hulls.
Since the upper and lower hulls rotate in different directions when viewed from left to right, to make the monotonic stack work, we first enumerate in ascending order to compute the lower hull, and then enumerate in descending order to compute the upper hull.
When constructing the hull, whenever the point about to be pushed onto the stack (\(P\)) and the two points at the top of the stack (\(S_1,S_2\), where \(S_1\) is the top) form a right turn, i.e., the cross product is less than \(0\): \(\overrightarrow{S_2S_1}\times \overrightarrow{S_1P}<0\), pop the top of the stack, go back one step, and continue checking until \(\overrightarrow{S_2S_1}\times \overrightarrow{S_1P}\ge 0\) or only one element remains in the stack.
Typically, points lying on the edges of the convex hull do not need to be preserved. Therefore, the "\(<\)" in the condition \(\overrightarrow{S_2S_1}\times \overrightarrow{S_1P}<0\) above can be changed to \(\le\) as appropriate, and the subsequent condition should be changed to \(>\).
Implementation
Code Implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
According to the above code, there are \(\textit{ans}\) elements on the convex hull (with point \(1\) stored additionally, so the \(h\) array has \(\textit{ans}+1\) elements), sorted in counterclockwise order. The perimeter is
Graham's Scan
Properties
Same as Andrew's algorithm, the time complexity of Graham's scan is \(O(n\log n)\), with the bottleneck also being sorting all points.
Process
First, find the point \(P\) with the smallest y-coordinate among all points. By the definition of convex hull, this point must lie on the convex hull. Then sort all points by the polar angle relative to point \(P\).
Similar to Andrew's algorithm, consider starting from point \(P\) and traveling counterclockwise on the convex hull. All nodes we pass must be "left turns". Formally, for any three consecutive points \(P_1, P_2, P_3\) in counterclockwise order on the convex hull, it must satisfy \(\overrightarrow{P_1 P_2} \times \overrightarrow{P_2 P_3} \ge 0\).
Create a new stack to store convex hull information. First push \(P\) onto the stack, then attempt to add each point in order of polar angle. If the point \(P_0\) to be pushed and the two points \(P_1, P_2\) at the top of the stack (where \(P_1\) is the top) form a "right turn", then pop \(P_1\) from the stack. Repeat this process until the point to be pushed and the two points at the top satisfy the condition, or only one element remains in the stack, then push \(P_0\) onto the stack.
Code Implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
Minkowski Sum
Definition
The Minkowski sum \(P+Q\) of point sets \(P\) and \(Q\) is defined as \(P+Q=\{a+b|a\in P,b\in Q\}\). That is, treat each point in set \(Q\) as a vector, translate each point in set \(P\) along these vectors, and the resulting set is \(P+Q\). Here we only discuss the Minkowski sum of convex hulls.
For example: For point set \(P=\{(0,0),(-3,3),(2,1)\}\) and point set \(Q=\{(0,0),(-1,3),(1,4),(2,2)\}\),
Translate \(P\) along each vector of \(Q\):
It is not difficult to see that the new shape is also a convex hull:
Properties
-
If point sets \(P\) and \(Q\) are convex, then their Minkowski sum \(P+Q\) is also convex.
Proof
Let \(e,f\in P+Q\), with \(a,b \in P\), \(c,d\in Q\) such that \(e=a+c,f=b+d\). Then for any \(t\in[0,1]\):
\[ \begin{aligned} te + (1-t)f &= t(a+c)+(1-t)(b+d)\\ &=(ta+(1-t)b)+(tc+(1-t)d)\\ &\in P+Q. \end{aligned} \]Q.E.D.
- If point sets \(P\) and \(Q\) are convex, then the edge set of their Minkowski sum \(P+Q\) is formed by connecting the edges of \(P\) and \(Q\) after sorting by polar angle.
Proof
Assume without loss of generality that the slope of any edge in convex set \(P\) is different from that of any edge in \(Q\). Rotate the coordinate system so that an edge \(XY\) of \(P\) is horizontal and at the bottom.
Let \(U\) be the lowest point in \(Q\) at this moment, and \(A\) be the lowest and leftmost point in \(P+Q\).
It follows that \(\vec{A} = \vec{X} + \vec{U}\), so \(A\) must lie on the boundary of \(P+Q\).
Similarly, the lowest and rightmost point \(B\) in \(P+Q\) satisfies \(\vec{B} = \vec{Y} + \vec{U}\), and also lies on the boundary of \(P+Q\).
Therefore, \(\vec{AB} = \vec{XY} + \vec{U}\).
If we rotate sequentially, the results continuously form each edge of \(P+Q\).
Q.E.D.
Implementation
Based on property 2, we can sort the convex sets \(P\) and \(Q\) by polar angle to obtain their order of appearance in \(P+Q\). Treat \(P_1+Q_1\) as the starting point of \(P+Q\), then use a merge-like approach to place edges sequentially.
Time complexity: \(O(n+m)\)
Implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
Example
Example [JSOI2018] War
There are two convex hulls \(P\) and \(Q\). Translate \(Q\) \(q\) times, and determine if there is an intersection after each move. \(1\le n,m\le 10^5,1\le q\le 10^5\).
Implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
3D Convex Hull
Basics
Circle inversion: Inversion center is \(O\), inversion radius is \(R\). If a line through \(O\) passes through \(P\) and \(P'\), and \(OP\times OP'=R^{2}\), then \(P\) and \(P'\) are said to be inverses of each other with respect to \(O\).
Process
The process of computing the convex hull is as follows:
- First, apply a slight perturbation to avoid four points being coplanar.
- For a known convex hull, add a new point \(P\). Treat \(P\) as a point light source and cast rays toward the convex hull. It can be seen that the visible and invisible faces of the light must be separated by several edges.
- Delete the visible faces of the light and add the planes formed by the separating edges and \(P\). Repeat this process. By Pick's theorem, Euler's formula (for convex polyhedra, the number of vertices \(V\), edges \(E\), and faces \(F\) satisfy \(V−E+F=2\)), and circle inversion, the complexity is \(O(n^2)\).1
Template Problem
Repeat the above process to obtain the answer.
Code Implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
Exercises
References and Notes
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用