极角排序

转于 http://www.cnblogs.com/devtang/archive/2012/02/01/2334977.html

介绍几种极角排序:

1.利用叉积的正负来作cmp.(即是按逆时针排序).此题就是用这种方法

1 bool cmp(const point &a, const point &b)//逆时针排序 
2 {
3     point origin;
4     origin.x = origin.y = 0;
5     return cross(origin,b,origin,a) < 0;
6 }

2.利用complex的内建函数。

 1 #include<complex>
 2 #define x real()
 3 #define y imag()
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 bool cmp(const Point& p1, const Point& p2)
 8 {
 9     return arg(p1) < arg(p2);
10 }

3.利用arctan计算极角大小。(范围『-180,180』)

1 bool cmp(const Point& p1, const Point& p2)
2 {
3     return atan2(p1.y, p1.x) < atan2(p2.y, p2.x);
4 }

4.利用象限加上极角,叉积。

 1 bool cmp(const point &a, const point &b)//先按象限排序,再按极角排序,再按远近排序 
 2 {
 3     if (a.y == 0 && b.y == 0 && a.x*b.x <= 0)return a.x>b.x;
 4     if (a.y == 0 && a.x >= 0 && b.y != 0)return true;
 5     if (b.y == 0 && b.x >= 0 && a.y != 0)return false;
 6     if (b.y*a.y <= 0)return a.y>b.y;
 7     point one;
 8     one.y = one.x = 0;
 9     return cross(one,a,one,b) > 0 || (cross(one,a,one,b) == 0 && a.x < b.x);   
 
10 }

POJ 2007:

[cpp]  view plain copy
  1. #include<iostream>  
  2. #include<cmath>  
  3. #include<complex>  
  4. #include<algorithm>  
  5. #define max(a,b) (a)>(b)?(a):(b)  
  6. #define min(a,b) (a)<(b)?(a):(b)  
  7. #define EPS 1e-8  
  8. using namespace std;  
  9. struct point {  
  10.     double x,y;      
  11. };  
  12. point convex[50];  
  13.   
  14. double cross(const point &p1, const point &p2, const point &q1, const point &q2)  
  15. {  
  16.     return (q2.y - q1.y)*(p2.x - p1.x) - (q2.x - q1.x)*(p2.y - p1.y);      
  17. }  
  18.   
  19. bool cmp(const point &a, const point &b)  
  20. {  
  21.     point origin;  
  22.     origin.x = origin.y = 0;  
  23.     return cross(origin,b,origin,a) < 0;  
  24. }  
  25.   
  26.   
  27. int main()  
  28. {  
  29.     int cnt = 0;  
  30.     while (scanf("%lf%lf",&convex[cnt].x,&convex[cnt].y) != EOF) {  
  31.         ++cnt;  
  32.     }  
  33.     sort(convex+1,convex+cnt,cmp);  
  34.     for (int i(0); i<cnt; ++i) {  
  35.         cout<<"("<<convex[i].x<<","<<convex[i].y<<")"<<endl;      
  36.     }  
  37.     return 0;      
  38. }  

附凸包模板

[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <math.h>  
  3. #include <stdlib.h>  
  4. #define _DEBUG 1  
  5. typedef struct  
  6. {  
  7.     double x;  
  8.     double y;  
  9. }POINT;  
  10.   
  11. POINT result[102];                          //保存凸包上的点  
  12. POINT a[102];                                 
  13. int n,top;  
  14.   
  15. double Distance(POINT p1,POINT p2)          //两点间的距离  
  16. {  
  17.     return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));  
  18. }  
  19. double Multiply(POINT p1,POINT p2,POINT p3) //叉积  
  20. {     
  21.    return ((p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x));   
  22. }  
  23. int Compare(const void *p1,const void *p2)  
  24. {  
  25.     POINT *p3,*p4;  
  26.     double m;  
  27.     p3=(POINT *)p1;   
  28.     p4=(POINT *)p2;   
  29.     m=Multiply(a[0],*p3,*p4) ;  
  30.     if(m<0) return 1;  
  31.     else if(m==0&&(Distance(a[0],*p3)<Distance(a[0],*p4)))  
  32.         return 1;  
  33.     else return -1;  
  34. }  
  35. void ConvexHull()//计算凸包,使用Graham 算法  
  36. {  
  37.    int i;  
  38.    result[0].x=a[0].x;  
  39.    result[0].y=a[0].y;  
  40.    result[1].x=a[1].x;  
  41.    result[1].y=a[1].y;  
  42.    result[2].x=a[2].x;  
  43.    result[2].y=a[2].y;  
  44.    top=2;  
  45.    for(i=3;i<=n;i++)  
  46.    {  
  47.        while(Multiply(result[top-1],result[top],a[i])<=0 && top>2)  
  48.             top--;  
  49.        result[top+1].x=a[i].x;  
  50.        result[top+1].y=a[i].y;  
  51.        top++;  
  52.    }  
  53. }  
  54.   
  55. int main()  
  56. {  
  57. #if _DEBUG==1  
  58.     freopen("ConvexHull.in","r",stdin);  
  59. #endif  
  60.   
  61.    int i,p;  
  62.    double px,py,len,temp;  
  63.    while(scanf("%d",&n)!=EOF)  
  64.    {  
  65.        for(i=0;i<n;i++)  
  66.            scanf("%lf%lf",&a[i].x,&a[i].y);  
  67.        if(n==1)  
  68.        {  
  69.            printf("0.00\n");  
  70.            continue;  
  71.        }  
  72.        else if(n==2)  
  73.        {  
  74.            printf("%.2lf\n",Distance(a[0],a[1]));  
  75.            continue;  
  76.        }  
  77.        py=-1;  
  78.        for(i=0;i<n;i++)  
  79.        {  
  80.            if(py==-1 || a[i].y<py)  
  81.            {  
  82.                px=a[i].x;  
  83.                py=a[i].y;  
  84.                p=i;  
  85.            }  
  86.            else if(a[i].y==py && a[i].x<px)  
  87.            {  
  88.                px=a[i].x;  
  89.                py=a[i].y;  
  90.                p=i;  
  91.            }  
  92.        }  
  93.        //swap(a[0],a[p])  
  94.        temp=a[0].x;  
  95.        a[0].x=a[p].x;  
  96.        a[p].x=temp;  
  97.        temp=a[0].y;  
  98.        a[0].y=a[p].y;  
  99.        a[p].y=temp;  
  100.   
  101.        qsort(&a[1],n-1,sizeof(POINT),Compare);  
  102.        a[n].x=a[0].x;  
  103.        a[n].y=a[0].y;  
  104.        ConvexHull();//计算凸包,使用Graham 算法  
  105.        len=0.0;  
  106.        for(i=0;i<top;i++)  
  107.            len=len+Distance(result[i],result[i+1]);  
  108.        printf("%.2lf\n",len);  
  109.    }  
  110.    return 0;  
  111. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值