描述
While creating a customer logo, ACM uses graphical utilities to draw a picture that can later be cut into special fluorescent materials. To ensure proper processing, the shapes in the picture cannot intersect. However, some logos contain such intersecting shapes. It is necessary to detect them and decide how to change the picture.
Given a set of geometric shapes, you are to determine all of their intersections. Only outlines are considered, if a shape is completely inside another one, it is not counted as an intersection.
输入
Input contains several pictures. Each picture describes at most 26 shapes, each specified on a separate line. The line begins with an uppercase letter that uniquely identifies the shape inside the corresponding picture. Then there is a kind of the shape and two or more points, everything separated by at least one space. Possible shape kinds are:
? square: Followed by two distinct points giving the opposite corners of the square.
?
rectangle: Three points are given, there will always be a right angle
between the lines connecting the first point with the second and the
second with the third.
? line: Specifies a line segment, two distinct end points are given.
? triangle: Three points are given, they are guaranteed not to be co-linear.
?
polygon: Followed by an integer number N (3 ≤ N ≤ 20) and N points
specifying vertices of the polygon in either clockwise or anti-clockwise
order. The polygon will never intersect itself and its sides will have
non-zero length.
All points are always given as two integer coordinates X and Y
separated with a comma and enclosed in parentheses. You may assume that
|X|, |Y | ≤ 10000.
The picture description is terminated by a line containing a single
dash (“-”). After the last picture, there is a line with one dot (“.”).
输出
For
each picture, output one line for each of the shapes, sorted
alphabetically by its identifier (X). The line must be one of the
following:
? “X has no intersections”, if X does not intersect with any other shapes.
? “X intersects with A”, if X intersects with exactly 1 other shape.
? “X intersects with A and B”, if X intersects with exactly 2 other shapes.
? “X intersects with A, B, . . ., and Z”, if X intersects with more than 2 other shapes.
Please note that there is an additional comma for more than two
intersections. A, B, etc. are all intersecting shapes, sorted
alphabetically.
Print one empty line after each picture, including the last one.
样例输入
A square (1,2) (3,2)
F line (1,3) (4,4)
W triangle (3,5) (5,5) (4,3)
X triangle (7,2) (7,4) (5,3)
S polygon 6 (9,3) (10,3) (10,4) (8,4) (8,1) (10,2)
B rectangle (3,3) (7,5) (8,3)
-
B square (1,1) (2,2)
A square (3,3) (4,4)
-
.
样例输出
A has no intersections
B intersects with S, W, and X
F intersects with W
S intersects with B
W intersects with B and F
X intersects with B
A has no intersections
B has no intersections
题意
给你多边形,如果在内部则视为不相交,判断哪些是相交的
题解
把多边形按边存,如果两个多边形相交,那么一定存在两条边相交
判断两条边相交,先用俩矩形快速排斥,再用跨立实验,如果ab和cd相交,那么cd的两端一定在向量ab的两侧,可以通过abc和abd叉积相乘<0判断是否相交
然后就是存多边形,这里正方形和矩形另外的点得通过向量计算一下
PS:码农题,读输入,输出都恶心,题目不算太难
代码
1 #include<cstdio> 2 #include<algorithm> 3 #include<vector> 4 #include<set> 5 using namespace std; 6 struct point 7 { 8 double x,y; 9 point(double x=0,double y=0):x(x),y(y){} 10 }; 11 bool judge(point a,point b,point c,point d) 12 { 13 if(!(min(a.x,b.x)<=max(c.x,d.x)&&min(c.y,d.y)<=max(a.y,b.y)&&min(c.x,d.x)<=max(a.x,b.x)&&min(a.y,b.y)<=max(c.y,d.y))) 14 return false; 15 double u,v,w,z; 16 u=(c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y); 17 v=(d.x-a.x)*(b.y-a.y)-(b.x-a.x)*(d.y-a.y); 18 w=(a.x-c.x)*(d.y-c.y)-(d.x-c.x)*(a.y-c.y); 19 z=(b.x-c.x)*(d.y-c.y)-(d.x-c.x)*(b.y-c.y); 20 return (u*v<=0.00000001&&w*z<=0.00000001); 21 } 22 vector<point>G[27]; 23 24 int main() 25 { 26 //freopen("A.txt","w",stdout); 27 int m; 28 double a,b; 29 char op[3],shape[20]; 30 while(scanf("%s",op)!=EOF,op[0]!=‘.‘) 31 { 32 for(int i=0;i<26;i++)G[i].clear(); 33 while(op[0]!=‘-‘) 34 { 35 int cnt=op[0]-‘A‘; 36 scanf("%s",shape); 37 if(shape[0]==‘s‘)///正方形 38 { 39 for(int i=1;i<=2;i++) 40 { 41 scanf(" (%lf,%lf)",&a,&b); 42 G[cnt].push_back(point(a,b)); 43 } 44 double A=G[cnt][0].x,B=G[cnt][0].y,C=G[cnt][1].x,D=G[cnt][1].y; 45 G[cnt].push_back(point((A*1.0+B+C-D)/2.0,(-A*1.0+B+C+D)/2.0)); 46 G[cnt].push_back(point((A*1.0-B+C+D)/2.0,(A*1.0+B-C+D)/2.0)); 47 swap(G[cnt][1],G[cnt][2]); 48 G[cnt].push_back(G[cnt][0]); 49 } 50 if(shape[0]==‘r‘)///矩形 51 { 52 for(int i=1;i<=3;i++) 53 { 54 scanf(" (%lf,%lf)",&a,&b); 55 G[cnt].push_back(point(a,b)); 56 } 57 G[cnt].push_back(point(G[cnt][0].x*1.0+G[cnt][2].x-G[cnt][1].x,G[cnt][0].y*1.0+G[cnt][2].y-G[cnt][1].y)); 58 G[cnt].push_back(G[cnt][0]); 59 } 60 if(shape[0]==‘l‘)///线 61 { 62 for(int i=1;i<=2;i++) 63 { 64 scanf(" (%lf,%lf)",&a,&b); 65 G[cnt].push_back(point(a,b)); 66 } 67 } 68 if(shape[0]==‘t‘)///三角形 69 { 70 for(int i=1;i<=3;i++) 71 { 72 scanf(" (%lf,%lf)",&a,&b); 73 G[cnt].push_back(point(a,b)); 74 } 75 G[cnt].push_back(G[cnt][0]); 76 } 77 if(shape[0]==‘p‘)///多边形 78 { 79 scanf("%d",&m); 80 for(int i=1;i<=m;i++) 81 { 82 scanf(" (%lf,%lf)",&a,&b); 83 G[cnt].push_back(point(a,b)); 84 } 85 G[cnt].push_back(G[cnt][0]); 86 } 87 scanf("%s",op); 88 } 89 for(int i=0;i<26;i++) 90 { 91 int flag=0; 92 set<int>SET; 93 if((int)G[i].size()==0)continue; 94 for(int j=0;j<(int)G[i].size()-1;j++) 95 { 96 for(int k=0;k<26;k++) 97 { 98 if((int)G[k].size()==0||i==k)continue; 99 for(int l=0;l<(int)G[k].size()-1;l++) 100 { 101 if(judge(G[i][j],G[i][j+1],G[k][l],G[k][l+1])) 102 { 103 flag=1; 104 SET.insert(k); 105 break; 106 } 107 } 108 } 109 } 110 if(flag==1) 111 { 112 vector<int>VEC(SET.begin(),SET.end()); 113 int len=(int)VEC.size(); 114 printf("%c intersects with",i+‘A‘); 115 if(len==2) 116 {printf(" %c and %c\n",VEC[0]+‘A‘,VEC[1]+‘A‘);continue;} 117 for(int l=0;l<len-1;l++) 118 printf(" %c,",VEC[l]+‘A‘); 119 if(len>1) 120 printf(" and %c",VEC[len-1]+‘A‘); 121 else 122 printf(" %c",VEC[len-1]+‘A‘); 123 printf("\n"); 124 } 125 else 126 printf("%c has no intersections\n",i+‘A‘); 127 } 128 printf("\n"); 129 } 130 return 0; 131 }
原文地址:https://www.cnblogs.com/taozi1115402474/p/9465495.html