POJ 3449 Geometric Shapes(判断几个不同图形的相交)

来源:互联网 发布:linux 终端 重启 编辑:程序博客网 时间:2024/06/06 04:01
Geometric Shapes
Time Limit: 2000MS
Memory Limit: 65536KTotal Submissions: 1243
Accepted: 524

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 intersectionsB intersects with S, W, and XF intersects with WS intersects with BW intersects with B and FX intersects with BA has no intersectionsB has no intersections

Source

CTU Open 2007

题意:

       给你n个多边形,这些多边形包括线段,三角形,矩形,正方形,和其他多边形. 然后要你输出他们之间相交的情况. 且多边形自己的边不会相交,且三角形不会退化成线段.

分析:

       本题不难,但是需要注意程序实现的各种细节才行.

       当给你矩形时,你得到的是(x1,y1) (x2,y2) 和(x3,y3)3个点.你需要求出(x4,y4). 分析可得

(x4,y4) =(x3+x1-x2, y3+y1-y2); 

       当给你正方形时,你得到的是对角线的端点(x1,y1)和(x3,y3).你需要计算出(x2,y2)和(x4,y4).

       接下来就两两判断是否相交即可.判断两个图形是否相交,只需要判断他们中的任意两条边是否有交点即可(线段相交判定).

已知正方形x1,y1;x3,y3;(对角线),求出其余两个点:

x2 = (x1+x3-y3+y1)/2; y2 = (x3-x1+y1+y3)/2;

x4= (x1+x3+y3-y1)/2; y4 = (-x3+x1+y1+y3)/2;

已知正方形三个点, 求第四点,三个点按照顺序(顺序or逆序)给出

(x4,y4) =(x3+x1-x2, y3+y1-y2); 

部分代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const double esp = 1e-8;int sgn(double x){    if(fabs(x) < esp) return 0;    if(x < 0) return -1;    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);    }    double operator * (const Point &b) const    {        return  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){}};int 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  //建立图形, n一共几个点, p是点集{    char id;    int n;    Point p[22];}node[30];int cmp(Node a, Node b){    return a.id < b.id;}char str[30];int check(Node a, Node b)  //枚举两个图形的每对线段的组合,判断是否相交, 下面枚举{    for(int i = 0; i < a.n; i++)        for(int j = 0; j < b.n; j++)            if(inter(Line(a.p[i], a.p[(i+1)%a.n]), Line(b.p[j], b.p[(j+1)%b.n])))                return 1;    return 0;} 
sort(node,node+n,cmp);          for(int i = 0;i < n;i++) //枚举每两个图形        {            printf("%c ",node[i].id);            memset(ff,false,sizeof(ff));            int cnt = 0;            for(int j = 0;j < n;j++)                if(i != j)                  if(check(node[i],node[j]))                    {                        cnt++;                        ff[j] = true;                    }            if(cnt == 0)printf("has no intersections\n");


0 0
原创粉丝点击