Scrambled Polygon POJ - 2007(极角排序)

Scrambled Polygon

 POJ - 2007

题意:

 

 

 

 

 

 

 

思路:其实就是将(0,0)这个点按照极角排序,其他点对于(0,0)来排序,将排序后输出就行,注意输入不定

 1 // 
 2 // Created by HJYL on 2020/1/17.
 3 //
 4 #include<iostream>
 5 #include<cstring>
 6 #include<cstdio>
 7 #include<cmath>
 8 #include<algorithm>
 9 using namespace std;
10 const double eps=1e8;
11 int dcmp(double x)
12 {
13     if(fabs(x)<eps)
14         return 0;
15     return x<0?-1:1;
16 }
17 struct Point{
18     int x,y;
19     int id;
20     Point(){}
21     Point(int id,double x,double y):id(id),x(x),y(y){}
22 }initial;//以当前点来求最外凸包
23 
24 typedef Point Vector;
25 
26 Vector operator-(Point A,Point B)
27 {
28     return Vector(-1,A.x-B.x,A.y-B.y);
29 }
30 //叉积
31 double Cross(Vector A,Vector B)
32 {
33     return A.x*B.y-A.y*B.x;
34 }
35 
36 double Len(Point A,Point B)
37 {
38     return (A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y);
39 }
40 
41 //极角排序
42 bool cmp(const Point& A,const Point& B)
43 {
44     double t=Cross(A-initial,B-initial);
45     if(t>0)//叉积大于0,B在A的左边,A在外凸包上
46         return true;
47     else if(t<0)
48         return false;
49     else
50         return Len(A,initial)<Len(B,initial);//极角相同采用最近的点
51 
52 }
53 
54 const int maxn=100;
55 int main()
56 {
57     Point p[maxn];
58     int x,y;
59     while(~scanf("%d%d",&x,&y))
60     {
61         int  num=0;
62         initial=Point(0,0,0);
63         while(~scanf("%d%d",&p[num].x,&p[num].y)){
64             num++;
65         }
66         printf("(0,0)\n");
67         for(int i=0;i<num;i++)
68         {
69             sort(p,p+num,cmp);
70             printf("(%d,%d)\n",p[i].x,p[i].y);
71         }
72     }
73     return 0;
74 }

 

posted @ 2020-01-29 20:24  branna  阅读(134)  评论(0编辑  收藏  举报