CG2017 PA1-1 Convex Hull (凸包)
1. 前置知识
计算几何算法:Graham Scan,详见教材 1.1 凸包的例子(1.1 An Example: Convex Hulls)
这里给到必要观看的视频课程章节,这些内容对理解和实现 凸包 Graham Scan 算法至关重要,标记有绿色√为必看章节:
2. 思路分析
这题可以说是10个大作业当中最简单的一个,直接按照课程中分析的方法实现即可,非常的简单。但这里需要特别注意一下退化情况:输入点落在extreme edge上面,以及数个相同的输入点。
3. 伪代码
// Brute Froce
Algorithm SLOWCONVEXHULL(P)
算法 SLOWCONVEXHULL(P)
Input. A set P of points in the plane.
输入:平面点集P
Output. A list L containing the vertices of CH(P) in clockwise order.
输出:由CH(P)的顶点沿顺时针方向排成的队列L
1. E <- null.
1. E <- null.
2. for all ordered pairs (p,q) ∈ P×P with p not equal to q
2. for (每一有序对(p, q) ∈ P × P,p ≠ q)
3. do valid <- true
3. do valid <- true
4. for all points r ∈ P not equal to p or q
4. for (除p 和q 之外的所有点r ∈ P)
5. do if r lies to the left of the directed line from p to q
5. do if (r 位于p 和q 所确定有向直线的左侧)
6. then valid <- false.
6. then valid <- false
7. if valid then Add the directed edge pq to E.
7. if (valid) then 将有向边pq加入到E
8. From the set E of edges construct a list L of vertices of CH(P), sorted in clockwise order.
8. 根据集合E中的各边,找出CH(P)的所有顶点,并按照顺时针方向将它们组织为列表L
// Graham Scan
Algorithm CONVEXHULL(P)
算法 CONVEXHULL(P)
Input. A set P of points in the plane.
输入:平面点集P
Output. A list containing the vertices of CH(P) in clockwise order.
输出:由CH(P)的所有顶点沿顺时针方向组成的一个列表
1. Sort the points by x-coordinate, resulting in a sequence p1, . . . , pn.
1. 根据x-坐标,对所有点进行排序,得到序列p1, …, pn
2. Put the points p1 and p2 in a list Lupper, with p1 as the first point.
2. 在Lupper 中加入p1 和p2(p1 在前)
3. for i <- 3 to n
3. for (i <- 3 to n)
4. do Append pi to Lupper.
4. do 在Lupper 中加入pi
5. while Lupper contains more than two points and the last three points in Lupper do not make a right turn
5. while (Lupper 中至少还有三个点,而且最末尾的三个点所构成的不是一个右拐)
6. do Delete the middle of the last three points from Lupper.
6. do 将倒数第二个顶点从Lupper 中删去
7. Put the points pn 6 and pn−1 in a list Llower, with pn as the first point.
7. 在Llower 中加入pn 和pn-1(pn 在前)
8. for i <- n−2 downto 1
8. for (i <- n-2 downto 1)
9. do Append pi to Llower.
9. do 在Llower 中加入pi
10. while Llower contains more than 2 points and the last three points in Llower do not make a right turn
10. while (Llower 中至少还有三个点,而且最末尾的三个点所构成的不是一个右拐)
11. do Delete the middle of the last three points from Llower.
11. do 将倒数第二个顶点从Llower 中删去
12. Remove the first and the last point from Llower to avoid duplication of the points where the upper and lower hull meet.
12. 将第一个和最后一个点从Llower 中删去(以免在上凸包与下凸包联接之后,出现重复顶点)
13. Append Llower to Lupper, and call the resulting list L.
13. 将Llower 联接到Lupper 后面(将由此得到的列表记为L)
14. return L
14. return(L)
4. 可视化结果示例
这里展示一个可视化后的凸包示例,灰色的点表示输入点,灰色的线表示Extreme Edge:
5. 项目代码
个人作业项目代码:Algorithm
1.1.1 Numerical Tests
Description | Entry method |
---|---|
toLeft test | boolean toLeft( Vector base1, Vector base2, Vector point ) |
inCircle test | double inCircle( Vector a, Vector b, Vector c, Vector p ) |
1.1.2 Convex Hull
Description | Entry method\File |
---|---|
Graham’s Scan | List<Vector> grahamScan( List<Vector> points ) |
Brute force | List<Vector> slowConvexHull( List<Vector> points ) |
Program ( including visualization ) | CG2017 PA1-1 Convex Hull / CG2017 PA5-2 Dynamic Convex Hull |
6. 拓展(Follow-ups)
- 动态凸包(PA5-1),支持动态插入新的输入点,和支持查询一个点是否在构造的凸包内部(或边界上)
- 实现其他凸包算法:Divide-And-Conquer,Jarvis March
- 实现更高维的凸包算法:3D Convex Hull,详见教材 11.凸包: 混合物(11. Convex Hulls: Mixing Things)
下一节:CG2017 PA5-1 Dynamic Convex Hull(动态凸包)
系列汇总:清华计算几何大作业思路分析和代码实现
3. 参考资料
- Computational Geometry: Algorithms and Applications
- 计算几何 ⎯⎯ 算法与应用, 邓俊辉译,清华大学出版社
- 计算几何 | Computational Geometry
4. 免责声明
※ 本文之中如有错误和不准确的地方,欢迎大家指正哒~
※ 此项目仅用于学习交流,请不要用于任何形式的商用用途,谢谢呢;