当前位置:网站首页>Graham‘s Scan法求解凸包问题
Graham‘s Scan法求解凸包问题
2022-07-31 15:49:00 【Yasen Jia】
问题背景
- 关于凸包
假设平面上有p0~p12共13个点,过某些点做一个多边形,使这个多边形能把所有点都“包”起来,当这个多边形为凸多边形时,我们称之为“凸包” - 凸包问题
在二维坐标系上,每个点都能用坐标(x,y)表示
给出点的数目与各点的坐标,求构成凸包的点
Graham’s Scan法
先找到凸包上的一个点,从那个点开始按逆时针方向逐个找凸包上的点
步骤
将所有点放在二维坐标系中,则纵坐标最小的点一定是凸包上的点,如图中p0
将所有点的坐标进行平移,使p0作为原点,如上图
计算各个点相对于p0的辐角 α \alpha α,按辐角从小到大的顺序对各个点进行排序。当 α \alpha α 相同时,距离p0比较近的点排在前面。例如上图得到的结果为p1、p2、p3、p4、p5、p6、p7、p8。由几何知识可以知道,第一个点p1和最后一个点p8一定是凸包上的点
现在我们知道了凸包上的第一个点p0和第二个点p1,我们把他们依次放在栈里面。
将当前点初始化为p2,并进行如下操作:
a. 判断栈顶的两个点形成的向量与栈顶点和当前点形成的向量之间是否是逆时针关系,即叉乘后得到的向量是否是逆时针方向。(这个问题的另一个表述是连接栈顶的两个点得到一条直线,看当前点是否在这个直线的左边) b.若是,则当前点为凸包上的点,将其压入栈中。并将排序中其后的点作为当前点,继续进行a的判断,直到遍历所有点。 c. 若不是,则栈顶元素不是凸包上的点,将栈顶元素弹出。当前点不变,继续进行a的判断,直到是逆时针关系。最终遍历完后,栈中的元素就是构成凸包的点
流程图

代码链接
边栏推荐
- 6-22 Vulnerability exploit - postgresql database password cracking
- Deployment应用生命周期与Pod健康检查
- Ubantu project 4: xshell, XFTP connected the virtual machine and set xshell copy and paste the shortcut
- The R language ggstatsplot package ggbarstats function visualizes bar charts, and adds hypothesis test results (including sample number, statistics, effect size and its confidence interval, significan
- Destruction order of thread_local variables
- Use of radiobutton
- [Meetup Preview] OpenMLDB+OneFlow: Link feature engineering to model training to accelerate machine learning model development
- Linux查看redis版本(查看mongodb版本)
- 【7.28】代码源 - 【Fence Painting】【合适数对(数据加强版)】
- R language ggplot2 visualization: use the ggboxplot function of the ggpubr package to visualize the grouped box plot, use the ggpar function to change the graphical parameters (caption, add, modify th
猜你喜欢

After Grafana is installed, the web opens and reports an error

全新宝马3系上市,安全、舒适一个不落

Kubernetes common commands

Ubantu project 4: xshell, XFTP connected the virtual machine and set xshell copy and paste the shortcut

Foreign media right, apple on May be true in inventory

定时器的类型

WPF项目--控件入门基础用法,必知必会XAML

Why is the field of hacking almost filled with boys?

Visualize GraphQL schemas with GraphiQL

6-22 Vulnerability exploit - postgresql database password cracking
随机推荐
Emmet syntax
mongo进入报错
使用 GraphiQL 可视化 GraphQL 架构
The use of border controls
button控件的使用
Why don't you make a confession during the graduation season?
Precautions and solutions when SIGABRT error is reported
小程序:matlab解微分方程「建议收藏」
工程力学复习资料
在资源管理类中提供对原始资源的访问——条款15
Kubernetes原理剖析与实战应用手册,太全了
tooltips使用教程(鼠标悬停时显示提示)
删除表格数据或清空表格
Why is the field of hacking almost filled with boys?
Premiere Pro 2022 for (pr 2022)v22.5.0
The principle of hough transform detection of straight lines (opencv hough straight line detection)
C语言-函数
ML.NET相关资源整理
01 Encounter typescript, build environment
After Grafana is installed, the web opens and reports an error