POJ 3449 判断多边形与多边形是否相交

来源:互联网 发布:mpii数据集介绍 编辑:程序博客网 时间:2024/06/06 05:29


const  double eps = 1e-10 ;double  add(double x , double y){        return fabs(x + y) < eps * (fabs(x) + fabs(y)) ? 0 : x+y ;}struct Point{       double x , y ;       Point(){}       Point(double _x , double _y):x(_x) , y(_y){}       Point operator - (Point o){             return Point(add(x , -o.x) , add(y , -o.y)) ;       }       Point operator + (Point o){             return Point(add(x , o.x) , add(y , o.y)) ;       }       double operator ^ (Point o){             return x * o.y - y * o.x ;       }};int   onseg(Point p1 ,Point p2 , Point q){      return   (p1 - q).x * (p2 - q).x <= 0            && (p1 - q).y * (p2 - q).y <= 0 ;}//判断线段p1p2, 线段q1q2是否相交int   intersection(Point p1 , Point p2 ,Point q1 , Point q2){      double d1 = (p2 - p1) ^ (q1 - p1) ;      double d2 = (p2 - p1) ^ (q2 - p1) ;      double d3 = (q2 - q1) ^ (p1 - q1) ;      double d4 = (q2 - q1) ^ (p2 - q1) ;      if(d1 == 0 && onseg(p1 , p2 , q1)) return 1 ;      if(d2 == 0 && onseg(p1 , p2 , q2)) return 1 ;      if(d3 == 0 && onseg(q1 , q2 , p1)) return 1 ;      if(d4 == 0 && onseg(q1 , q2 , p2)) return 1 ;      if(d1 * d2 < 0 && d3 * d4 < 0) return 1 ;      return 0 ;}struct  Segline{      Point s , t ;      Segline(){}      Segline(Point _s , Point _t):s(_s) , t(_t){}      int  segintersec(Segline o){           return intersection(s , t , o.s , o.t) ;      }};struct  Poly{      string name ;      int  n ;      vector<Point> lispoint ;      Poly(){}      Poly(int _n):n(_n){           lispoint.clear() ;      }      int  polyintersec(Poly o){           for(int i = 0 ; i < n ; i++){               Segline a = Segline(lispoint[i] , lispoint[(i+1)%n]) ;               for(int j = 0 ; j < o.n ; j++){                   Segline b = Segline(o.lispoint[j] , o.lispoint[(j+1)%o.n]) ;                   if(a.segintersec(b))  return 1 ;               }           }           return 0  ;      }      friend bool operator < (Poly a , Poly b){           return a.name < b.name ;      }};int  main(){     char  sname[108] , skind[108] ;     double x[4] , y[4] ;     int i , j , k ,  _n ;     Poly py ;     vector<Poly> lis ;  lis.clear() ;     while(scanf("%s" , sname)){          if(strcmp(sname , ".") == 0)  break ;          if(strcmp(sname , "-") == 0){               sort(lis.begin() , lis.end()) ;               for(i = 0 ; i < lis.size() ; i++){                   Poly a = lis[i] ;                   vector<string> ans ;ans.clear() ;                   for(j = 0 ; j < lis.size() ; j++){                       if(i == j) continue ;                       Poly b = lis[j] ;                       if(a.polyintersec(b)) ans.push_back(b.name) ;                   }                   if(ans.size() == 0)                      printf("%s has no intersections\n" , a.name.c_str()) ;                   else if(ans.size() == 1)                      printf("%s intersects with %s\n" , a.name.c_str() , ans[0].c_str()) ;                   else if(ans.size() == 2)                      printf("%s intersects with %s and %s\n" , a.name.c_str() , ans[0].c_str() , ans[1].c_str() ) ;                   else{                      printf("%s intersects with" , a.name.c_str()) ;                      for(k = 0 ; k < ans.size() - 1 ; k++) printf(" %s," , ans[k].c_str()) ;                      printf(" and %s\n" , ans[k].c_str()) ;                   }               }               lis.clear() ;               puts("")  ;               continue ;          }          scanf("%s" , skind) ;          if(skind[0] == 'l'){                scanf(" (%lf,%lf)" , &x[0] , &y[0]) ;                scanf(" (%lf,%lf)" , &x[1] , &y[1]) ;                py = Poly(2) ;                for(i = 0 ; i < 2 ; i++)                   py.lispoint.push_back(Point(x[i] , y[i])) ;          }          else if(skind[0] == 's'){                scanf(" (%lf,%lf)" , &x[0] , &y[0]) ;                scanf(" (%lf,%lf)" , &x[2] , &y[2]) ;                x[1] = (x[0] + x[2] + y[2] - y[0]) * 0.5 ;                y[1] = (y[0] + y[2] - x[2] + x[0]) * 0.5 ;                x[3] = (x[0] + x[2] - y[2] + y[0]) * 0.5 ;                y[3] = (y[0] + y[2] + x[2] - x[0]) * 0.5 ;                py = Poly(4) ;                for(i = 0 ; i < 4 ; i++)                   py.lispoint.push_back(Point(x[i] , y[i])) ;          }          else if(skind[0] == 'r'){                scanf(" (%lf,%lf)" , &x[0] , &y[0]) ;                scanf(" (%lf,%lf)" , &x[1] , &y[1]) ;                scanf(" (%lf,%lf)" , &x[2] , &y[2]) ;                x[3] = x[0] + x[2] - x[1] ;                y[3] = y[0] + y[2] - y[1] ;                py = Poly(4) ;                for(i = 0 ; i < 4 ; i++)                   py.lispoint.push_back(Point(x[i] , y[i])) ;          }          else if(skind[0] == 't'){                py = Poly(3) ;                for(i = 0 ; i < 3 ; i++){                     scanf(" (%lf,%lf)" , &x[0] , &y[0]) ;                     py.lispoint.push_back(Point(x[0] , y[0])) ;                }          }          else{                scanf(" %d" , &_n) ;                py = Poly(_n) ;                for(i = 0 ; i < py.n ; i++){                     scanf(" (%lf,%lf)" , &x[0] , &y[0]) ;                     py.lispoint.push_back(Point(x[0] , y[0])) ;                }          }          py.name = string(sname) ;          lis.push_back(py) ;     }     return 0 ;}



0 0
原创粉丝点击