poj 3449 Geometric Shapes(判断多边形是否相交)

来源:互联网 发布:linux oracle安装在哪 编辑:程序博客网 时间:2024/05/22 14:14

网址:点击打开链接

Geometric Shapes
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 1614 Accepted: 680

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
题意:此题的输入输出实在是恶心- - 出题人绝壁是抱着虐我们的心态出题的..

输入一个字母,表示多边形的名字

输入一个多边形类型,可以是正方形,直线,矩形,三角形,多边形

最后让你按照名字的字典序输出分别和哪几个多边形相交了。

思路:具体就不说了,注意要用不规范相交,互跨是判断不出一条线段包含另一条线段的情况的,看代码吧,写了好久,真的恶心到了= = 

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;#define N 30#define eps 1e-8struct Point{    double x,y;};struct Node{    char name[2];    int flag;    int num;    Point d[N];} node[N];void get(Point a,Point c,Point *b,Point *d)///已知正方形一条对角线上的两个点,求另外两个点{    ///已知ac点,求bd点    double x,y,mx, my;    mx = (a.x+c.x)/2.0, my = (a.y+c.y)/2.0;    x = a.x - mx;    y = a.y - my;    (*b).x = -y + mx;    (*b).y = x + my;    x = c.x - mx;    y = c.y - my;    (*d).x = - y + mx;    (*d).y = x + my;}bool cmp(Node a,Node b){    return a.name[0]<b.name[0];}double cross(Point a,Point b,Point c) ///叉积{    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);}int dblcmp(double m){    if (fabs(m) < eps) return 0;    return m > 0 ? 1 : -1;}int inter(Point a,Point b,Point c,Point d)///判断线段是否相交,不规范相交。{    if (dblcmp(max(a.x, b.x)-min(c.x, d.x)) >= 0 && dblcmp(max(c.x, d.x)-min(a.x, b.x)) >= 0            && dblcmp(max(a.y, b.y)-min(c.y, d.y)) >= 0 && dblcmp(max(c.y, d.y)-min(a.y, b.y)) >= 0            && dblcmp(cross(a, d, c)*cross(b, d, c)) <= 0 && dblcmp(cross(c, b, a)*cross(d, b, a)) <= 0)        return 1;    return 0;}int check(Node a,Node b)///判断多边形a是否和多边形b相交,注意内含算不相交{    a.d[a.num]=a.d[0];    b.d[b.num]=b.d[0];    for(int i=0; i<a.num; i++)        for(int j=0; j<b.num; j++)            if(inter(a.d[i],a.d[i+1],b.d[j],b.d[j+1]))                return 1;    return 0;}int main(){    Point a,c;    while(1)    {        char op[N];        int t=0;        while(scanf("%s",node[t++].name))        {            if(node[t-1].name[0]=='-'||node[t-1].name[0]=='.')                break;            scanf("%s",op);            if(strcmp(op,"square")==0)///正方形            {                node[t-1].flag=0;                scanf(" (%lf,%lf)",&node[t-1].d[0].x,&node[t-1].d[0].y);                scanf(" (%lf,%lf)",&node[t-1].d[2].x,&node[t-1].d[2].y);                Point b, d;                get(node[t-1].d[0],node[t-1].d[2],&node[t-1].d[1],&node[t-1].d[3]);                node[t-1].num=4;            }            else if(strcmp(op,"line")==0)///直线            {                scanf(" (%lf,%lf)",&node[t-1].d[0].x,&node[t-1].d[0].y);                scanf(" (%lf,%lf)",&node[t-1].d[1].x,&node[t-1].d[1].y);                node[t-1].num=2;            }            else if(strcmp(op,"triangle")==0)///三角形            {                scanf(" (%lf,%lf)",&node[t-1].d[0].x,&node[t-1].d[0].y);                scanf(" (%lf,%lf)",&node[t-1].d[1].x,&node[t-1].d[1].y);                scanf(" (%lf,%lf)",&node[t-1].d[2].x,&node[t-1].d[2].y);                node[t-1].num=3;            }            else if(strcmp(op,"rectangle")==0)///矩形            {                scanf(" (%lf,%lf)",&node[t-1].d[0].x,&node[t-1].d[0].y);                scanf(" (%lf,%lf)",&node[t-1].d[1].x,&node[t-1].d[1].y);                scanf(" (%lf,%lf)",&node[t-1].d[2].x,&node[t-1].d[2].y);                node[t-1].d[3].x = node[t-1].d[2].x + (node[t-1].d[0].x - node[t-1].d[1].x);                node[t-1].d[3].y = node[t-1].d[2].y + (node[t-1].d[0].y - node[t-1].d[1].y);                node[t-1].num=4;            }            else if(strcmp(op,"polygon")==0)///多边形            {                scanf("%d",&node[t-1].num);                for(int i=0; i<node[t-1].num; i++)                    scanf(" (%lf,%lf)",&node[t-1].d[i].x,&node[t-1].d[i].y);            }        }        if(node[t-1].name[0]=='.')            break;        t--;        sort(node,node+t,cmp);        int cnt;        for(int i=0; i<t; i++)        {            cnt=0;            for(int j=0; j<t; j++)            {                if(i==j) continue;                if(check(node[i],node[j]))                    op[cnt++]=node[j].name[0];            }            if(cnt==0)                printf("%c has no intersections\n",node[i].name[0]);            else if(cnt==1)                printf("%c intersects with %c\n",node[i].name[0],op[0]);            else if(cnt==2)                printf("%c intersects with %c and %c\n",node[i].name[0],op[0],op[1]);            else            {                printf("%c intersects with ",node[i].name[0]);                for(int j=0; j<cnt-1; j++)                    printf("%c, ",op[j]);                printf("and %c\n",op[cnt-1]);            }        }            printf("\n");    }    return 0;}


0 0
原创粉丝点击