转于 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:
- #include<iostream>
- #include<cmath>
- #include<complex>
- #include<algorithm>
- #define max(a,b) (a)>(b)?(a):(b)
- #define min(a,b) (a)<(b)?(a):(b)
- #define EPS 1e-8
- using namespace std;
- struct point {
- double x,y;
- };
- point convex[50];
- double cross(const point &p1, const point &p2, const point &q1, const point &q2)
- {
- return (q2.y - q1.y)*(p2.x - p1.x) - (q2.x - q1.x)*(p2.y - p1.y);
- }
- bool cmp(const point &a, const point &b)
- {
- point origin;
- origin.x = origin.y = 0;
- return cross(origin,b,origin,a) < 0;
- }
- int main()
- {
- int cnt = 0;
- while (scanf("%lf%lf",&convex[cnt].x,&convex[cnt].y) != EOF) {
- ++cnt;
- }
- sort(convex+1,convex+cnt,cmp);
- for (int i(0); i<cnt; ++i) {
- cout<<"("<<convex[i].x<<","<<convex[i].y<<")"<<endl;
- }
- return 0;
- }
附凸包模板
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- #define _DEBUG 1
- typedef struct
- {
- double x;
- double y;
- }POINT;
- POINT result[102]; //保存凸包上的点
- POINT a[102];
- int n,top;
- double Distance(POINT p1,POINT p2) //两点间的距离
- {
- return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
- }
- double Multiply(POINT p1,POINT p2,POINT p3) //叉积
- {
- return ((p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x));
- }
- int Compare(const void *p1,const void *p2)
- {
- POINT *p3,*p4;
- double m;
- p3=(POINT *)p1;
- p4=(POINT *)p2;
- m=Multiply(a[0],*p3,*p4) ;
- if(m<0) return 1;
- else if(m==0&&(Distance(a[0],*p3)<Distance(a[0],*p4)))
- return 1;
- else return -1;
- }
- void ConvexHull()//计算凸包,使用Graham 算法
- {
- int i;
- result[0].x=a[0].x;
- result[0].y=a[0].y;
- result[1].x=a[1].x;
- result[1].y=a[1].y;
- result[2].x=a[2].x;
- result[2].y=a[2].y;
- top=2;
- for(i=3;i<=n;i++)
- {
- while(Multiply(result[top-1],result[top],a[i])<=0 && top>2)
- top--;
- result[top+1].x=a[i].x;
- result[top+1].y=a[i].y;
- top++;
- }
- }
- int main()
- {
- #if _DEBUG==1
- freopen("ConvexHull.in","r",stdin);
- #endif
- int i,p;
- double px,py,len,temp;
- while(scanf("%d",&n)!=EOF)
- {
- for(i=0;i<n;i++)
- scanf("%lf%lf",&a[i].x,&a[i].y);
- if(n==1)
- {
- printf("0.00\n");
- continue;
- }
- else if(n==2)
- {
- printf("%.2lf\n",Distance(a[0],a[1]));
- continue;
- }
- py=-1;
- for(i=0;i<n;i++)
- {
- if(py==-1 || a[i].y<py)
- {
- px=a[i].x;
- py=a[i].y;
- p=i;
- }
- else if(a[i].y==py && a[i].x<px)
- {
- px=a[i].x;
- py=a[i].y;
- p=i;
- }
- }
- //swap(a[0],a[p])
- temp=a[0].x;
- a[0].x=a[p].x;
- a[p].x=temp;
- temp=a[0].y;
- a[0].y=a[p].y;
- a[p].y=temp;
- qsort(&a[1],n-1,sizeof(POINT),Compare);
- a[n].x=a[0].x;
- a[n].y=a[0].y;
- ConvexHull();//计算凸包,使用Graham 算法
- len=0.0;
- for(i=0;i<top;i++)
- len=len+Distance(result[i],result[i+1]);
- printf("%.2lf\n",len);
- }
- return 0;
- }