简单凸包,叉乘的完美利用(右手定则)。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define maxn 110
using namespace std;
int n;
int top;
struct stu
{
double x,y;
};
stu mapp[maxn],re[maxn];
stu s;
void input()//输入
{
for(int i=0;i<n;i++)
{
cin>>mapp[i].x>>mapp[i].y;
}
}
double dis(stu x,stu y)//计算距离
{
return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));
}
double mul(stu x,stu y,stu z)
{
return (y.x-x.x)*(z.y-x.y)-(y.y-x.y)*(z.x-x.x);
}
void find()//查找起始点
{
for(int i=0;i<n;i++)
{
if(i==0||mapp[i].y<s.y)
{
s=mapp[i];
swap(mapp[0],mapp[i]);
}
else if(mapp[i].y==s.y)
{
if(mapp[i].x<s.x)
{
s=mapp[i];
swap(mapp[0],mapp[i]);
}
}
}
}
bool cmp(stu x,stu y)
{
double k=mul(s,x,y);
if(k>0) return true;
else if(k==0&&dis(s,x)<dis(s,y)) return true;
return false;
}
void tubao()
{
int i;
for(i=0;i<3;i++) re[i]=mapp[i];
top=2;
for(i=3;i<=n;i++)
{
while(mul(re[top-1],re[top],mapp[i])<=0) top--;
re[++top]=mapp[i];
}
}
void cal()
{
double len=0;
for(int i=0;i<top;i++)
{
//cout<<re[i].x<<" "<<re[i].y<<"~"<<endl;
len+=dis(re[i],re[i+1]);
}
printf("%.2f\n",len);
//cout<<top<<endl;
//cout<<s.x<<" "<<s.y<<endl;
}
int main()
{
cin.sync_with_stdio(false);
while(cin>>n&&n)
{
input();
//特判
if(n==1)
{
cout<<"0.00"<<endl;
continue;
}
else if(n==2)
{
printf("%.2f\n",dis(mapp[0],mapp[1]));
continue;
}
find();//查找起始点
sort(mapp+1,mapp+n,cmp);//排序
/*for(int i=0;i<n;i++)
{
cout<<mapp[i].x<<" "<<mapp[i].y<<"~"<<endl;
}*/
mapp[n]=mapp[0];//成环
tubao();
cal();
}
return 0;
}