UVA 12931 Common Area
来源:互联网 发布:超级基因优化液排雷 编辑:程序博客网 时间:2024/06/05 00:35
题意:给出两个多边形,判断是否有公共面积。
做法:很显然若不重合,必然有一个多边形有边穿过或就在另一个多边形里面。问题就很简单了。
#include<map>#include<string>#include<cstring>#include<cstdio>#include<cstdlib>#include<cmath>#include<queue>#include<vector>#include<iostream>#include<algorithm>#include<bitset>#include<climits>#include<list>#include<iomanip>#include<stack>#include<set>using namespace std;const double eps = 1e-8;const double pi = acos(-1.0);int sgn(double x){return fabs(x)<eps?0:x<0?-1:1;}struct point{double x,y;point(){}point(double x,double y){this->x=x;this->y=y;}point operator +(point a){return point(x+a.x,y+a.y);}point operator -(point a){return point(x-a.x,y-a.y);}double operator *(point a){return x*a.x + y*a.y;}double operator /(point a){return x*a.y-y*a.x;}bool operator <(point a)const{return x!=a.x?x<a.x:y<a.y;}};struct line{point s,e;line(){}line(point s,point e){this->s=s;this->e=e;}//????????//?????0??????,?1????,?0????,?2???//???????2?,??????pair<int,point> operator *(line a){point res = s;if(sgn((s-e)/(a.s-a.e))==0){if(sgn((s-a.e)/(a.s-a.e))==0)return make_pair(0,res);//??return make_pair(1,res);//??}double t=(s-a.s)/(a.s-a.e)/((s-e)/(a.s-a.e));res.x+=(e.x-s.x)*t;res.y+=(e.y-s.y)*t;return make_pair(2,res);}};//a?b??c point transxy(point a,point b,double c){point t1=a-b,t2=point(sin(c),cos(c));return b+point(t1/t2,t1*t2);}//?????double dis(point a,point b){point t=a-b;return sqrt(t*t);}//??????bool inter(line l1,line l2){returnmax(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&sgn((l2.s-l1.e)/(l1.s-l1.e))*sgn((l2.e-l1.e)/(l1.s-l1.e))<=0&&sgn((l1.s-l2.e)/(l2.s-l2.e))*sgn((l1.e-l2.e)/(l2.s-l2.e))<=0;}//????l1???l2????bool seg_inter_line(line l1,line l2) {return sgn((l2.s-l1.e)/(l1.s-l1.e))*sgn((l2.e-l1.e)/(l1.s-l1.e))<=0;}//??????????res,?????????point point_to_line(point p,line l){point res;double t=(p-l.s)*(l.e-l.s)/((l.e-l.s)*(l.e-l.s));res.x=l.s.x+(l.e.x-l.s.x)*t;res.y=l.s.y+(l.e.y-l.s.y)*t;return res;}//??????????????????point point_to_seg(point p,line l){point res;double t =(p-l.s)*(l.e-l.s)/((l.e-l.s)*(l.e-l.s));if(t>=0&&t<=1){res.x=l.s.x+(l.e.x - l.s.x)*t;res.y=l.s.y+(l.e.y - l.s.y)*t;}elseres=dis(p,l.s)<dis(p,l.e)?l.s:l.e;return res;}//???????double calc_area(point p[],int n){double res = 0;for(int i = 0;i < n;i++)res += (p[i]/p[(i+1)%n])/2;return fabs(res);}//???????bool onseg(point p,line l){returnsgn((l.s-p)/(l.e-p))==0&&sgn((p.x-l.s.x)*(p.x-l.e.x))<=0&&sgn((p.y-l.s.y)*(p.y-l.e.y))<=0;}//?????????//???????,????????(??????????<0??>0)int inconvexpoly(point a,point p[],int n){for(int i = 0;i < n;i++){if(sgn((p[i]-a)/(p[(i+1)%n]-a))<0)return 1;//???? if(onseg(a,line(p[i],p[(i+1)%n])))return 0;//?????}return -1;//????}//??????????int point_in_poly(point a,point p[],int n){int wn=0;for(int i=0;i<n;i++){if(onseg(a,line(p[i],p[(i+1)%n])))return 0;//????? int t1=sgn((p[(i+1)%n]-p[i])/point(a-p[i]));int t2=sgn(p[i].y-a.y),t3=sgn(p[(i+1)%n].y-a.y);if(t1>0&&t2<=0&&t3>0) wn++;if(t1<0&&t3<=0&&t2>0) wn--;}return wn!=0?-1:1;//-1???,1??? }//??????//?????//??????????????????//????1~n-1bool isconvex(point p[],int n){bool s[3];memset(s,false,sizeof(s));for(int i=0;i<n;i++){s[sgn((p[(i+1)%n]-p[i])/(p[(i+2)%n]-p[i]))+1]=1;if(s[0] && s[2])return 0;}return 1;}//??int covexhull(point p[],int n,point ch[]){sort(p,p+n);int m=0;for(int i=0;i<n;i++){while(m>1&&sgn((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>-1;i--){while(m>k&&sgn((ch[m-1]-ch[m-2])/(p[i]-ch[m-2]))<=0)m--;ch[m++]=p[i];}if(n>1)m--;return m;}point bx[2][110];int main(){int n,m,cs=0;while(scanf("%d",&n)!=EOF){for(int i=0;i<n;i++)scanf("%lf%lf",&bx[0][i].x,&bx[0][i].y);scanf("%d",&m);for(int i=0;i<m;i++)scanf("%lf%lf",&bx[1][i].x,&bx[1][i].y);bool flag=0;for(int i=0;!flag&&i<n;i++){line a=line(bx[0][i],bx[0][(i+1)%n]);vector<point>bb;bb.push_back(bx[0][i]);bb.push_back(bx[0][(i+1)%n]);for(int j=0;j<m;j++){line b=line(bx[1][j],bx[1][(j+1)%m]);if(!inter(a,b))continue; pair<int,point> res=a*b;if(res.first==2)bb.push_back(res.second);}sort(bb.begin(),bb.end());int len=bb.size();for(int j=0;!flag&&j<len;j++){point t=bb[j]+bb[(j+1)%len];t.x/=2;t.y/=2;if(point_in_poly(bb[j],bx[1],m)<0||point_in_poly(t,bx[1],m)<0)flag=1;}}for(int i=0;!flag&&i<m;i++){line a=line(bx[1][i],bx[1][(i+1)%m]);vector<point>bb;bb.push_back(bx[1][i]);bb.push_back(bx[1][(i+1)%m]);for(int j=0;j<n;j++){line b=line(bx[0][j],bx[0][(j+1)%n]);if(!inter(a,b))continue; pair<int,point> res=a*b;if(res.first==2)bb.push_back(res.second);}sort(bb.begin(),bb.end());int len=bb.size();for(int j=0;!flag&&j<len;j++){point t=bb[j]+bb[(j+1)%len];t.x/=2;t.y/=2;if(point_in_poly(bb[j],bx[0],n)<0||point_in_poly(t,bx[0],n)<0)flag=1;}}if(!flag){bool flag1=0;for(int i=0;!flag1&&i<n;i++)if(point_in_poly(bx[0][i],bx[1],m)==1)flag1=1;if(!flag1)flag=1;}printf("Case %d: %s\n",++cs,flag?"Yes":"No");}}
Given two simple polygons, your task is to determine whether they have a non-empty common area.
Note that the two rectangles in gure (a) share a segment, but no common area at all.
By \simple polygon", we mean the polygons will not be self-intersecting or self-touching, and will
not have duplicated vertices or adjacent collinear segments.
Note: be sure to test your program with many special cases.
Input
There will be at most 100 test cases. Each test case contains two lines, one for each polygon. Each
polygon begins with an integer n (3 n 100), the number of vertices, then n pairs of integers (x; y)
(
- UVA 12931 Common Area
- UVa 10589 - Area
- UVA 1641 - ASCII Area
- UVa 1641 - ASCII Area
- UVa 1641 - ASCII Area
- UVa 1641 ASCII Area
- ASCII Area UVA
- UVA 10522 Height to Area
- UVa 10522 - Height to Area
- UVa 10522 - Height to Area
- UVa 10522 - Height to Area
- UVA 1641(p337)----ASCII Area
- uva-10252-Common Permutation
- UVa 10252 Common Permutation
- uva 10252Common Permutation
- Uva 10252 - Common Permutation
- UVa 10252 - Common Permutation
- UVA--10252 Common Permutation
- uva 247 calling circles
- su -认证失败
- css3实现动画横幅
- Jexus Web Server完整 图文两种形式配置教程
- 黑马程序员--UDP通信
- UVA 12931 Common Area
- 二分图多重匹配问题
- layout
- Android ViewPager多页面滑动切换以及动画效果
- Chisel Tutorial(九)——状态单元
- ACM —— 1009 Edge Detection
- Android异步更新UI的四种方式
- hadoop搭建环境(1)
- java 动态代理实现