POJ 3449 Geometric Shapes(判斷幾個不同圖形的相交,線段相交判斷)

C - Geometric Shapes
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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

題意:給你一些多邊形的點,判斷每個多邊形和那些多邊形相交,編號按照字典序輸出

關於對一個樣例的解釋:


題解:枚舉每個多邊形的每條邊看是否相交,這裏的相交是包括端點的,

   關鍵是給你正方形不相鄰兩個點求另外兩個點怎麼求,長方形給你3個點求第四個點怎麼求?

   

   


   因爲對角線的交點爲兩條對角線的中點,所以 

   x0 + x2 =  x1 + x3

   y0 + y2 =  y1 + y3

   可以證明分割的這幾個小三角形是全等的所以有

   x1 - x3 = y2 - y1

   y1 - y3 = 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

   長方形也是給定三個點求第四個點,很好求,推一下就出來了。

   解決了上面的問題之後,把點放到集合中暴力判斷線段就行了。

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct Point
{
    double x,y;
    Point(double x = 0,double y = 0):x(x),y(y){}
};
typedef Point Vector;
typedef Point point;
Vector operator + (Vector a, Vector b) { return Vector(a.x+b.x,a.y+b.y) ;}
Vector operator - (Vector a, Vector b) { return Vector(a.x-b.x,a.y-b.y) ;}
Vector operator * (Vector a,double p) { return Vector(a.x*p,a.y*p) ;}
Vector operator / (Vector a,double p) { return Vector(a.x/p,a.y/p) ;}
double Dot(Vector a,Vector b) { return a.x*b.x + a.y*b.y ;}
double Length(Vector a) { return sqrt(Dot(a,a)) ;}
double Cross(Vector a, Vector b) { return a.x*b.y - a.y*b.x ;}
const double eps = 1e-8;
int dcmp(double x)
{
    if(fabs(x) < eps) return 0;
    else return x < 0 ? -1 : 1;
}
bool operator == (Point a,Point b)
{
    return dcmp(a.x-b.x) == 0&& dcmp(a.y-b.y) == 0;
}
bool operator < (Point a,Point b)
{
    return a.x < b.x || (a.x == b.x && a.y < b.y);
}
//下面註釋掉的這個是判斷直線相交的 本題中全都是線段
/*double Across(point a,point b,point c,point d) //非規範相交也包含在內
{
    double tmp=Cross(c-a,b-a)*Cross(d-a,b-a);
    if(tmp<0||fabs(tmp)<eps) return true;
    return false;
}*/
bool Onsegment(Point p,Point a,Point b)  //p在ab上
{
    return dcmp(Cross(b-a,p-a)) == 0 && dcmp(Dot(b-p,a-p)) < 0 || (p == a) || (p == b);
}
//相交就算 非規範相交也算
bool Segmentsection(Point a,Point b,Point c,Point d)
{
    double d1 = Cross(b-a,c-a),d2 = Cross(b-a,d-a),d3 = Cross(d-c,a-c),d4 = Cross(d-c,b-c);
    if(dcmp(d1)*dcmp(d2) < 0 && dcmp(d3)*dcmp(d4) < 0) return true;
    else if(dcmp(d1) == 0 && Onsegment(c,a,b) ) return true;
    else if(dcmp(d2) == 0 && Onsegment(d,a,b) ) return true;
    else if(dcmp(d3) == 0 && Onsegment(a,c,d) ) return true;
    else if(dcmp(d4) == 0 && Onsegment(b,c,d) ) return true;
    else return false;
}
struct Line
{
    Point s,e;
    Line(Point s = 0,Point e = 0) :s(s),e(e){}
};

struct polygon
{
    Point p[30];
    int num;
}poly[50];

bool Ispoly(polygon a,polygon b)
{
    if(a.num != 0 && b.num != 0)
    {
         for(int i = 0; i < a.num; i++)
        {
            for(int j = 0; j < b.num; j++)
            {
                if( Segmentsection(a.p[i],a.p[(i+1)%a.num],b.p[j],b.p[(j+1)%b.num]) ) //有一個交點就可以返回了
                    return true;
            }
        }
    }
    return false;
}
int main()
{
    char str[10],strr[20];
    memset(poly,0,sizeof(poly));
    while(scanf("%s",str) != EOF) //str表示前面的大寫字母
    {
        if(strcmp(str,".") == 0)
        break;
        if(strcmp(str,"-") == 0)
        {
            char c[30];
            int k,j;
            for(int i = 0; i < 26; i++)
            {
                if(poly[i].num==0) //輸入中沒有該圖形
                continue;
                k = 0;
                for(j = 0; j < 26; j++)
                {
                    if(poly[i].num==0||i==j) //輸入中沒有該圖形或者遍歷到自己的時候
                    continue;
                    if(Ispoly(poly[i],poly[j])) //如果有交點
                    {
                        c[k++] = j + 'A';
                    }
                }
                if(k == 0) //i與其他圖形全都沒有交點
                {
                    printf("%c has no intersections\n",i+'A');
                }
                else
                {
                    printf("%c intersects with %c",i+'A',c[0]);
                    if(k == 2)
                    {
                        printf(" and %c",c[1]);
                    }
                    else if(k > 2)
                    {
                        for(int m = 1; m < k-1; m++)
                        {
                            printf(", %c",c[m]);
                        }
                        printf(", and %c",c[k-1]); //輸出格式問題
                    }
                    printf("\n");
                }
            }
            printf("\n");
            memset(poly,0,sizeof(poly)); //輸出以後清空
            continue;
        }
        scanf("%s",strr);
        int temp = str[0]-'A'; //用這個式子把字母轉化成數字temp(0~25)排序 因爲輸出要按照字典序輸出
        double x,y;
        if(strcmp(strr,"square") == 0)
        {
            poly[temp].num = 4;
            scanf(" (%lf,%lf)",&x,&y);
            poly[temp].p[0].x = x, poly[temp].p[0].y = y;
            scanf(" (%lf,%lf)",&x,&y);
            poly[temp].p[2].x = x, poly[temp].p[2].y = y;

            poly[temp].p[1].x = (poly[temp].p[0].x+poly[temp].p[2].x +poly[temp].p[2].y-poly[temp].p[0].y)/2;
            poly[temp].p[1].y = (poly[temp].p[0].y+poly[temp].p[2].y+poly[temp].p[0].x-poly[temp].p[2].x)/2;
            poly[temp].p[3].x = (poly[temp].p[0].x+poly[temp].p[2].x +poly[temp].p[0].y-poly[temp].p[2].y)/2;
            poly[temp].p[3].y = (poly[temp].p[0].y+poly[temp].p[2].y+poly[temp].p[2].x-poly[temp].p[0].x)/2;

        }
        else if(strcmp(strr,"rectangle") == 0)
        {
            poly[temp].num = 4;
            scanf(" (%lf,%lf)",&x,&y);
            poly[temp].p[0].x = x, poly[temp].p[0].y = y;
            scanf(" (%lf,%lf)",&x,&y);
            poly[temp].p[1].x = x, poly[temp].p[1].y = y;
            scanf(" (%lf,%lf)",&x,&y);
            poly[temp].p[2].x = x, poly[temp].p[2].y = y;
            poly[temp].p[3].x = (poly[temp].p[0].x + poly[temp].p[2].x - poly[temp].p[1].x);
            poly[temp].p[3].y = ( poly[temp].p[2].y -  poly[temp].p[1].y +  poly[temp].p[0].y);
        }
        else if(strcmp(strr,"line") == 0)
        {
            poly[temp].num = 2;
            scanf(" (%lf,%lf)",&x,&y);
            poly[temp].p[0].x = x, poly[temp].p[0].y = y;
            scanf(" (%lf,%lf)",&x,&y);
            poly[temp].p[1].x = x, poly[temp].p[1].y = y;
        }
        else if(strcmp(strr,"polygon") == 0)
        {
            int n;
            scanf("%d",&n);
            poly[temp].num = n;
            for(int i = 0; i < n; i++)
            {
                scanf(" (%lf,%lf)",&x,&y);
                poly[temp].p[i].x = x, poly[temp].p[i].y = y;
            }
        }
        else
         {

            poly[temp].num = 3;
            scanf(" (%lf,%lf)",&x,&y);
            poly[temp].p[0].x = x, poly[temp].p[0].y = y;
            scanf(" (%lf,%lf)",&x,&y);
            poly[temp].p[1].x = x, poly[temp].p[1].y = y;
            scanf(" (%lf,%lf)",&x,&y);
            poly[temp].p[2].x = x, poly[temp].p[2].y = y;
        }
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章