POJ 3449 Geometric Shapes (求正方形的另外两点)

Geometric Shapes

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 1470   Accepted: 622

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 + x3 = x0 + x2;

x1 - x3 = y2 - y0;

y1 + y3 = y0 + y2;

y3 - y1 = x2 - x0;

求得另一对不相邻的顶点(x1,y1),(x3,y3)。

x1 = (x0 + x2 + y2 - y0) / 2

x3 = (x0 + x2 + y0 - y2) / 2

y1 = (y0 + y2 + x0 - x2) / 2

y3 = (y0 + y2 - x0 + x2) / 2

这样的话其他就没有难点了,就是遍历每个图形,看是否与其他的相交,如果不知道向量如何旋转的,请看http://www.cnblogs.com/Howe-Young/p/4466975.html

/*************************************************************************
    > File Name:            poj_3449.cpp
    > Author:               Howe_Young
    > Mail:                 [email protected]
    > Created Time:         2015年05月01日 星期五 18时38分39秒
 ************************************************************************/

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>

using namespace std;
const int maxn = 30;
struct point{
    double x, y;
};
struct shape{
    char id;
    char name[10];
    point edge[22];
    char info[maxn];
    int index, num;
};
shape s[maxn];
int n, i;
char ch;
void input()
{
        s[i].id = ch;
        s[i].index = 0;
        scanf("%s", s[i].name);
        if (strcmp(s[i].name, "square") == 0)
        {
            scanf(" (%lf, %lf) (%lf, %lf)", &s[i].edge[0].x, &s[i].edge[0].y, &s[i].edge[2].x, &s[i].edge[2].y);
            s[i].edge[1].x = (s[i].edge[0].x + s[i].edge[2].x + s[i].edge[2].y - s[i].edge[0].y) / 2;
            s[i].edge[1].y = (s[i].edge[0].y + s[i].edge[2].y - s[i].edge[2].x + s[i].edge[0].x) / 2;
            s[i].edge[3].x = (s[i].edge[0].x + s[i].edge[2].x - s[i].edge[2].y + s[i].edge[0].y ) / 2;
            s[i].edge[3].y = (s[i].edge[0].y + s[i].edge[2].y + s[i].edge[2].x - s[i].edge[0].x) / 2;
            s[i].num = 4;
            return;
        }
        else if (strcmp(s[i].name, "rectangle") == 0)
        {
            scanf(" (%lf,%lf) (%lf, %lf) (%lf, %lf)", &s[i].edge[0].x, &s[i].edge[0].y, &s[i].edge[1].x, &s[i].edge[1].y, &s[i].edge[2].x, &s[i].edge[2].y);
            s[i].edge[3].x = s[i].edge[0].x + s[i].edge[2].x - s[i].edge[1].x;
            s[i].edge[3].y = s[i].edge[0].y + s[i].edge[2].y - s[i].edge[1].y;
            s[i].num = 4;
            return;
        }
        else if (strcmp(s[i].name, "line") == 0)
        {
            scanf(" (%lf, %lf) (%lf, %lf)", &s[i].edge[0].x, &s[i].edge[0].y, &s[i].edge[1].x, &s[i].edge[1].y);
            s[i].num = 2;
            return;
        }
        else if(strcmp(s[i].name, "triangle") == 0)
        {
            scanf(" (%lf, %lf) (%lf, %lf) (%lf, %lf)", &s[i].edge[0].x, &s[i].edge[0].y, &s[i].edge[1].x, &s[i].edge[1].y, &s[i].edge[2].x, &s[i].edge[2].y);
            s[i].num = 3;
            return;
        }
        else
        {
            int k;
            scanf("%d", &k);
            for (int j = 0; j < k; j++)
                scanf(" (%lf, %lf)", &s[i].edge[j].x, &s[i].edge[j].y);
            s[i].num = k;
            return;
        }

}
double x_multi(point p1, point p2, point p3)
{
    return (p3.x - p1.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p3.y - p1.y);
}
bool on_segment(point p1, point p2, point p3)
{
    double minx, miny, maxx, maxy;
    if (p1.x > p2.x)
    {
        minx = p2.x;
        maxx = p1.x;
    }
    else
    {
        minx = p1.x;
        maxx = p2.x;
    }
    if (p1.y > p2.y)
    {
        miny = p2.y;
        maxy = p1.y;
    }
    else
    {
        maxy = p2.y;
        miny = p1.y;
    }
    return (p3.x >= minx && p3.x <= maxx && p3.y >= miny && p3.y <= maxy);
}
bool segment_intersect(point p1, point p2, point p3, point p4)
{
    double d1 = x_multi(p1, p2, p3);
    double d2 = x_multi(p1, p2, p4);
    double d3 = x_multi(p3, p4, p1);
    double d4 = x_multi(p3, p4, p2);
    if (d1 * d2 < 0 && d3 * d4 < 0)
        return true;
    if (d1 == 0 && on_segment(p1, p2, p3))
        return true;
    if (d2 == 0 && on_segment(p1, p2, p4))
        return true;
    if (d3 == 0 && on_segment(p3, p4, p1))
        return true;
    if (d4 == 0 && on_segment(p3, p4, p2))
        return true;
    return false;
}
bool intersected(shape a, shape b)
{
    for (int p = 1; p <= a.num; p++)
    {
        for (int q = 1; q <= b.num; q++)
        {
            if (segment_intersect(a.edge[p - 1], a.edge[p % a.num], b.edge[q - 1], b.edge[q % b.num]))
            {
                return true;
            }
        }
    }
    return false;
}
bool cmp1(const shape a, const shape b)
{
    return a.id < b.id;
}
bool cmp2(const char a, const char b)
{
    return a < b;
}
void output(shape a)
{
    if (a.index == 0)
    {
        printf("%c has no intersections\n", a.id);
        return;
    }
    if (a.index == 1)
    {
        printf("%c intersects with %c\n", a.id, a.info[0]);
        return;
    }
    if (a.index == 2)
    {
        printf("%c intersects with %c and %c\n", a.id, a.info[0], a.info[1]);
        return;
    }
    printf("%c intersects with %c, ", a.id, a.info[0]);
    for (int t = 1; t < a.index - 1; t++)
        printf("%c, ", a.info[t]);
    printf("and %c\n", a.info[a.index - 1]);

}
int main()
{
    while (cin >> ch && ch != ‘.‘)
    {
        memset(s, 0, sizeof(s));
        i = 0;
        if (ch == ‘-‘)
            continue;
        input();
        while (cin >> ch)
        {
            i++;
            if (ch == ‘-‘)
                break;
            input();
        }
        n = i;
        for (int t = 0; t < n; t++)
        {
            for (int j = t + 1; j < n; j++)
            {
                if (intersected(s[t], s[j]))
                {
                    s[t].info[s[t].index++] = s[j].id;
                    s[j].info[s[j].index++] = s[t].id;
                }
            }
        }
        sort(s, s + n, cmp1);
        for (int t = 0; t < n; t++)
        {
            sort(s[t].info, s[t].info + s[t].index, cmp2);
            output(s[t]);
        }
        puts("");
    }
    return 0;
}

时间: 2024-10-11 05:47:11

POJ 3449 Geometric Shapes (求正方形的另外两点)的相关文章

POJ 3449 Geometric Shapes 判断多边形相交

题意不难理解,给出多个多边形,输出多边形间的相交情况(嵌套不算相交),思路也很容易想到.枚举每一个图形再枚举每一条边 恶心在输入输出,不过还好有sscanf(),不懂可以查看cplusplus网站 根据正方形对角的两顶点求另外两个顶点公式: x2 = (x1+x3-y3+y1)/2; y2 = (x3-x1+y1+y3)/2; x4= (x1+x3+y3-y1)/2; y4 = (-x3+x1+y1+y3)/2; 还有很多细节要处理 #include <iostream> #include &

POJ 3449 Geometric Shapes --计算几何,线段相交

题意: 给一些多边形或线段,输出与每一个多边形或线段的有哪一些多边形或线段. 解法: 想法不难,直接暴力将所有的图形处理成线段,然后暴力枚举,相交就加入其vector就行了.主要是代码有点麻烦,一步一步来吧. 还有收集了一个线段旋转的函数. Vector Rotate(Point P,Vector A,double rad){ //以P为基准点把向量A旋转rad return Vector(P.x+A.x*cos(rad)-A.y*sin(rad),P.y+A.x*sin(rad)+A.y*co

简单几何(线段相交)+模拟 POJ 3449 Geometric Shapes

题目传送门 题意:给了若干个图形,问每个图形与哪些图形相交 分析:题目说白了就是处理出每个图形的线段,然后判断是否相交.但是读入输出巨恶心,就是个模拟题加上线段相交的判断,我第一次WA不知道输出要按字母序输出,第二次WA是因为忘记多边形的最后一条线段,还好找到了,没有坚持的话就不会AC了. /************************************************ * Author :Running_Time * Created Time :2015/10/31 星期六

POJ 3449 Geometric Shapes

判断两个多边形是否相交,只需判断边是否有相交. 编码量有点大,不过思路挺简单的. #include<cstdio> #include<cstring> #include<vector> #include<cmath> #include<string> #include<queue> #include<list> #include<algorithm> #include<iostream> using

TOJ 2560 Geometric Shapes(判断多边形是否相交)

描述 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 inte

POJ 2299 Ultra-QuickSort(归并排序求逆序对数)

题目地址:POJ 2299 今天下午的多校看来没有白做...实在做不出题闲着无聊看小白鼠学会了个归并排序.哈哈. 归并排序简单地说其实就是先分成一个二叉树直至单个,然后依次从最底层不断进行合并,逆序对数就是在合并的过程中,加入后面的那段中到了比他大的时候,那后面的那些就都是比他大的,都是逆序对数,所以直接加上即可.网上资料很多,就不细说了..用了分治的思想. 自己根据理解写的代码,考虑的太不全面了..又调了好长时间... 代码如下: #include <algorithm> #include

POJ 2540 半平面交求可行区域面积

Hotter Colder Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2343   Accepted: 981 Description The children's game Hotter Colder is played as follows. Player A leaves the room while player B hides an object somewhere in the room. Player

poj 2104主席树求区间第k小

POJ - 2104 题意:求区间第k小 思路:无修改主席树 AC代码: #include "iostream" #include "iomanip" #include "string.h" #include "stack" #include "queue" #include "string" #include "vector" #include "set&

POJ 1177 Picture(扫描线求周长)

与求面积并的差不多,但是这个与扫描的方向相同的情况不太好处理,如果扫描线离散化两次扫两遍其实也可以解决这个问题,但是这样无论在时间还是空间上稍微就有点浪费了啊.这里因为我是离散x坐标的所以对于平行于y轴的方向上的统计比较难统计.处理的方法是:标记区间左边的断点,和右边的断点,求出这个区间一共有多少个断点.就可以统计出平行于y轴的长度了.这里合并的时候需要判断右边的左区间和左边的右区间是否相同,如果相同的话,说明他们连接到了一起,要减去多加的. Picture Time Limit: 2000MS