您的位置:首页 > 其它

vijos p1233

2016-04-24 18:44 288 查看
多边形面积,注意退化成一条线的情况。

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
const double EPS = 1e-9;
const double INF = 1e30;
const double PI = 3.1415926;
struct pt
{
double x,y;
pt(double x=0,double y=0):x(x),y(y){}
pt read()
{
int a,b;
scanf("%d%d",&a,&b);
this->x=a;this->y=b;
return *this;
}
};
typedef pt vec;
inline vec operator - (vec a,vec b){return vec(a.x-b.x,a.y-b.y);}
inline double dot(vec a,vec b){return a.x*b.x+a.y*b.y;}
inline double len(vec a){return sqrt(dot(a,a));}
inline double cross(vec a,vec b){return a.x*b.y-a.y*b.x;}
inline bool operator < (const vec &a,const vec &b){return a.x<b.x || (a.x==b.x && a.y<b.y);}
inline int dcmp(double x){if(fabs(x)<EPS)return 0;return (x>0)?1:-1;}
double PolygonAera(pt *p,int n)
{
double aera=0.0;
for(int i=1;i<n-1;i++)aera+=cross(p[i]-p[0],p[i+1]-p[0]);
return aera/2.0;
}
double pc(pt *p,int n)
{
double c=len(p[0]-p[n-1]);
for(int i=1;i<n;i++)c+=len(p[i]-p[i-1]);
return c;
}
int chull(pt *p,int n,pt *ch)
{
sort(p,p+n);
int m=0;
for(int i=0;i<n;i++)
{
while(m>1 && cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--;
ch[m++]=p[i];
}
int k=m;
for(int i=n-2;i>=0;i--)
{
while(m>k && cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--;
ch[m++]=p[i];
}
if(n>1)m--;
return m;
}
int main()
{
int n;
scanf("%d",&n);
pt s[20],ch[20];
for(int i=0;i<n;i++)s[i].read();
int m=chull(s,n,ch);
bool ok=false;
for(int i=0;i<m;i++)if(i>1 && dcmp(cross(ch[i]-ch[i-1],ch[i-1]-ch[i-2]))!=0)ok=true;
double c=pc(ch,m);
double d=PolygonAera(ch,m);
if(ok)printf("%.2lf\n%.2lf\n",c,d);
else printf("%.2lf\n%.2lf\n",c/2.0,0.0);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: