极角排序以及凸包模板

来源:互联网 发布:ios广告拦截软件 编辑:程序博客网 时间:2024/05/22 14:19

几种极角排序

#include<iostream>using namespace std;struct point{    double x,y;};double cross(double x1,double y1,double x2,double y2) //计算叉积{    return (x1*y2-x2*y1);}double compare(point a,point b,point c){    return cross((a.x-c.x),(a.y-c.y),(b.x-c.x),(b.y-c.y));}bool cmp0(point a,point b) //利用叉积按极角从小到大排序{    point c;//原点    c.x = 0;    c.y = 0;    if(compare(c,a,b)==0)        return a.x<b.x;    else return compare(a,b,c)>0;}//利用complex类按极角从小到大排序:bool cmp1(const point& a, const point& b) // 利用complex类按极角从小到大排序{    complex<double> c1(a.x,a.y);                   //头文件 #include<complex>    complex<double> c2(b.x,b.y);    if( arg(c1) == arg(c2))        return a.x<b.x;    return arg(c1) < arg(c2);}//利用atan2()函数按极角从小到大排序bool cmp2(point a,point b)//利用atan2()函数按极角从小到大排序{    if(atan2(a.y,a.x)!=atan2(b.y,b.x))        return atan2(a.y,a.x)<atan2(b.y,b.x);    else return a.x<b.x;}//利用叉积按极角从小到大排序//先按象限从小到大排序 再按极角从小到大排序int Quadrant(point a)  //象限排序{    if(a.x>0&&a.y>=0)  return 1;    if(a.x<=0&&a.y>0)  return 2;    if(a.x<0&&a.y<=0)  return 3;    if(a.x>=0&&a.y<0)  return 4;}bool cmp3(point a,point b)  //先按象限从小到大排序 再按极角从小到大排序{    if(Quadrant(a)==Quadrant(b))        return cmp1(a,b);    else Quadrant(a)<Quadrant(b);}

这里附上两个凸包模板,两个的主函数内容一模一样,不同的是排序方式。第一种排序方式是利用atan2函数进行排序(个人观点不建议使用这种排序方式,因为在有些题目当中会伤精度,导致WA);另一种方式是利用叉积进行排序。

凸包模板1

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<stack>#include <algorithm>const int MAX=0x3f3f3f3f;using namespace std;struct point{    long long  x;    //这里定义为long long  有的题会用到double。    long long  y;} P[50005],S[50005];  //P中存点,S模拟栈存凸包的点;long long  xx;long long  yy;// 计算各个点相对于 P0 的幅角 α ,按从小到大的顺序对各个点排序。当 α 相同时,距离 P0 比较近的排在前面。bool cmp(struct point a,struct point b)//利用atan2函数进行排序{    if(atan2(a.y-yy,a.x-xx)!=atan2(b.y-yy,b.x-xx))        return (atan2(a.y-yy,a.x-xx))<(atan2(b.y-yy,b.x-xx));    return a.x<b.x;}//叉积判断点的位置long long  Cross(long long  x1,long long  y1,long long  x2,long long  y2){    return (x1*y2-x2*y1);}long long Compare(struct point a,struct point b,struct point c){    return Cross((b.x-a.x),(b.y-a.y),(c.x-a.x),(c.y-a.y));}int main(){    int n,i,j,t;    while(cin>>n)    {        int top = 1;        xx = MAX;        yy = MAX;        for(i=0; i<n; i++)        {            cin>>P[i].x>>P[i].y;            if(P[i].y<yy)                yy = P[i].y;//这里找出纵坐标最小的点        }        for(i=0; i<=n; i++)        {            if(P[i].y==yy&&P[i].x<xx)//如果yy值有多个则找x最小的点            {                xx=P[i].x;                t=i;            }        }        P[t] = P[0];        sort(P+1,P+n,cmp);        S[0].x = xx;        S[0].y = yy;        S[1] = P[1];        for(i = 2; i<n;)        {            if(top&&(Compare(S[top-1],S[top],P[i])<0)) top--;            else S[++top] = P[i++];        }        for(i=0; i<=top; i++)            cout<<S[i].x<<" "<<S[i].y<<endl;    }    return 0;}

凸包模板2

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<stack>#include <algorithm>const int MAX=0x3f3f3f3f;using namespace std;struct point{    long long  x;    //这里定义为long long  有的题会用到double。    long long  y;} P[50005],S[50005];  //P中存点,S模拟栈存凸包的点;long long  xx;long long  yy;long long  Cross(long long  x1,long long  y1,long long  x2,long long  y2)////叉积判断点的位置{    return (x1*y2-x2*y1);}long long Compare(struct point a,struct point b,struct point c){    return Cross((b.x-a.x),(b.y-a.y),(c.x-a.x),(c.y-a.y));}bool cmp(struct point a,struct point b)//利用叉积进行排序{    point c;    c.x=0;    c.y=0;// 原点    if(Compare(a,b,c)==0)        return a.x<b.x;    else        return Compare(a,b,c)>0;   //从小到大排序}int main(){    int n,i,j,t;    while(cin>>n)    {        int top = 1;        xx = MAX;        yy = MAX;        for(i=0; i<n; i++)        {            cin>>P[i].x>>P[i].y;            if(P[i].y<yy)                yy = P[i].y;//这里找出纵坐标最小的点        }        for(i=0; i<=n; i++)        {            if(P[i].y==yy&&P[i].x<xx)//如果yy值有多个则找x最小的点            {                xx=P[i].x;                t=i;            }        }        P[t] = P[0];        sort(P+1,P+n,cmp);        S[0].x = xx;        S[0].y = yy;        S[1] = P[1];        for(i = 2; i<n;)        {            if(top&&(Compare(S[top-1],S[top],P[i])<0)) top--;            else S[++top] = P[i++];        }        for(i=0; i<=top; i++)            cout<<S[i].x<<" "<<S[i].y<<endl;    }    return 0;}
原创粉丝点击