Scrambled Polygon POJ - 2007 (polar angle sorting)

Scrambled Polygon

 POJ - 2007

Meaning of the questions:

 

 

 

 

 

 

 

Ideas: in fact, the (0,0) according to this point of the polar angle ordering, for other points (0,0) to sort, after sorting the output on the line, pay attention to the input variable

 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 }

 

Guess you like

Origin www.cnblogs.com/Vampire6/p/12241198.html