您的位置:首页 > 其它

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

2012-08-09 10:32 162 查看
Geometric Shapes

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 1056Accepted: 442
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

CTU Open 2007

题目:http://poj.org/problem?id=3449

题意:给定一些多边形或线段,判断他们的相交情况

分析:这题数据范围很小,所以把每个多边形都看成n条线段组成,两个多边形是否相交,只要枚举他们的线段是否相交即可,这样处理起来比较方便。。。还有这次又1Y了,看来函数式的编程还是很有效的

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
typedef double mType;
struct Tpoint
{
    mType x,y;
    Tpoint(){}
    Tpoint(mType _x,mType _y):x(_x),y(_y){}
};
struct Tsegment
{
    Tpoint s,t;
    Tsegment(){}
    Tsegment(Tpoint _s,Tpoint _t):s(_s),t(_t){}
    Tsegment(mType sx,mType sy,mType tx,mType ty):s(sx,sy),t(tx,ty){}
};
Tpoint MakeVector(Tpoint P,Tpoint Q)
{
    return Tpoint(Q.x-P.x,Q.y-P.y);
}
mType CrossProduct(Tpoint P,Tpoint Q)
{
    return P.x*Q.y-P.y*Q.x;
}
mType MultiCross(Tpoint P,Tpoint Q,Tpoint R)
{
    return CrossProduct(MakeVector(Q,P),MakeVector(Q,R));
}
bool IsIntersect(Tsegment P,Tsegment Q)
{
    if(max(P.s.x,P.t.x)<min(Q.s.x,Q.t.x)||max(P.s.y,P.t.y)<min(Q.s.y,Q.t.y)||
       max(Q.s.x,Q.t.x)<min(P.s.x,P.t.x)||max(Q.s.y,Q.t.y)<min(P.s.y,P.t.y))return 0;
    return (MultiCross(P.t,P.s,Q.s)*MultiCross(P.t,P.s,Q.t)<=0&&
            MultiCross(Q.t,Q.s,P.s)*MultiCross(Q.t,Q.s,P.t)<=0);
}
void makeSquare(Tpoint A,Tpoint C,Tpoint &B,Tpoint &D)
{
    B.x=(A.y-C.y+A.x+C.x)/2.0;
    B.y=(C.x-A.x+A.y+C.y)/2.0;
    D.x=A.x+C.x-B.x;
    D.y=A.y+C.y-B.y;
}
void makeRect(Tpoint A,Tpoint B,Tpoint C,Tpoint &D)
{
    D.x=A.x+C.x-B.x;
    D.y=A.y+C.y-B.y;
}
Tsegment g[33][33];
int r[33],out[33][33],m[33];
int n;
void getit(Tpoint &P)
{
    while(getchar()!='(');
    scanf("%lf",&P.x);
    while(getchar()!=',');
    scanf("%lf",&P.y);
    while(getchar()!=')');
}
void init(char s[55])
{
    int i=s[0]-'A',j,m;
    Tpoint q[33];
    scanf("%s",s);
    if(s[0]=='s')
    {
        getit(q[0]);
        getit(q[2]);
        makeSquare(q[0],q[2],q[1],q[3]);
        q[4]=q[0];
        r[i]=4;
    }
    if(s[0]=='r')
    {
        getit(q[0]);
        getit(q[1]);
        getit(q[2]);
        makeRect(q[0],q[1],q[2],q[3]);
        q[4]=q[0];
        r[i]=4;
    }
    if(s[0]=='l')
    {
        getit(q[0]);
        getit(q[1]);
        r[i]=1;
    }
    if(s[0]=='t')
    {
        getit(q[0]);
        getit(q[1]);
        getit(q[2]);
        q[3]=q[0];;
        r[i]=3;
    }
    if(s[0]=='p')
    {
        scanf("%d",&m);
        for(j=0;j<m;++j)getit(q[j]);
        q[m]=q[0];
        r[i]=m;
    }
    for(j=0;j<r[i];++j)
        g[i][j]=Tsegment(q[j],q[j+1]);
}
bool check(int a,int b)
{
    for(int i=0;i<r[a];++i)
        for(int j=0;j<r[b];++j)
            if(IsIntersect(g[a][i],g[b][j]))return 1;
    return 0;
}
int main()
{
    char s[55];
    int i,j;
    while(scanf("%s",s),s[0]!='.')
    {
        memset(r,0,sizeof(r));
        memset(m,0,sizeof(m));
        init(s);
        while(scanf("%s",s),s[0]!='-')init(s);
        for(i=0;i<26;++i)
            for(j=i+1;j<26;++j)
                if(check(i,j))
                {
                    out[i][m[i]++]=j+'A';
                    out[j][m[j]++]=i+'A';
                }
        for(i=0;i<26;++i)
            if(r[i])
            {
                printf("%c ",i+'A');
                if(m[i])
                {
                    printf("intersects with %c",out[i][0]);
                    if(m[i]>2)
                    {
                        for(j=1;j<m[i]-1;++j)
                            printf(", %c",out[i][j]);
                        printf(",");
                    }
                    if(m[i]>1)printf(" and %c",out[i][m[i]-1]);
                    puts("");
                }
                else puts("has no intersections");
            }
        puts("");
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: