TOJ 2560 Geometric Shapes(判断多边形是否相交)

描述

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

时间: 2024-08-04 14:09:45

TOJ 2560 Geometric Shapes(判断多边形是否相交)的相关文章

POJ 3449 Geometric Shapes 判断多边形相交

题意不难理解,给出多个多边形,输出多边形间的相交情况(嵌套不算相交),思路也很容易想到.枚举每一个图形再枚举每一条边 恶心在输入输出,不过还好有sscanf(),不懂可以查看cplusplus网站 根据正方形对角的两顶点求另外两个顶点公式: x2 = (x1+x3-y3+y1)/2; y2 = (x3-x1+y1+y3)/2; x4= (x1+x3+y3-y1)/2; y4 = (-x3+x1+y1+y3)/2; 还有很多细节要处理 #include <iostream> #include &

POJ 3449 Geometric Shapes --计算几何,线段相交

题意: 给一些多边形或线段,输出与每一个多边形或线段的有哪一些多边形或线段. 解法: 想法不难,直接暴力将所有的图形处理成线段,然后暴力枚举,相交就加入其vector就行了.主要是代码有点麻烦,一步一步来吧. 还有收集了一个线段旋转的函数. Vector Rotate(Point P,Vector A,double rad){ //以P为基准点把向量A旋转rad return Vector(P.x+A.x*cos(rad)-A.y*sin(rad),P.y+A.x*sin(rad)+A.y*co

zoj 1010 (线段相交判断+多边形求面积)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=10 Area Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Jerry, a middle school student, addicts himself to mathematical research. Maybe the problems he has thought are

zoj 1010 Area 判断线段是否相交(把线段扩充一倍后 好处理) + 多边形求面积

题目来源: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=10 题意:  给定n个点的, 如果这n个点不能形成多边形 以及 n < 3 时, 输出, "Impossible",  否则 输出 多边形的面积. 分析: 这题主要在 分析  n 个点 是否形成 多边形.  枚举 每条边,  看 这条边 是否与 其他 n - 3 条边 不规范相交. (当处理 其他 边时, 我们采用 扩充线段一倍) 代码如下: con

POJ 3449 Geometric Shapes (求正方形的另外两点)

Geometric Shapes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1470   Accepted: 622 Description While creating a customer logo, ACM uses graphical utilities to draw a picture that can later be cut into special fluorescent materials. To

POJ 3449 Geometric Shapes

判断两个多边形是否相交,只需判断边是否有相交. 编码量有点大,不过思路挺简单的. #include<cstdio> #include<cstring> #include<vector> #include<cmath> #include<string> #include<queue> #include<list> #include<algorithm> #include<iostream> using

POJ1584 判断多边形是否为凸多边形,并判断点到直线的距离

求点到直线的距离: double dis(point p1,point p2){   if(fabs(p1.x-p2.x)<exp)//相等的  {    return fabs(p2.x-pegx);    }  else     {   double k=(p2.y-p1.y)/(p2.x-p1.x);   double b=p2.y-k*p2.x;   return fabs(k*pegx-pegy+b)/sqrt(k*k+1);//返回的是距离的   }}判断多边形是否为凸多边形 if

Jack Straws(判断线段是否相交 + 并查集)

/** http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1840  题意: 判断线段是否相交  (包括间接相交) 输入: N(代表有n条线段) sx  sy  ex  ey(一条直线的两端点的坐标) : : : a b(判断第a条和第b条线段是否相交) : : : 0 0输入0 0 询问结束 输出:若相交输出  CONNECTED 否则         NOT CONNECTED */ #includ

poj1584 A Round Peg in a Ground Hole 判断多边形凹凸,点到线的距离【基础计算几何】

大致思路:首先对于所给的洞的点,判断是否是凸多边形,图形的输入和输出可以是顺时针或者逆时针,而且允许多点共线 Debug 了好几个小时,发现如下问题 判断三点是否共线,可用斜率公式判断 POINT point_A, point_B, point_C; if(point_A.x == point_B.x || point_B.x == point_C.x){ if(point_A.x == point_B.x && point_B.x == point_C.x) continue; }els