poj 3449 判断许多个几何图形是否相交

来源:互联网 发布:php 在线人数统计 编辑:程序博客网 时间:2024/05/24 04:06

Geometric Shapes
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 1814 Accepted: 770

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<math.h>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const double eps=1e-8;#define MAXN 100struct node{    int num,tt;    int a[MAXN];};node ans[MAXN];struct point{    double x,y;    point(){}    point(double xx,double yy){        x=xx,y=yy;    }};point p[MAXN][MAXN];int maps[MAXN][MAXN],num[MAXN];int cmp1(const void *x,const void *y){    node *A=(node *)x;    node *B=(node *)y;    return A->num - B->num;}int dcmp(double x){    if (fabs (x) < eps) return 0;    else    return x < 0 ? -1 : 1;}double Cross(point p1,point p2,point p3){    return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);}double Cross2(point A, point B){    return A.x * B.y - A.y * B.x;}double Dis(point A, point B){    return sqrt((A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y));}point operator - (point A,point B){    return point(A.x-B.x, A.y-B.y);}point operator + (point A, point B){    return point(A.x+B.x, A.y+B.y);}point operator * (point A, double p){    return point(A.x*p, A.y*p);}bool operator == (point A, point B){    return (A.x-B.x) == 0 && (A.y-B.y) == 0;}bool SegmentProperIntersection(point a1, point a2, point b1, point b2)  {    double c1 = Cross2 (a2 - a1, b1 - a1), c2 = Cross2 (a2 - a1, b2 - a1),           c3 = Cross2 (b2 - b1, a1 - b1), c4 = Cross2 (b2 - b1, a2 - b1);    return dcmp (c1) * dcmp (c2) <= 0 && dcmp (c3 * c4) <= 0;}int main(){    int cnt=1,n;    char ch[5],str[30];    //freopen("in.txt","r",stdin);    memset(maps,0,sizeof(maps));    while(scanf("%s",ch),strcmp(ch,".")!=0)    {        if(strcmp(ch,"-")){            scanf("%s",str);            if(strcmp(str,"square")==0){                scanf(" (%lf,%lf)",&p[cnt][1].x,&p[cnt][1].y);                scanf(" (%lf,%lf)",&p[cnt][3].x,&p[cnt][3].y);                p[cnt][2].x=(p[cnt][1].x+p[cnt][3].x+p[cnt][1].y-p[cnt][3].y)/2;                p[cnt][4].x=p[cnt][1].x+p[cnt][3].x-p[cnt][2].x;                p[cnt][2].y=(p[cnt][1].y+p[cnt][3].y+p[cnt][3].x-p[cnt][1].x)/2;                p[cnt][4].y=p[cnt][1].y+p[cnt][3].y-p[cnt][2].y;                p[cnt][5]=p[cnt][1];                num[cnt]=5;                maps[cnt][0]=ch[0]-'A';            }            else if(strcmp(str,"line")==0){                scanf(" (%lf,%lf)",&p[cnt][1].x,&p[cnt][1].y);                scanf(" (%lf,%lf)",&p[cnt][2].x,&p[cnt][2].y);                num[cnt]=2;                maps[cnt][0]=ch[0]-'A';            }            else if(strcmp(str,"triangle")==0){                scanf(" (%lf,%lf)",&p[cnt][1].x,&p[cnt][1].y);                scanf(" (%lf,%lf)",&p[cnt][2].x,&p[cnt][2].y);                scanf(" (%lf,%lf)",&p[cnt][3].x,&p[cnt][3].y);                p[cnt][4].x=p[cnt][1].x;                p[cnt][4].y=p[cnt][1].y;                num[cnt]=4;                maps[cnt][0]=ch[0]-'A';            }            else if(strcmp(str,"polygon")==0){                scanf("%d",&n);                for(int i=1;i<=n;i++)                    scanf(" (%lf,%lf)",&p[cnt][i].x,&p[cnt][i].y);                p[cnt][n+1].x=p[cnt][1].x;                p[cnt][n+1].y=p[cnt][1].y;                num[cnt]=n+1;                maps[cnt][0]=ch[0]-'A';            }            else if(strcmp(str,"rectangle")==0){                scanf(" (%lf,%lf)",&p[cnt][1].x,&p[cnt][1].y);                scanf(" (%lf,%lf)",&p[cnt][2].x,&p[cnt][2].y);                scanf(" (%lf,%lf)",&p[cnt][3].x,&p[cnt][3].y);                p[cnt][4].x=p[cnt][1].x+p[cnt][3].x-p[cnt][2].x;                p[cnt][4].y=p[cnt][1].y+p[cnt][3].y-p[cnt][2].y;                p[cnt][5]=p[cnt][1];                num[cnt]=5;                maps[cnt][0]=ch[0]-'A';            }            cnt++;            continue;        }        for(int i=1;i<cnt;i++){            for(int j=i+1;j<cnt;j++){                for(int k=1;k<=num[i]-1;k++){                    for(int q=1;q<=num[j]-1;q++){                        if(SegmentProperIntersection(p[i][k],p[i][k+1],p[j][q],p[j][q+1])){                            maps[i][j]=1;                            maps[j][i]=1;                            goto kkk;                        }                    }                }                kkk:                    ;            }        }        for(int i=1;i<cnt;i++){            ans[i].tt=0;            ans[i].num=maps[i][0];            for(int j=1;j<cnt;j++){                if(maps[i][j])                    ans[i].a[ans[i].tt++]=maps[j][0];            }        }        qsort(ans+1,cnt-1,sizeof(ans[0]),cmp1);        for(int i=1;i<cnt;i++){            if(ans[i].tt==0)                printf("%c has no intersections",ans[i].num+'A');            else if(ans[i].tt==1){                printf("%c intersects with ",ans[i].num+'A');                printf("%c",ans[i].a[0]+'A');            }            else if(ans[i].tt==2){                sort(ans[i].a,ans[i].a+ans[i].tt);                printf("%c intersects with ",ans[i].num+'A');                printf("%c and %c",ans[i].a[0]+'A',ans[i].a[1]+'A');            }            else{                sort(ans[i].a,ans[i].a+ans[i].tt);                printf("%c intersects with ",ans[i].num+'A');                for(int j=0;j<ans[i].tt-1;j++)                    printf("%c, ",ans[i].a[j]+'A');                printf("and %c",ans[i].a[ans[i].tt-1]+'A');            }            puts("");        }        cnt=1;        puts("");        memset(maps,0,sizeof(maps));    }    return 0;}


阅读全文
0 0
原创粉丝点击