【凸包模板】Graham算法、Andrew算法
来源:互联网 发布:网络钵钵鸡什么意思 编辑:程序博客网 时间:2024/05/24 01:03
参考:http://blog.csdn.net/bone_ace/article/details/46239187
这里我主要是学习Granham算法、Andrew算法,复杂度O(nlogn);
具体的算法过程可以参考上面的链接;
这里只提一个知识点:叉积
A x B 的值为正,则如上图所示,oab左旋,点b在向量oa左侧!
学习的过程就是把别人的模板变成自己的模板(不了解-->理解);
1.极角排序--Graham算法
const int maxn=1005;int n,tot;struct node{ double x,y;} a[maxn],p[maxn];double dis(node A,node B) //距离{ return sqrt((B.x-A.x)*(B.x-A.x)+(B.y-A.y)*(B.y-A.y));}double x(node A,node B,node C) //叉积公式{ return (B.x-A.x)*(C.y-A.y)-(C.x-A.x)*(B.y-A.y);}bool cmp(node A,node B) //极角排序{ double pp=x(A,B,a[0]); if(pp>0) return true; if(pp<0) return false; return dis(a[0],A)<dis(a[0],B);}void Graham() //Graham算法{ int k=0; for(int i=1; i<n; i++) if(a[i].y<a[k].y||(a[i].y==a[k].y&&a[i].x<a[k].x)) k=i; swap(a[0],a[k]); sort(a+1,a+n,cmp); tot=2; p[0]=a[0],p[1]=a[1]; for(int i=2; i<n; i++) { while(tot>0&&x(p[tot-2],p[tot-1],a[i])<=0) tot--; //不带共线的! p[tot++]=a[i]; }}
2.水平排序--Andrew算法
int n,tot;struct point{ double x,y;};point a[1005],p[1005];double dis(point A,point B) //距离{ return sqrt((B.x-A.x)*(B.x-A.x)+(B.y-A.y)*(B.y-A.y));}double xmul(point A,point B,point C) //叉积{ return (B.x-A.x)*(C.y-A.y)-(B.y-A.y)*(C.x-A.x);}int cmp(point A,point B) //水平排序{ return (A.x<B.x||(A.x==B.x&&A.y<B.y));}void Andrew() //Andrew 算法{ sort(a,a+n,cmp); tot=0; for(int i=0;i<n;i++) { while(tot>1&&xmul(p[tot-2],p[tot-1],a[i])<0) tot--; //加共线的点! p[tot++]=a[i]; } int k=tot; for(int i=n-2;i>=0;i--) { while(tot>k&&xmul(p[tot-2],p[tot-1],a[i])<0) tot--; p[tot++]=a[i]; } if(n>1) tot--;}
//如果是判断稳定凸包的话,使用水平排序;否则用极角排序;
//计算面积:double area(node p1,node p2,node p3) return { ( (p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y) ) / 2.0 ; } 叉积的一半;
阅读全文
0 0
- 【凸包模板】Graham算法、Andrew算法
- 二维凸包模板 【Graham扫描算法】
- 二维凸包模板(Graham算法)
- 凸包 graham 算法
- 凸包 graham 算法
- 凸包 graham 算法
- 1214:凸包模板(Andrew算法)
- 凸包-Graham Scan算法
- 凸包(graham算法)--转载
- 二维凸包 Graham 算法
- 凸包-Graham-Scan算法
- Graham Scan凸包算法
- Graham凸包算法简介
- 凸包之Andrew算法——模板整理
- 二维凸包 Andrew算法
- 凸包Graham模板
- 凸包Graham Scan算法实现
- Graham算法——凸包问题
- C++整理(1)
- Android 实现ViewFlipper左右滑动效果
- 链表
- Debug 心得
- 华中科技大学计算机历年考研复试上机题
- 【凸包模板】Graham算法、Andrew算法
- 设计模式(十九)iterator(于C++过时)
- java jsonp
- 数据库系统原理概论第二章学习攻略(也就是学习重点)
- 多态
- Leetcode 174. Dungeon Game
- 面向对象(下)
- git(三)-.gitignore和分支
- 微信小程序-获得用户openid、昵称等信息的方式