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


题意:

       给你n个多边形,这些多边形包括线段,三角形,矩形,正方形,和其他多边形. 然后要你输出他们之间相交的情况. 且多边形自己的边不会相交,且三角形不会退化成线段.

分析:

       本题不难,但是需要注意程序实现的各种细节才行.

       当给你矩形时,你得到的是(x1,y1) (x2,y2) 和(x3,y3)3个点.你需要求出(x4,y4). 分析可得

(x4,y4) =(x3+x1-x2, y3+y1-y2); 

       当给你正方形时,你得到的是对角线的端点(x1,y1)和(x3,y3).你需要计算出(x2,y2)和(x4,y4).

       接下来就两两判断是否相交即可.判断两个图形是否相交,只需要判断他们中的任意两条边是否有交点即可(线段相交判定).

已知正方形x1,y1;x3,y3;(对角线),求出其余两个点:

x2 = (x1+x3-y3+y1)/2; y2 = (x3-x1+y1+y3)/2;

x4= (x1+x3+y3-y1)/2; y4 = (-x3+x1+y1+y3)/2;

已知正方形三个点, 求第四点,三个点按照顺序(顺序or逆序)给出

(x4,y4) =(x3+x1-x2, y3+y1-y2); 

部分代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const double esp = 1e-8;
int sgn(double x)
{
    if(fabs(x) < esp) return 0;
    if(x < 0) return -1;
    return 1;
}
struct Point
{
    double x, y;
    Point(){}
    Point(double xx, double yy) : x(xx), y(yy){}
    Point operator - (const Point &b) const
    {
        return Point(x-b.x, y-b.y);
    }
    double operator * (const Point &b) const
    {
        return  x*b.x + y*b.y;
    }
    double operator ^ (const Point &b) const
    {
        return x*b.y-y*b.x;
    }
};
struct Line
{
    Point s, e;
    Line(){}
    Line(Point ss, Point ee) : s(ss), e(ee){}
};
int 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  //建立图形, n一共几个点, p是点集
{
    char id;
    int n;
    Point p[22];
}node[30];
int cmp(Node a, Node b)
{
    return a.id < b.id;
}
char str[30];
int 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 1;
    return 0;
} 
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");


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值