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

该问题要求处理一组几何形状,判断它们是否相交。输入包含多幅图片,每幅图片描述了最多26个形状,包括正方形、矩形、线段、三角形和多边形等。形状不能相交,若相交则需要输出相交的形状标识。输出按形状标识字母顺序,列出每个形状的相交情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Geometric Shapes
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 1056 Accepted: 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

题意:给定一些多边形或线段,判断他们的相交情况
分析:这题数据范围很小,所以把每个多边形都看成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;
}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值