题目链接:https://vjudge.net/problem/POJ-3449
题意:给出若干几何体,判断每个几何体与其它几何体的相交情况,并依次输出。
思路:
首先要知道的是根据正方形对角线的两个点怎么求其它两个点,比如已知(x0,y0),(x2,y2),那么:
x1+x3=x0+x2,
x1-x3=y2-y0,
y1+y3=y0+y2,
y1-y3=x0-x2
之后就暴力枚举了,枚举所有几何体的所有边,用线段相交判断几何体相交。这题的输入输出很恶心。
AC代码:
#include#include #include #include #include #include using namespace std; const double eps=1e-8; int sgn(double x){ if(abs(x) return 0; else if(x<0) return -1; else return 1; } struct Point{ double x,y; Point(){} Point(double xx,double yy){ x=xx,y=yy; } Point operator + (const Point& b)const{ return Point(x+b.x,y+b.y); } Point operator - (const Point& b)const{ return Point(x-b.x,y-b.y); } double operator ^ (const Point& b)const{ return x*b.y-y*b.x; } }; struct Line{ Point s,e; Line(){} Line(Point ss,Point ee){ s=ss,e=ee; } }; bool inter(Line l1,Line l2){ return max(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; } struct node{ char id; int num; Point pt[25]; }sp[30]; bool cmp(node a,node b){ return a.id<b.id; } bool check(node a,node b){ for(int i=0;i i) for(int j=0;j j) if(inter(Line(a.pt[i],a.pt[(i+1)%a.num]),Line(b.pt[j],b.pt[(j+1)%b.num]))) return true; return false; } vector<char> vc; int n; char str[30]; int main(){ while(scanf("%s",str),str[0]!='.'){ sp[0].id=str[0]; scanf("%s",str); if(strcmp(str,"square")==0){ sp[0].num=4; scanf(" (%lf,%lf)",&sp[0].pt[0].x,&sp[0].pt[0].y); scanf(" (%lf,%lf)",&sp[0].pt[2].x,&sp[0].pt[2].y); sp[0].pt[1].x=(sp[0].pt[0].x+sp[0].pt[2].x+(sp[0].pt[2].y-sp[0].pt[0].y))/2; sp[0].pt[3].x=(sp[0].pt[0].x+sp[0].pt[2].x-(sp[0].pt[2].y-sp[0].pt[0].y))/2; sp[0].pt[1].y=(sp[0].pt[0].y+sp[0].pt[2].y+(sp[0].pt[0].x-sp[0].pt[2].x))/2; sp[0].pt[3].y=(sp[0].pt[0].y+sp[0].pt[2].y-(sp[0].pt[0].x-sp[0].pt[2].x))/2; } else if(strcmp(str,"rectangle")==0){ sp[0].num=4; scanf(" (%lf,%lf)",&sp[0].pt[0].x,&sp[0].pt[0].y); scanf(" (%lf,%lf)",&sp[0].pt[1].x,&sp[0].pt[1].y); scanf(" (%lf,%lf)",&sp[0].pt[2].x,&sp[0].pt[2].y); sp[0].pt[3].x=sp[0].pt[2].x+(sp[0].pt[0].x-sp[0].pt[1].x); sp[0].pt[3].y=sp[0].pt[2].y+(sp[0].pt[0].y-sp[0].pt[1].y); } else if(strcmp(str,"line")==0){ sp[0].num=2; scanf(" (%lf,%lf)",&sp[0].pt[0].x,&sp[0].pt[0].y); scanf(" (%lf,%lf)",&sp[0].pt[1].x,&sp[0].pt[1].y); } else if(strcmp(str,"triangle")==0){ sp[0].num=3; scanf(" (%lf,%lf)",&sp[0].pt[0].x,&sp[0].pt[0].y); scanf(" (%lf,%lf)",&sp[0].pt[1].x,&sp[0].pt[1].y); scanf(" (%lf,%lf)",&sp[0].pt[2].x,&sp[0].pt[2].y); } else{ scanf("%d",&sp[0].num); for(int i=0;i 0].num;++i) scanf(" (%lf,%lf)",&sp[0].pt[i].x,&sp[0].pt[i].y); } n=1; while(scanf("%s",str),str[0]!='-'){ sp[n].id=str[0]; scanf("%s",str); if(strcmp(str,"square")==0){ sp[n].num=4; scanf(" (%lf,%lf)",&sp[n].pt[0].x,&sp[n].pt[0].y); scanf(" (%lf,%lf)",&sp[n].pt[2].x,&sp[n].pt[2].y); sp[n].pt[1].x=(sp[n].pt[0].x+sp[n].pt[2].x+(sp[n].pt[2].y-sp[n].pt[0].y))/2; sp[n].pt[3].x=(sp[n].pt[0].x+sp[n].pt[2].x-(sp[n].pt[2].y-sp[n].pt[0].y))/2; sp[n].pt[1].y=(sp[n].pt[0].y+sp[n].pt[2].y+(sp[n].pt[0].x-sp[n].pt[2].x))/2; sp[n].pt[3].y=(sp[n].pt[0].y+sp[n].pt[2].y-(sp[n].pt[0].x-sp[n].pt[2].x))/2; } else if(strcmp(str,"rectangle")==0){ sp[n].num=4; scanf(" (%lf,%lf)",&sp[n].pt[0].x,&sp[n].pt[0].y); scanf(" (%lf,%lf)",&sp[n].pt[1].x,&sp[n].pt[1].y); scanf(" (%lf,%lf)",&sp[n].pt[2].x,&sp[n].pt[2].y); sp[n].pt[3].x=sp[n].pt[2].x+(sp[n].pt[0].x-sp[n].pt[1].x); sp[n].pt[3].y=sp[n].pt[2].y+(sp[n].pt[0].y-sp[n].pt[1].y); } else if(strcmp(str,"line")==0){ sp[n].num=2; scanf(" (%lf,%lf)",&sp[n].pt[0].x,&sp[n].pt[0].y); scanf(" (%lf,%lf)",&sp[n].pt[1].x,&sp[n].pt[1].y); } else if(strcmp(str,"triangle")==0){ sp[n].num=3; scanf(" (%lf,%lf)",&sp[n].pt[0].x,&sp[n].pt[0].y); scanf(" (%lf,%lf)",&sp[n].pt[1].x,&sp[n].pt[1].y); scanf(" (%lf,%lf)",&sp[n].pt[2].x,&sp[n].pt[2].y); } else{ scanf("%d",&sp[n].num); for(int i=0;i i) scanf(" (%lf,%lf)",&sp[n].pt[i].x,&sp[n].pt[i].y); } ++n; } sort(sp,sp+n,cmp); for(int i=0;i i){ printf("%c ",sp[i].id); vc.clear(); for(int j=0;j j) if(j!=i) if(check(sp[i],sp[j])) vc.push_back(sp[j].id); if(vc.size()==0){ printf("has no intersections\n"); } else if(vc.size()==1){ printf("intersects with %c\n",vc[0]); } else if(vc.size()==2){ printf("intersects with %c and %c\n",vc[0],vc[1]); } else{ printf("intersects with "); for(int j=0;j 1;++j) printf("%c, ",vc[j]); printf("and %c\n",vc[vc.size()-1]); } } printf("\n"); } return 0; }