poj 2007 Scrambled Polygon

来源:互联网 发布:p2p网络金融 编辑:程序博客网 时间:2024/06/05 10:48

 


#include <cstdio>
#include <algorithm>
#include <cmath>
struct P{
 int x;
 int y;
 double slope;
} point[100];
P stack[100];

int main()
{
    scanf("%d%d",&point[0].x,&point[0].y);
 int count=1;
 int top=3;
 while (scanf("%d%d",&point[count].x,&point[count].y) != EOF)
 {
  if (point[count].x==0 && point[count].y==0) continue;
        point[count].slope=atan2(double(point[count].y),double(point[count].x));
  count++;
 }

 for (int i=1;i<count;i++)
  for (int j=i+1;j<count;j++)
  {
   if (((point[i].x*point[j].y)-(point[j].x*point[i].y)) < 0)
   {
    P t=point[i];
    point[i]=point[j];
    point[j]=t;
   }
  }
      
   
 stack[0]=point[0];
 stack[1]=point[1];
 stack[2]=point[2];
 for (int i=3;i<count;i++)
 {
  while (top)
  {
  P p1=stack[top-1];
  P p2=stack[top-2];
  int x1=p1.x-p2.x,y1=p1.y-p2.y;
  int x2=point[i].x-p2.x,y2=point[i].y-p2.y;
  if ( (x1*y2-x2*y1) < 0)
   top--;
  else break;
  }
  stack[top++]=point[i];
 }

 for (int i=0;i<top;i++)
  printf("(%d,%d)/n",stack[i].x,stack[i].y);


 return 0;
}