Topic link: https: //vjudge.net/problem/POJ-3449
The meaning of problems: a plurality of given geometry, the geometry is determined for each case intersect with other geometry, and sequentially outputs.
Ideas:
First thing to know is how to find the other two points from the two diagonals of the square points, is known for example (x0, y0), (x2, y2), then:
x1+x3=x0+x2,
x1-x3=y2-y0,
y1 + y3 = y0 + y2,
y1-y3=x0-x2
After the violence on the enumeration, enumeration of all sides of all geometry, line segment intersection judgment geometry intersect. Input Output This question is very sick.
AC Code:
#include<cstdio> #include<algorithm> #include<cstdlib> #include<cmath> #include<vector> #include<cstring> using namespace std; const double eps=1e-8; int sgn(double x){ if(abs(x)<eps) 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<a.num;++i) for(int j=0;j<b.num;++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 ] .e-sp [ 0 ] .pt [ 0 ] .e)) / 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 ] .e)) / 2 ; sp [ 0 ] .pt [ 1 ] .the = (sp [ 0 ] .pt [ 0 ] + .e sp [ 0 ] .pt [ 2 ] .the + (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<sp[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<sp[n].num;++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<n;++i){ printf("%c ",sp[i].id); vc.clear(); for(int j=0;j<n;++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<vc.size()-1;++j) printf("%c, ",vc[j]); printf("and %c\n",vc[vc.size()-1]); } } printf("\n"); } return 0; }