计算几何:极角排序(poj 2007 Scrambled Polygon)与简单凸包(poj 1113 Wall)

来源:互联网 发布:七天网络网页版 编辑:程序博客网 时间:2024/05/27 08:13

ps:好久没来写博客了..准备重新开始了、两道简单题

poj 2007:http://poj.org/problem?id=2007 

按照(0,0)逆时针排序,由于在-180 ~ 180之内,直接叉积极角排序即可

/*将p[1]到p[m-1]的点根据p[0]按逆时针方向输出排序*/#include <iostream>#include <algorithm>#include <cstdio>#define MAXN 60using 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+10using 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


0 0
原创粉丝点击