计算几何:极角排序(poj 2007 Scrambled Polygon)与简单凸包(poj 1113 Wall)
2016-07-23 10:53
681 查看
ps:好久没来写博客了..准备重新开始了、两道简单题
poj 2007:http://poj.org/problem?id=2007
按照(0,0)逆时针排序,由于在-180 ~ 180之内,直接叉积极角排序即可
![](http://poj.org/images/2007_3.jpg)
接下来一道凸包:
poj 1113 http://poj.org/problem?id=1113
求一个多边形距离为L的周长,一开始以为是求凸包然后等比例放大,后来一想每个顶点用一点园周即可,整个图形合起来就是凸包的周长加一个半径为L的圆的周长
此题推荐一个不错的排序方法(水平序): http://www.tuicool.com/articles/iyqiY3Z
poj 2007:http://poj.org/problem?id=2007
按照(0,0)逆时针排序,由于在-180 ~ 180之内,直接叉积极角排序即可
![](http://poj.org/images/2007_3.jpg)
/*将p[1]到p[m-1]的点根据p[0]按逆时针方向输出排序*/ #include <iostream> #include <algorithm> #include <cstdio> #define MAXN 60 using namespace std; struct point{ int x,y; }p[MAXN]; int cross(int x1,int y1,int x2,int y2){ return x2*y1 - x1*y2; } bool cmp(const point &a,const point &b){ int x1 = a.x - p[0].x,y1 = a.y - p[0].y; int x2 = b.x - p[0].x,y2 = b.y - p[0].y; return cross(x1,y1,x2,y2) < 0; } int main() { int m =0; while(~scanf("%d%d",&p[m].x,&p[m].y)) m++; sort(p+1,p+m,cmp); for(int i = 0;i<m;i++) printf("(%d,%d)\n",p[i].x,p[i].y); return 0; }
接下来一道凸包:
poj 1113 http://poj.org/problem?id=1113
求一个多边形距离为L的周长,一开始以为是求凸包然后等比例放大,后来一想每个顶点用一点园周即可,整个图形合起来就是凸包的周长加一个半径为L的圆的周长
#include <iostream> #include <algorithm> #include <cstdio> #include <cmath> #include <stack> #define PI acos(-1) #define MAXN 1000+10 using namespace std; int N,L; struct point{ int x,y; }p[MAXN]; int cross(int x1,int y1,int x2,int y2){ return x2*y1 - x1*y2; } double dis(const point &a,const point &b){ return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } double dis(int x1,int y1,int x2,int y2){ return sqrt((x1 - x2)*(x1 - x2) + (y1 - y2) * (y1 - y2)); } bool cmp(const point &a,const point &b){ int x1 = a.x - p[0].x,y1 = a.y - p[0].y; int x2 = b.x - p[0].x,y2 = b.y - p[0].y; if(cross(x1,y1,x2,y2) != 0) return cross(x1,y1,x2,y2) < 0; else return dis(a,p[0]) < dis(b,p[0]); } bool ok(point p1,point p2,point p3){ int x1 = p2.x - p1.x; int y1 = p2.y - p1.y; int x2 = p3.x - p2.x; int y2 = p3.y - p2.y; int c = cross(x1,y1,x2,y2); if(c <= 0) return true; else return false; } //凸包扫描算法 void GrahamScan(){ stack<point> s; s.push(p[0]);s.push(p[1]); int i = 2; while(i < N ){ point p2 = s.top();s.pop(); point p1 = s.top(); point p3 = p[i]; if(ok(p1,p2,p3)){ s.push(p2);s.push(p[i]); i++; } } s.pop(); double C = 0.0; point now = p[N-1]; while(!s.empty()){ //printf("pre (s.top):x= %d,y = %d\n",s.top().x,s.top().y); C += dis(now,s.top()); now = s.top(); s.pop(); } C += dis(p[0],p[N-1]); long long ans = (long long )((C + (2.0*L*PI))+0.5); printf("%I64d\n",ans); } int main() { while(~scanf("%d%d",&N,&L)){ int m = 1; scanf("%d%d",&p[0].x,&p[0].y); for(int i=1;i<N;i++){ point pt; scanf("%d%d",&pt.x,&pt.y); if(pt.y < p[0].y || (pt.y == p[0].y && pt.x < p[0].x)){ p[m].x = p[0].x;p[m++].y = p[0].y; p[0].x = pt.x;p[0].y = pt.y; } else{ p[m].x = pt.x;p[m++].y = pt.y; } } sort(p+1,p+N,cmp); GrahamScan(); } return 0; }
此题推荐一个不错的排序方法(水平序): http://www.tuicool.com/articles/iyqiY3Z
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- ACMer博客瀑布流分析
- ACM程序设计大赛题目分类
- 2015年acm国内排名
- 计算字符串最后一个单词长度
- ACM网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 矩阵的乘法操作
- 蚂蚁爬行问题
- 蚂蚁爬行问题
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- HDOJ 1002 A + B Problem II (Big Numbers Addition)
- 初学ACM - 半数集(Half Set)问题 NOJ 1010 / FOJ 1207
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1002