POJ 2007 叉积排序

题意:

给出凸包上的点,其中一个点是(0,0),要求从(0,0)按照逆时针输出所有点。

题解:

不能有极角排序,因为多边形可能在原点的左侧。

View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <algorithm>
 5 #include <cstring>
 6 #include <cmath>
 7 
 8 #define N 2222222
 9 #define EPS 1e-3 
10 //不能直接极角排序,因为图形可能全部在原点左侧 
11 using namespace std;
12 
13 struct PO
14 {
15     double x,y,angle;
16 }p[N];
17 
18 int n=1;
19 
20 inline double cross(PO &a,PO &b,PO &c)
21 {
22     return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
23 }
24 
25 inline int doublecmp(double x)
26 {
27     if(x>EPS) return 1;
28     else if(x<-EPS) return -1;
29     return 0;
30 }
31 
32 inline bool cmp(PO a,PO b)
33 {
34     return doublecmp(cross(p[1],a,b))>=0;
35 }
36 
37 inline void go()
38 {
39     for(int i=2;i<=n;i++)
40         if(doublecmp(p[i].x)==0&&doublecmp(p[i].y)==0)
41         {
42             swap(p[i].x,p[1].x); swap(p[i].y,p[1].y);
43             break;
44         }
45     sort(p+2,p+1+n,cmp);
46     for(int i=1;i<=n;i++) printf("(%.0lf,%.0lf)\n",p[i].x,p[i].y);
47 }
48 
49 int main()
50 {
51     while(scanf("%lf%lf",&p[n].x,&p[n].y)!=EOF) n++;
52     n--; go();
53     return 0;
54 }
原文地址:https://www.cnblogs.com/proverbs/p/2924639.html