poj 3449 Geometric Shapes

输入输出超恶心的,这个题要注意的是线段不是规范相交,也算相交,没有图形包含的情况;

思路:计算几何。思路并不复杂,枚举当前几何体的所有边和其他几何体的所有边比较。另外,已知正方形的一对不相邻的顶点(x0,y0),(x2,y2),可以由方程组:

x1 + x3 = x0 + x2;

x1 - x3 =y0- y2  ;

y1 + y3 = y0 + y2;

y3 - y1 = x0 - x2  ;

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

矩形另外一个点为 x3 = x0 + x2 - x1 ,y3 = y0 + y2 - y1;

View Code
#include<iostream>

#include<cstdio>

#include<cstdlib>

#include<algorithm>

#include<cmath>

#include<cstring>

using namespace std;

class Point{

public:

      double x,y;    

};

class Polygon {

public:

      int n,N;

      char num;

      Point point[24];

      int node[30];

      bool operator < (Polygon b) const {

            return num < b.num;

        }

}polygon[30];

int Input( int n , int cnt ,char num){

    for( int i = 0 ; i < n ; i ++ ){

       scanf( " (%lf,%lf)",&polygon[cnt].point[i].x,&polygon[cnt].point[i].y );    

    }

    polygon[cnt].n = n;    

    polygon[cnt].num = num; 

    cnt ++;

    return cnt;

}

void Get_tri( int n ){

    polygon[n-1].n = 4;

    polygon[n-1].point[3].x=polygon[n-1].point[0].x+polygon[n-1].point[2].x-polygon[n-1].point[1].x;

    polygon[n-1].point[3].y=polygon[n-1].point[0].y+polygon[n-1].point[2].y-polygon[n-1].point[1].y;

}

void Get_point( int n ){

    Point p,m;

    polygon[n-1].point[2] = polygon[n-1].point[1];

    p.x = ( polygon[n-1].point[0].x + polygon[n-1].point[2].x )/2.0;

    p.y = ( polygon[n-1].point[0].y + polygon[n-1].point[2].y )/2.0;

    m.x =  polygon[n-1].point[0].x - p.x; m.y =  polygon[n-1].point[0].y - p.y;

    polygon[n-1].point[1].x = -m.y + p.x; polygon[n-1].point[1].y = m.x + p.y;

    m.x =  polygon[n-1].point[2].x - p.x; m.y =  polygon[n-1].point[2].y - p.y;

    polygon[n-1].point[3].x = -m.y + p.x; polygon[n-1].point[3].y = m.x + p.y;

    polygon[n-1].n = 4;

}

double on_segment( Point p1 , Point p2  , Point q  ){

   return ( p1.x - q.x)*( p2.y - q.y ) - ( p2.x - q.x )*( p1.y - q.y );

}

double Min( double a , double b ){

     return a < b ?a : b;

    }

double Max( double a, double b ){

    return a > b ? a : b;

    }

int dcmp( double x)

{

   if( fabs( x )< 1.0e-8) return 0;

   if( x < 0 ) return -1;

   return 1; 

}

bool judge( Point p1 , Point p2 ,Point q ){

    double max_x = Max( p1.x ,p2.x );

    double min_x = Min( p1.x ,p2.x );

    if( q.x >= min_x && q.x <= max_x  )

        return true;

    return false;

}

bool segment( Point p1 , Point p2 , Point q1 ,Point q2 ){

    int d1 = dcmp(on_segment( p1 , p2 , q1 ));

    int d2 = dcmp(on_segment( p1 , p2 , q2 ));

    int d3 = dcmp(on_segment( q1 , q2 , p1 ));

    int d4 = dcmp(on_segment( q1 , q2 , p2 ));

    if( d1*d2 < 0 && d3*d4< 0 ) return true;

    if( d1==0 && judge( p1 , p2 , q1 )) return true;

    if( d2==0 && judge( p1 , p2 , q2 )) return true;

    if( d3==0 && judge( q1,  q2 , p1 )) return true;

    if( d4==0 && judge( q1 , q2 , p2 ))return true;

    return false;    

}

bool Cross( int n , int m ){

    int t = polygon[n].n ,tt=polygon[m].n;

   for( int k = 0 ; k < t ; k ++ ){

      for( int l = 0 ; l <tt ; l ++ ){

        if(segment(polygon[n].point[k],polygon[n].point[(k+1)%t],polygon[m].point[l],polygon[m].point[(l+1)%tt] ))

          return true;

        }

    }    

    return false;  

}

void Solve( int n ){

    for( int i = 0 ; i < n ; i ++ )

         polygon[i].N = 0;

    for( int i = 0 ; i < n ; i++ ){        

        for( int j = i+1 ; j < n ; j ++ ){

            if( Cross( i , j ) ){

    //            printf( "%d %d\n",i,j );

              polygon[i].node[polygon[i].N++] = j;

              polygon[j].node[polygon[j].N++] = i;

             }

            }

        }    

}

int main(  ){

    char num[3],str[100];

    int cnt = 0,n;

    while( scanf( "%s",num ) , num[0]!='.' ){

        if( num[0] != '-' ){

            scanf( "%s",str );

            switch( str[0] ){

               case 's':cnt = Input( 2 ,cnt,num[0] );

                        Get_point( cnt ); break;

               case 'l':cnt = Input( 2 ,cnt,num[0] ); break;

               case 't':cnt = Input( 3 ,cnt,num[0] );break;

               case 'p':scanf( "%d",&n );cnt = Input( n ,cnt,num[0] );break;

               case 'r':cnt = Input( 3 ,cnt,num[0] );

                        Get_tri( cnt );break;

            }

        }

        else {

            sort( polygon , polygon + cnt );

            Solve( cnt );

            for( int i =0 ; i < cnt ; i ++ ){

               if( polygon[i].N ==0 ) printf( "%c has no intersections\n",polygon[i].num );

               else { printf( "%c intersects with ",polygon[i].num );

                    if( polygon[i].N == 1 )printf( "%c\n",polygon[polygon[i].node[0]].num );

                    else if( polygon[i].N == 2 ) 

                    printf( "%c and %c\n",polygon[polygon[i].node[0]].num,polygon[polygon[i].node[1]].num );

                    else

                    {int j;

                    for(  j = 0 ; j < polygon[i].N -1; j ++ )

                      printf( "%c, ",polygon[polygon[i].node[j]].num );

                          printf( "and %c\n",polygon[polygon[i].node[j]].num );                    



                   }

            }            

            }

            puts( "" );

            cnt = 0;    

        }

    }

    //system( "pause" );

    return 0;

}

 

你可能感兴趣的:(shape)