POJ 3449 Geometric Shapes(判断几个不同图形的相交,线段相交判断)

Geometric Shapes

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 1243 Accepted: 524

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         题目首先要 根据正方形给出的两个对角线的顶点,求另外两个顶点。 正方形,已知 (x0,y0) 和(x2,y2)  可以根据下列关系求(x1,y1),(x3,y3)   x1+x3 = x0+x2; x1-x3  =  y2-y0; y1+y3 =  y0-y2; y1-y3 =  x0-x2;   矩形类似。   然后判断线段相交即可。     注意输入输出格式控制。    

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>

using namespace std;
const double eps = 1e-8;
int sgn(double x)
{
    if(fabs(x) < eps)return 0;
    if(x < 0)return -1;
    else return 1;
}
struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y)
    {
        x = _x;y = _y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x - b.x,y - b.y);
    }
    //叉积
    double operator ^(const Point &b)const
    {
        return x*b.y - y*b.x;
    }
    //点积
    double operator *(const Point &b)const
    {
        return x*b.x + y*b.y;
    }
};
struct Line
{
    Point s,e;
    Line(){}
    Line(Point _s,Point _e)
    {
        s = _s;e = _e;
    }
};
//*判断线段相交
bool inter(Line l1,Line l2)
{
    return
    max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
    max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
    max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
    max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
    sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= 0 &&
    sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) <= 0;
}

struct Node
{
    char id;
    int n;//点数
    Point p[22];
}node[30];
bool cmp(Node a,Node b)
{
    return a.id < b.id;
}
char str[30];
bool check(Node a,Node b)
{
    for(int i = 0;i < a.n;i++)
        for(int j = 0;j < b.n;j++)
           if(inter(Line(a.p[i],a.p[(i+1)%a.n]),Line(b.p[j],b.p[(j+1)%b.n])))
              return true;
    return false;
}
bool ff[30];
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n;
    while(scanf("%s",str) == 1)
    {
        if(str[0] == '.')break;
        node[0].id = str[0];
        scanf("%s",str);
        if(strcmp(str,"square")==0)
        {
            node[0].n = 4;
            scanf(" (%lf,%lf)",&node[0].p[0].x,&node[0].p[0].y);
            //cout<<node[0].p[0].x<<" "<<node[0].p[0].y<<endl;
            scanf(" (%lf,%lf)",&node[0].p[2].x,&node[0].p[2].y);
           // cout<<node[0].p[2].x<<" "<<node[0].p[2].y<<endl;
            node[0].p[1].x = ((node[0].p[0].x+node[0].p[2].x)+(node[0].p[2].y-node[0].p[0].y))/2;
            node[0].p[1].y = ((node[0].p[0].y+node[0].p[2].y)+(node[0].p[0].x-node[0].p[2].x))/2;
            node[0].p[3].x = ((node[0].p[0].x+node[0].p[2].x)-(node[0].p[2].y-node[0].p[0].y))/2;
            node[0].p[3].y = ((node[0].p[0].y+node[0].p[2].y)-(node[0].p[0].x-node[0].p[2].x))/2;
        }
        else if(strcmp(str,"line")==0)
        {
            node[0].n = 2;
            scanf(" (%lf,%lf)",&node[0].p[0].x,&node[0].p[0].y);
            scanf(" (%lf,%lf)",&node[0].p[1].x,&node[0].p[1].y);
        }
        else if(strcmp(str,"triangle")==0)
        {
            node[0].n = 3;
            scanf(" (%lf,%lf)",&node[0].p[0].x,&node[0].p[0].y);
            scanf(" (%lf,%lf)",&node[0].p[1].x,&node[0].p[1].y);
            scanf(" (%lf,%lf)",&node[0].p[2].x,&node[0].p[2].y);
        }
        else if(strcmp(str,"rectangle")==0)
        {
            node[0].n = 4;
            scanf(" (%lf,%lf)",&node[0].p[0].x,&node[0].p[0].y);
            scanf(" (%lf,%lf)",&node[0].p[1].x,&node[0].p[1].y);
            scanf(" (%lf,%lf)",&node[0].p[2].x,&node[0].p[2].y);
            node[0].p[3].x = node[0].p[2].x + (node[0].p[0].x - node[0].p[1].x);
            node[0].p[3].y = node[0].p[2].y + (node[0].p[0].y - node[0].p[1].y);
        }
        else if(strcmp(str,"polygon")==0)
        {
            scanf("%d",&node[0].n);
            for(int i = 0;i < node[0].n;i++)
            {
                scanf(" (%lf,%lf)",&node[0].p[i].x,&node[0].p[i].y);
            }
        }
        n = 1;
        while(scanf("%s",str)==1)
        {

            //cout<<str<<endl;
            if(str[0] == '-')break;
            node[n].id = str[0];
            scanf("%s",str);
            if(strcmp(str,"square")==0)
            {
                node[n].n = 4;
                scanf(" (%lf,%lf)",&node[n].p[0].x,&node[n].p[0].y);
                scanf(" (%lf,%lf)",&node[n].p[2].x,&node[n].p[2].y);
                node[n].p[1].x = ((node[n].p[0].x+node[n].p[2].x)+(node[n].p[2].y-node[n].p[0].y))/2;
                node[n].p[1].y = ((node[n].p[0].y+node[n].p[2].y)+(node[n].p[0].x-node[n].p[2].x))/2;
                node[n].p[3].x = ((node[n].p[0].x+node[n].p[2].x)-(node[n].p[2].y-node[n].p[0].y))/2;
                node[n].p[3].y = ((node[n].p[0].y+node[n].p[2].y)-(node[n].p[0].x-node[n].p[2].x))/2;
            }
            else if(strcmp(str,"line")==0)
            {
                node[n].n = 2;
                scanf(" (%lf,%lf)",&node[n].p[0].x,&node[n].p[0].y);
                scanf(" (%lf,%lf)",&node[n].p[1].x,&node[n].p[1].y);
            }
            else if(strcmp(str,"triangle")==0)
            {
                node[n].n = 3;
                scanf(" (%lf,%lf)",&node[n].p[0].x,&node[n].p[0].y);
                scanf(" (%lf,%lf)",&node[n].p[1].x,&node[n].p[1].y);
                scanf(" (%lf,%lf)",&node[n].p[2].x,&node[n].p[2].y);
            }
            else if(strcmp(str,"rectangle")==0)
            {
                node[n].n = 4;
                scanf(" (%lf,%lf)",&node[n].p[0].x,&node[n].p[0].y);
                scanf(" (%lf,%lf)",&node[n].p[1].x,&node[n].p[1].y);
                scanf(" (%lf,%lf)",&node[n].p[2].x,&node[n].p[2].y);
                node[n].p[3].x = node[n].p[2].x + (node[n].p[0].x - node[n].p[1].x);
                node[n].p[3].y = node[n].p[2].y + (node[n].p[0].y - node[n].p[1].y);
            }
            else if(strcmp(str,"polygon")==0)
            {
                scanf("%d",&node[n].n);
                for(int i = 0;i < node[n].n;i++)
                {
                    scanf(" (%lf,%lf)",&node[n].p[i].x,&node[n].p[i].y);
                }
            }
            n++;
        }
        sort(node,node+n,cmp);
        for(int i = 0;i < n;i++)
        {
            printf("%c ",node[i].id);
            memset(ff,false,sizeof(ff));
            int cnt = 0;
            for(int j = 0;j < n;j++)
                if(i != j)
                  if(check(node[i],node[j]))
                    {
                        cnt++;
                        ff[j] = true;
                    }
            if(cnt == 0)printf("has no intersections\n");
            else if(cnt == 1)
            {
                printf("intersects with ");
                for(int j = 0 ; j < n;j++)
                    if(ff[j])
                {
                    printf("%c\n",node[j].id);
                    break;
                }
            }
            else if(cnt == 2)
            {
                printf("intersects with ");
                for(int j = 0 ; j < n;j++)
                    if(ff[j])
                {
                    if(cnt==2)printf("%c ",node[j].id);
                    if(cnt==1)printf("and %c\n",node[j].id);
                    cnt--;
                }
            }
            else
            {
                printf("intersects with ");
                for(int j = 0 ; j < n;j++)
                    if(ff[j])
                {
                    if(cnt > 1)printf("%c, ",node[j].id);
                    if(cnt==1)printf("and %c\n",node[j].id);
                    cnt--;
                }
            }
        }

        printf("\n");
    }
}

 

         

    原文作者:kuangbin
    原文地址: https://www.cnblogs.com/kuangbin/p/3194393.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞