#include<bits/stdc++.h>
using namespace std;
struct Z
{
double x,y;
} z[1000],p[1000];
//距离
double dis(Z a,Z b)
{
double d=(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
return d;
}
//叉积
double mm(Z a,Z b,Z c)
{
return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
//级角排序
int cmp(Z a,Z b)
{
double x=mm(a,b,z[0]);
if(x>0||(x==0&&dis(a,z[0])<dis(b,z[0])))
return 1;
return 0;
}
//输出排序
int cmpp(Z a,Z b)
{
if(a.x!=b.x)
return a.x<b.x;
else return a.y<b.y;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int m;
cin>>m;
int w=m;
while(w--)
{
cin>>z[w].x>>z[w].y;
}
//Graham扫描法
int k=0;
for(int i=0; i<m; i++)
{
if(z[i].y<z[k].y||(z[i].y==z[k].y&&z[i].x<z[k].x))k=i;
}
swap(z[0],z[k]);
sort(z+1,z+m,cmp);
int top=2;
p[0]=z[0];
p[1]=z[1];
for(int i=2; i<m; i++)
{
while(top>1&&mm(p[top-1],p[top-2],z[i])>=0)
{
top--;
}
p[top++]=z[i];
}
//输出要求
sort(p,p+top,cmpp);
cout<<top<<endl;
for(int i=0; i<top; i++)
{
cout<<p[i].x<<" "<<p[i].y<<endl;;
}
}
}
凸包模版和博客
最新推荐文章于 2023-02-15 18:49:45 发布