poj3449 Geometric Shapes【计算几何】

含【判断线段相交】、【判断两点在线段两侧】、【判断三点共线】、【判断点在线段上】模板
 
Geometric Shapes
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions:2105 Accepted: 883

Description

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

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 (“.”).

Output

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.

Sample Input

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)
-
.

Sample Output

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

Source

 

题意:

给定一些图形及其坐标(可能是正方形、矩形、三角形、多边形、线段),问每一个图形和其他哪些图形相交

思路:

因为数据量很小,暴力判断每一个图形的每一条边和其他每一个图形的每一条边是否相交

输入输出有点恶心 没别的了

  1 //#include <bits/stdc++.h>
  2 #include<iostream>
  3 #include<cmath>
  4 #include<algorithm>
  5 #include<stdio.h>
  6 #include<cstring>
  7 
  8 using namespace std;
  9 typedef long long int LL;
 10 
 11 #define zero(x) (((x) > 0? (x) : -(x)) < eps)
 12 //enum shape{"square", "rectangle", "line", "triangle", "polygon"};
 13 const double eps = 1e-8;
 14 const double pi = 3.141592654;
 15 struct point{
 16     int x,y;
 17     point()
 18     {
 19         x = 0;
 20         y = 0;
 21     }
 22     point(int _x, int _y)
 23     {
 24         x = _x;
 25         y = _y;
 26     }
 27 };
 28 
 29 struct node{
 30     int sh;
 31     int nvertices;
 32     point p[25];
 33     char name;
 34     /*bool operator < (const node &b)const
 35     {
 36         name < b.name;
 37     }*/
 38 };
 39 node shapes[30];
 40 
 41 bool cmp(node a, node b)
 42 {
 43     return a.name < b.name;
 44 }
 45 
 46 double xmult(point p1, point p2, point p0)
 47 {
 48     return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
 49 }
 50 
 51 //判断三点共线
 52 int dots_inline(point p1, point p2, point p3)
 53 {
 54     return zero(xmult(p1, p2, p3));
 55 }
 56 
 57 //判断点是否在线段上,包括端点
 58 int dot_online_in(point p, point l1, point l2)
 59 {
 60     return zero(xmult(p, l1, l2)) && (l1.x - p.x) * (l2.x - p.x) < eps && (l1.y - p.y) * (l2.y - p.y) < eps;
 61 }
 62 
 63 //判断两点在线段同侧,点在线段上返回0
 64 int same_side(point p1, point p2, point l1, point l2)
 65 {
 66     return xmult(l1, p1, l2) * xmult(l1, p2, l2) > eps;
 67 }
 68 
 69 //判断两线段相交,包括端点和部分重合
 70 int intersect_in(point u1, point u2, point v1, point v2)
 71 {
 72     if(!dots_inline(u1, u2, v1) || !dots_inline(u1, u2, v2)){
 73         return !same_side(u1, u2, v1, v2) && !same_side(v1, v2, u1, u2);
 74     }
 75 
 76     return dot_online_in(u1, v1, v2) || dot_online_in(u2, v1, v2) || dot_online_in(v1, u1, u2) || dot_online_in(v2, u1, u2);
 77 
 78 }
 79 
 80 /*bool inter(point u1, point u2, point v1, point v2)
 81 {
 82     return
 83     max(u1.x, u2.x) >= min(v1.x, v2.x) &&
 84     max(v1.x, v2.x) >= min(u1.x, u2.x) &&
 85     max(u1.y, u2.y) >= min(v1.y, v2.y) &&
 86     max(v1.y, v2.y) >= min(u1.y, u2.y) &&
 87     sgn((v))
 88 }*/
 89 
 90 char str[30];
 91 bool inter[30];
 92 int main()
 93 {
 94     int cnt = 0;
 95     while(scanf("%s", str) == 1){
 96         cnt = 0;
 97         if(str[0] =='.')break;
 98         shapes[0].name = str[0];
 99         scanf("%s", str);
100         if(strcmp(str, "square") == 0){
101             shapes[0].nvertices = 4;
102             int x0, x2, y0, y2;
103             //cin>>x0>>y0>>x2>>y2;
104             scanf(" (%d,%d)", &x0, &y0);
105             scanf(" (%d,%d)", &x2, &y2);
106             shapes[0].p[0] = point(x0, y0);
107             shapes[0].p[2] = point(x2, y2);
108             shapes[0].p[1] = point((x0 + x2 + y2 - y0) / 2, (y0 + y2 + x0 - x2) / 2);
109             shapes[0].p[3] = point((x0 + x2 - y2 + y0) / 2, (y0 + y2 - x0 + x2) / 2);
110         }
111         else if(strcmp(str, "line") == 0){
112             shapes[0].nvertices = 2;
113             int x0, y0, x1, y1;
114             scanf(" (%d,%d) (%d,%d)", &x0, &y0, &x1, &y1);
115             shapes[0].p[0] = point(x0, y0);
116             shapes[0].p[1] = point(x1, y1);
117         }
118         else if(strcmp(str, "triangle") == 0){
119             shapes[0].nvertices = 3;
120             int x0, y0, x1, y1, x2, y2;
121             scanf(" (%d,%d) (%d,%d) (%d,%d)", &x0, &y0, &x1, &y1, &x2, &y2);
122             shapes[0].p[0] = point(x0, y0);
123             shapes[0].p[1] = point(x1, y1);
124             shapes[0].p[2] = point(x2, y2);
125         }
126         else if(strcmp(str, "rectangle") == 0){
127             shapes[0].nvertices = 4;
128             int x0, y0, x1, y1, x2, y2;
129             scanf(" (%d,%d) (%d,%d) (%d,%d)", &x0, &y0, &x1, &y1, &x2, &y2);
130             shapes[0].p[0] = point(x0, y0);
131             shapes[0].p[1] = point(x1, y1);
132             shapes[0].p[2] = point(x2, y2);
133             shapes[0].p[3] = point(x2 + x0 - x1, y2 + y0 - y1);
134         }
135         else if(strcmp(str, "polygon") == 0){
136             scanf("%d", &shapes[0].nvertices);
137             for(int i = 0; i < shapes[0].nvertices; i++){
138                 scanf(" (%d,%d)", &shapes[0].p[i].x, &shapes[0].p[i].y);
139             }
140         }
141         cnt++;
142         while(scanf("%s", str) == 1){
143             if(str[0] == '-')break;
144             shapes[cnt].name = str[0];
145             scanf("%s", str);
146             if(strcmp(str, "square") == 0){
147                 shapes[cnt].nvertices = 4;
148                 int x0, x2, y0, y2;
149                 scanf(" (%d,%d) (%d,%d)", &x0, &y0, &x2, &y2);
150                 shapes[cnt].p[0] = point(x0, y0);
151                 shapes[cnt].p[2] = point(x2, y2);
152                 shapes[cnt].p[1] = point((x0 + x2 + y2 - y0) / 2, (y0 + y2 + x0 - x2) / 2);
153                 shapes[cnt].p[3] = point((x0 + x2 - y2 + y0) / 2, (y0 + y2 - x0 + x2) / 2);
154             }
155             else if(strcmp(str, "line") == 0){
156                 shapes[cnt].nvertices = 2;
157                 int x0, y0, x1, y1;
158                 scanf(" (%d,%d) (%d,%d)", &x0, &y0, &x1, &y1);
159                 shapes[cnt].p[0] = point(x0, y0);
160                 shapes[cnt].p[1] = point(x1, y1);
161             }
162             else if(strcmp(str, "triangle") == 0){
163                 shapes[cnt].nvertices = 3;
164                 int x0, y0, x1, y1, x2, y2;
165                 scanf(" (%d,%d) (%d,%d) (%d,%d)", &x0, &y0, &x1, &y1, &x2, &y2);
166                 shapes[cnt].p[0] = point(x0, y0);
167                 shapes[cnt].p[1] = point(x1, y1);
168                 shapes[cnt].p[2] = point(x2, y2);
169             }
170             else if(strcmp(str, "rectangle") == 0){
171                 shapes[cnt].nvertices = 4;
172                 int x0, y0, x1, y1, x2, y2;
173                 scanf(" (%d,%d) (%d,%d) (%d,%d)", &x0, &y0, &x1, &y1, &x2, &y2);
174                 shapes[cnt].p[0] = point(x0, y0);
175                 shapes[cnt].p[1] = point(x1, y1);
176                 shapes[cnt].p[2] = point(x2, y2);
177                 shapes[cnt].p[3] = point(x2 + x0 - x1, y2 + y0 - y1);
178             }
179             else if(strcmp(str, "polygon") == 0){
180                 scanf("%d", &shapes[cnt].nvertices);
181                 for(int i = 0; i < shapes[cnt].nvertices; i++){
182                     scanf(" (%d,%d)", &shapes[cnt].p[i].x, &shapes[cnt].p[i].y);
183                 }
184             }
185             cnt++;
186         }
187 
188         sort(shapes, shapes + cnt, cmp);
189         for(int i = 0; i < cnt; i++){
190             printf("%c ", shapes[i].name);
191             memset(inter, 0, sizeof(inter));
192             int ans = 0;
193             for(int j = 0; j < cnt; j++){
194                 if(i == j)continue;
195                 for(int a = 0; a < shapes[i].nvertices; a++){
196                     for(int b = 0; b < shapes[j].nvertices; b++){
197                         if(intersect_in(shapes[i].p[a], shapes[i].p[(a + 1) % shapes[i].nvertices], shapes[j].p[b], shapes[j].p[(b + 1) % shapes[j].nvertices])){
198                             inter[j] = true;
199                         }
200                     }
201                 }
202                 if(inter[j] == true){
203                     ans++;
204                 }
205             }
206             if(ans == 0){
207                 printf("has no intersections\n");
208             }
209             else{
210                 printf("intersects with");
211                 if(ans == 1){
212                     for(int j = 0; j < cnt; j++){
213                         if(inter[j]){
214                             printf(" %c\n", shapes[j].name);
215                         }
216                     }
217                 }
218                 else if(ans == 2){
219                     int tmp = 0;
220                     for(int j = 0; j < cnt; j++){
221                         if(inter[j]){
222                             if(tmp){
223                                 printf(" and");
224                             }
225                             printf(" %c", shapes[j].name);
226                             tmp++;
227                         }
228                     }
229                     printf("\n");
230                 }
231                 else{
232                     int tmp = 0;
233                     for(int j = 0; j < cnt; j++){
234                         if(inter[j]){
235                             if(tmp == ans - 1){
236                                 printf(" and %c", shapes[j].name);
237                             }
238                             else{
239                                 printf(" %c,", shapes[j].name);
240                             }
241                             tmp++;
242                         }
243 
244                     }
245                     printf("\n");
246                 }
247             }
248         }
249         printf("\n");
250     }
251     return 0;
252 }

 

转载于:https://www.cnblogs.com/wyboooo/p/9737202.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值