栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 1369 Art Gallery

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 1369 Art Gallery

#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<cmath>using namespace std;#define MAXN 1510const double EPS=1e-12;struct point{       double x,y; } convex[MAXN];struct line{       point a,b;       double ang;        }l[MAXN],st[MAXN];int n,ccnt; double  operator *(const point &x,const point &y) { return x.x*y.y-x.y*y.x; } point operator -(point x,const point &y)  { x.x-=y.x,x.y-=y.y;    return x; } point operator *(const line &x,const line &y) { double a1=(y.b-x.a)*(y.a-x.a),a2=(y.a-x.b)*(y.b-x.b);   point r;   r.x=(x.a.x*a2+x.b.x*a1)/(a2+a1);   r.y=(x.a.y*a2+x.b.y*a1)/(a2+a1);   return r; } bool  operator ==(const point &a,const point &b) { return fabs(a.x-b.x)<EPS&&fabs(a.y-b.y)<EPS; } bool operator <(const line &x,const line &y){  if(fabs(x.ang-y.ang)<EPS)  return (y.b-x.a)*(x.b-y.a)>EPS;  return x.ang<y.ang;}bool judgeout(const line &x,const point &p){return (p-x.a)*(x.b-x.a)>EPS; }bool parellel(const line &x,const line &y){ return fabs((x.b-x.a)*(y.b-y.a))<-EPS; }void inputdata(){  scanf("%d",&n);  scanf("%lf%lf",&l[0].b.x,&l[0].b.y);   l[n-1].a.x=l[0].b.x,l[n-1].a.y=l[0].b.y;  for(int i=1;i<n;i++)  {    scanf("%lf%lf",&l[i].b.x,&l[i].b.y);    l[i-1].a.x=l[i].b.x,l[i-1].a.y=l[i].b.y;  }  for(int i=0;i<n;i++)    l[i].ang=atan2(l[i].b.y-l[i].a.y,l[i].b.x-l[i].a.x);}double hplaneintersection(){  int top=1,bot=0;  sort(l,l+n);   int tmp=1;   for(int i=1;i<n;++i)    if(l[i].ang-l[i-1].ang>EPS) l[tmp++]=l[i];    n=tmp;    st[0]=l[0],st[1]=l[1];    for(int i=2;i<n;++i)    {       if(parellel(st[top],st[top-1])||parellel(st[bot],st[bot+1]))  return 0;       while(bot<top&&judgeout(l[i],st[top]*st[top-1])) top--;       while(bot<top&&judgeout(l[i],st[bot]*st[bot+1])) bot++;     st[++top]=l[i];    }    while((bot<top&&judgeout(st[bot],st[top]*st[top-1]))) top--;    while((bot<top&&judgeout(st[top],st[bot]*st[bot+1])))bot++;     if(top<bot+1) return 0.00;     st[++top]=st[bot];    ccnt=0;    for(int i=bot;i<top;i++)    convex[ccnt++]=st[i]*st[i+1];    double ans=0;    convex[ccnt]=convex[0];    for(int i=0;i<ccnt;i++)    ans+=convex[i]*convex[i+1];    return ans/2;}double chackdirection(){ double ans=0; for(int i=0;i<n;++i) ans+=l[i].a*l[i].b; return ans;}void changedirection(){  for(int i=0;i<n;++i)  swap(l[i].a,l[i].b);}int main(){    int t;    scanf("%d",&t);    while(t--)    {      inputdata();      if(chackdirection()<0)  changedirection();      printf("%.2fn",hplaneintersection());    } return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/380602.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号