poj 3449 Geometric Shapes(計算幾何)

Geometric Shapes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 1343   Accepted: 565

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



題意:給出一些線段或多邊形,問它們是否相交(碰到也不行)

題解:只需把所有輸入都化成n邊型(線段也是),然後由一個多邊形枚舉其他多邊形,然後再所有邊相枚舉,求是否相交,而明顯線段相交的話2個圖形就相交了。

           不過這題的輸入輸出有點麻煩,有點算是模擬的計算幾何了


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct point{
    double x,y;
}mid,my[5];
struct polygon{
    int n;
    char name[18];
    struct point a[28];
}p[28];
char s[10008],ch;
int all;
double MAX(double x,double y){ return x>y?x:y; }
double MIN(double x,double y){ return x<y?x:y; }
double cross(struct point p1,struct point p2,struct point p3)
{
    return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);
}
int cmp(const void *a,const void *b)
{
    struct polygon c=*(struct polygon *)a;
    struct polygon d=*(struct polygon *)b;
    return strcmp(c.name,d.name);
}
void add_polygon(int n)
{
    int i;
    p[all].n=n;
    for(i=0;i<n;i++)
    {
        scanf("%s",s);
        sscanf(s+1,"%lf%c%lf",&p[all].a[i].x,&ch,&p[all].a[i].y);
    }
}
void add_rectangle()
{
    int i;
    p[all].n=4;
    for(i=0;i<3;i++)
    {
        scanf("%s",s);
        sscanf(s+1,"%lf%c%lf",&p[all].a[i].x,&ch,&p[all].a[i].y);
    }
    p[all].a[3].x=p[all].a[0].x+p[all].a[2].x-p[all].a[1].x;
    p[all].a[3].y=p[all].a[0].y+p[all].a[2].y-p[all].a[1].y;
}
void add_square()
{
    p[all].n=4;
    scanf("%s",s);
    sscanf(s+1,"%lf%c%lf",&p[all].a[0].x,&ch,&p[all].a[0].y);
    scanf("%s",s);
    sscanf(s+1,"%lf%c%lf",&p[all].a[2].x,&ch,&p[all].a[2].y);
    mid.x=(p[all].a[0].x+p[all].a[2].x)/2;
    mid.y=(p[all].a[0].y+p[all].a[2].y)/2;
    p[all].a[1].x=mid.x+(p[all].a[0].y-mid.y);
    p[all].a[1].y=mid.y-(p[all].a[0].x-mid.x);
    p[all].a[3].x=mid.x-(p[all].a[0].y-mid.y);
    p[all].a[3].y=mid.y+(p[all].a[0].x-mid.x);
}
int intersect(struct point p1,struct point p2,struct point p3,struct point p4)
{
    if(MAX(p1.x,p2.x)<MIN(p3.x,p4.x)) return 0;
    if(MIN(p1.x,p2.x)>MAX(p3.x,p4.x)) return 0;
    if(MAX(p3.y,p4.y)<MIN(p1.y,p2.y)) return 0;
    if(MIN(p3.y,p4.y)>MAX(p1.y,p2.y)) return 0;
    if(cross(p1,p2,p3)*cross(p1,p2,p4)>1e-8) return 0;
    if(cross(p3,p4,p1)*cross(p3,p4,p2)>1e-8) return 0;
    return 1;
}
int check(struct polygon p1,struct polygon p2)
{
    int i,j;

    for(i=0;i<p1.n;i++)
    {
        for(j=0;j<p2.n;j++)
            if(intersect(p1.a[i],p1.a[i+1],p2.a[j],p2.a[j+1])) return 1;
    }

    return 0;
}
int main()
{
    int i,j,ans,cou,res[28];

    while(scanf("%s",s),s[0]!='.')
    {
        all=0;
        do{
            strcpy(p[all].name,s);
            scanf("%s",s);
            if(s[0]=='l') add_polygon(2);
            else if(s[0]=='t') add_polygon(3);
            else if(s[0]=='r') add_rectangle();
            else if(s[0]=='s') add_square();
            else
            {
                scanf("%d",&i);
                add_polygon(i);
            }
            p[all].a[p[all].n]=p[all].a[0];
            all++;
        }while(scanf("%s",s),s[0]!='-');
        qsort(p,all,sizeof(p[0]),cmp);
        for(i=0;i<all;i++)
        {
            for(ans=j=res[i]=0;j<all;j++)
            {
                if(i==j) continue;
                if(check(p[i],p[j])){ res[j]=1; ans++;}
                else res[j]=0;
            }
            printf("%s ",p[i].name);
            if(!ans) printf("has no intersections\n");
            else if(ans==1)
            {
                printf("intersects with ");
                for(j=0;j<all;j++) if(res[j]) printf("%s\n",p[j].name);
            }
            else if(ans==2)
            {
                printf("intersects with ");
                for(j=0;j<all;j++)
                {
                    if(!res[j]) continue;
                    printf("%s and ",p[j].name);
                    break;
                }
                for(j++;j<all;j++) if(res[j]) printf("%s\n",p[j].name);
            }
            else
            {
                printf("intersects with");
                for(cou=j=0;j<all;j++)
                {
                    if(!res[j]) continue;
                    printf(" %s",p[j].name);
                    if(++cou<ans-1) printf(",");
                    else if(cou==ans-1) printf(", and");
                    else printf("\n");
                }
            }
        }
        printf("\n");
    }

    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章