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

  1. A square (1,2) (3,2)
  2. F line (1,3) (4,4)
  3. W triangle (3,5) (5,5) (4,3)
  4. X triangle (7,2) (7,4) (5,3)
  5. S polygon 6 (9,3) (10,3) (10,4) (8,4) (8,1) (10,2)
  6. B rectangle (3,3) (7,5) (8,3)
  7. -
  8. B square (1,1) (2,2)
  9. A square (3,3) (4,4)
  10. -
  11. .

Sample Output

  1. A has no intersections
  2. B intersects with S, W, and X
  3. F intersects with W
  4. S intersects with B
  5. W intersects with B and F
  6. X intersects with B
  7.  
  8. A has no intersections
  9. B has no intersections

Source

这个题目有个小的知识点就是知道正方形的对角线上的两个点坐标,求出其他两个点,我刚开始是用向量旋转做的,后来觉得一定还有其他的办法,因为正方形比较特殊,后来去网上搜到了可以直接用对角线上的坐标(x0,y0),(x2,y2)来求,对应关系如下:

x1 + x3 = x0 + x2;

x1 - x3 = y2 - y0;

y1 + y3 = y0 + y2;

y3 - y1 = x2 - x0;

求得另一对不相邻的顶点(x1,y1),(x3,y3)。

x1 = (x0 + x2 + y2 - y0) / 2

x3 = (x0 + x2 + y0 - y2) / 2

y1 = (y0 + y2 + x0 - x2) / 2

y3 = (y0 + y2 - x0 + x2) / 2

这样的话其他就没有难点了,就是遍历每个图形,看是否与其他的相交,如果不知道向量如何旋转的,请看http://www.cnblogs.com/Howe-Young/p/4466975.html

  1. /*************************************************************************
  2. > File Name: poj_3449.cpp
  3. > Author: Howe_Young
  4. > Mail: 1013410795@qq.com
  5. > Created Time: 2015年05月01日 星期五 18时38分39秒
  6. ************************************************************************/
  7.  
  8. #include <cstdio>
  9. #include <iostream>
  10. #include <cstring>
  11. #include <cmath>
  12. #include <cstdlib>
  13. #include <algorithm>
  14.  
  15. using namespace std;
  16. const int maxn = ;
  17. struct point{
  18. double x, y;
  19. };
  20. struct shape{
  21. char id;
  22. char name[];
  23. point edge[];
  24. char info[maxn];
  25. int index, num;
  26. };
  27. shape s[maxn];
  28. int n, i;
  29. char ch;
  30. void input()
  31. {
  32. s[i].id = ch;
  33. s[i].index = ;
  34. scanf("%s", s[i].name);
  35. if (strcmp(s[i].name, "square") == )
  36. {
  37. scanf(" (%lf, %lf) (%lf, %lf)", &s[i].edge[].x, &s[i].edge[].y, &s[i].edge[].x, &s[i].edge[].y);
  38. s[i].edge[].x = (s[i].edge[].x + s[i].edge[].x + s[i].edge[].y - s[i].edge[].y) / ;
  39. s[i].edge[].y = (s[i].edge[].y + s[i].edge[].y - s[i].edge[].x + s[i].edge[].x) / ;
  40. s[i].edge[].x = (s[i].edge[].x + s[i].edge[].x - s[i].edge[].y + s[i].edge[].y ) / ;
  41. s[i].edge[].y = (s[i].edge[].y + s[i].edge[].y + s[i].edge[].x - s[i].edge[].x) / ;
  42. s[i].num = ;
  43. return;
  44. }
  45. else if (strcmp(s[i].name, "rectangle") == )
  46. {
  47. scanf(" (%lf,%lf) (%lf, %lf) (%lf, %lf)", &s[i].edge[].x, &s[i].edge[].y, &s[i].edge[].x, &s[i].edge[].y, &s[i].edge[].x, &s[i].edge[].y);
  48. s[i].edge[].x = s[i].edge[].x + s[i].edge[].x - s[i].edge[].x;
  49. s[i].edge[].y = s[i].edge[].y + s[i].edge[].y - s[i].edge[].y;
  50. s[i].num = ;
  51. return;
  52. }
  53. else if (strcmp(s[i].name, "line") == )
  54. {
  55. scanf(" (%lf, %lf) (%lf, %lf)", &s[i].edge[].x, &s[i].edge[].y, &s[i].edge[].x, &s[i].edge[].y);
  56. s[i].num = ;
  57. return;
  58. }
  59. else if(strcmp(s[i].name, "triangle") == )
  60. {
  61. scanf(" (%lf, %lf) (%lf, %lf) (%lf, %lf)", &s[i].edge[].x, &s[i].edge[].y, &s[i].edge[].x, &s[i].edge[].y, &s[i].edge[].x, &s[i].edge[].y);
  62. s[i].num = ;
  63. return;
  64. }
  65. else
  66. {
  67. int k;
  68. scanf("%d", &k);
  69. for (int j = ; j < k; j++)
  70. scanf(" (%lf, %lf)", &s[i].edge[j].x, &s[i].edge[j].y);
  71. s[i].num = k;
  72. return;
  73. }
  74.  
  75. }
  76. double x_multi(point p1, point p2, point p3)
  77. {
  78. return (p3.x - p1.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p3.y - p1.y);
  79. }
  80. bool on_segment(point p1, point p2, point p3)
  81. {
  82. double minx, miny, maxx, maxy;
  83. if (p1.x > p2.x)
  84. {
  85. minx = p2.x;
  86. maxx = p1.x;
  87. }
  88. else
  89. {
  90. minx = p1.x;
  91. maxx = p2.x;
  92. }
  93. if (p1.y > p2.y)
  94. {
  95. miny = p2.y;
  96. maxy = p1.y;
  97. }
  98. else
  99. {
  100. maxy = p2.y;
  101. miny = p1.y;
  102. }
  103. return (p3.x >= minx && p3.x <= maxx && p3.y >= miny && p3.y <= maxy);
  104. }
  105. bool segment_intersect(point p1, point p2, point p3, point p4)
  106. {
  107. double d1 = x_multi(p1, p2, p3);
  108. double d2 = x_multi(p1, p2, p4);
  109. double d3 = x_multi(p3, p4, p1);
  110. double d4 = x_multi(p3, p4, p2);
  111. if (d1 * d2 < && d3 * d4 < )
  112. return true;
  113. if (d1 == && on_segment(p1, p2, p3))
  114. return true;
  115. if (d2 == && on_segment(p1, p2, p4))
  116. return true;
  117. if (d3 == && on_segment(p3, p4, p1))
  118. return true;
  119. if (d4 == && on_segment(p3, p4, p2))
  120. return true;
  121. return false;
  122. }
  123. bool intersected(shape a, shape b)
  124. {
  125. for (int p = ; p <= a.num; p++)
  126. {
  127. for (int q = ; q <= b.num; q++)
  128. {
  129. if (segment_intersect(a.edge[p - ], a.edge[p % a.num], b.edge[q - ], b.edge[q % b.num]))
  130. {
  131. return true;
  132. }
  133. }
  134. }
  135. return false;
  136. }
  137. bool cmp1(const shape a, const shape b)
  138. {
  139. return a.id < b.id;
  140. }
  141. bool cmp2(const char a, const char b)
  142. {
  143. return a < b;
  144. }
  145. void output(shape a)
  146. {
  147. if (a.index == )
  148. {
  149. printf("%c has no intersections\n", a.id);
  150. return;
  151. }
  152. if (a.index == )
  153. {
  154. printf("%c intersects with %c\n", a.id, a.info[]);
  155. return;
  156. }
  157. if (a.index == )
  158. {
  159. printf("%c intersects with %c and %c\n", a.id, a.info[], a.info[]);
  160. return;
  161. }
  162. printf("%c intersects with %c, ", a.id, a.info[]);
  163. for (int t = ; t < a.index - ; t++)
  164. printf("%c, ", a.info[t]);
  165. printf("and %c\n", a.info[a.index - ]);
  166.  
  167. }
  168. int main()
  169. {
  170. while (cin >> ch && ch != '.')
  171. {
  172. memset(s, , sizeof(s));
  173. i = ;
  174. if (ch == '-')
  175. continue;
  176. input();
  177. while (cin >> ch)
  178. {
  179. i++;
  180. if (ch == '-')
  181. break;
  182. input();
  183. }
  184. n = i;
  185. for (int t = ; t < n; t++)
  186. {
  187. for (int j = t + ; j < n; j++)
  188. {
  189. if (intersected(s[t], s[j]))
  190. {
  191. s[t].info[s[t].index++] = s[j].id;
  192. s[j].info[s[j].index++] = s[t].id;
  193. }
  194. }
  195. }
  196. sort(s, s + n, cmp1);
  197. for (int t = ; t < n; t++)
  198. {
  199. sort(s[t].info, s[t].info + s[t].index, cmp2);
  200. output(s[t]);
  201. }
  202. puts("");
  203. }
  204. return ;
  205. }

POJ 3449 Geometric Shapes (求正方形的另外两点)的更多相关文章

  1. POJ 3449 Geometric Shapes(判断几个不同图形的相交,线段相交判断)

    Geometric Shapes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1243   Accepted: 524 D ...

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

    题意: 给一些多边形或线段,输出与每一个多边形或线段的有哪一些多边形或线段. 解法: 想法不难,直接暴力将所有的图形处理成线段,然后暴力枚举,相交就加入其vector就行了.主要是代码有点麻烦,一步一 ...

  3. POJ 3449 Geometric Shapes 判断多边形相交

    题意不难理解,给出多个多边形,输出多边形间的相交情况(嵌套不算相交),思路也很容易想到.枚举每一个图形再枚举每一条边 恶心在输入输出,不过还好有sscanf(),不懂可以查看cplusplus网站 根 ...

  4. 简单几何(线段相交)+模拟 POJ 3449 Geometric Shapes

    题目传送门 题意:给了若干个图形,问每个图形与哪些图形相交 分析:题目说白了就是处理出每个图形的线段,然后判断是否相交.但是读入输出巨恶心,就是个模拟题加上线段相交的判断,我第一次WA不知道输出要按字 ...

  5. POJ 3449 Geometric Shapes

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

  6. TZOJ 2560 Geometric Shapes(判断多边形是否相交)

    描述 While creating a customer logo, ACM uses graphical utilities to draw a picture that can later be ...

  7. poj3449 Geometric Shapes【计算几何】

    含[判断线段相交].[判断两点在线段两侧].[判断三点共线].[判断点在线段上]模板   Geometric Shapes Time Limit: 2000MS   Memory Limit: 655 ...

  8. poj 1474 Video Surveillance - 求多边形有没有核

    /* poj 1474 Video Surveillance - 求多边形有没有核 */ #include <stdio.h> #include<math.h> const d ...

  9. poj 1279 Art Gallery - 求多边形核的面积

    /* poj 1279 Art Gallery - 求多边形核的面积 */ #include<stdio.h> #include<math.h> #include <al ...

随机推荐

  1. html5系列.基础知识

    兼容性问题 创建一个html5页面 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  2. CSS3 box-shadow(阴影使用)

    from: http://jingyan.baidu.com/article/03b2f78c4d9fae5ea237aea6.html css3 box-shadow 内阴影与外阴影 1- box- ...

  3. 简单学C——第一天

    基本功 一.数据类型: 在C语言中,有数据类型这一说法.为何有这一说法?是因为在现实生活中存在着不同的数据,(例如整数,小数,字符即a b c d , . ; "  之类).由于计算机中所有 ...

  4. awk里的各种坑

    今天又遇到一个,一旦需要定义一个局部数组(awk通过把局部变量定义在函数参数列表来实现局部这一特征)那么这个数组可以正常的操作,但是无法对他取长度,一旦使用length(tempArr)会得到这么一个 ...

  5. 用nodejs,express,ejs,mongo,extjs实现了简单了网站后台管理系统

    源代码下载地址:http://download.csdn.net/detail/guoyongrong/6498611 这个系统其实是出于学习nodejs的目的而改写的系统. 原来的系统前端使用了ex ...

  6. Spring 配置自动扫描spring bean配置

    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...

  7. BZOJ 1483 梦幻布丁

    Description \(N\)个布丁摆成一行,进行\(M\)次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为\(1,2,2,1\)的四个布丁一共有\ ...

  8. Hotel

    poj3667:http://poj.org/problem?id=3667 题目大意:Hotel有N(1 ≤ N ≤ 50,000)间rooms,并且所有的rooms都是连续排列在同一边,group ...

  9. codeforces C. Inna and Huge Candy Matrix

    http://codeforces.com/problemset/problem/400/C 题意:给你一个n*m的矩阵,然后在矩阵中有p个糖果,给你每个糖果的初始位置,然后经过x次顺时针反转,y次旋 ...

  10. Cylinder

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2374 思路:三分枚举. #include &l ...