ac1097

判断线段与直线的相交  这里有个地方需要注意的就是在 转换的时候容易报错 在叉积完后的判断符号的时候需要注意 这个地方会超出int 的范围

2014-06-0320:14:04

#include <iostream>
#include <cstdio>
#include <string.h>
#include <cmath>
using namespace std;
struct point{
   int x,y;
   point(int a = 0 , int b = 0){
       x =a ; y = b;
     }
}R[105];
point operator - (const point a ,const point b){
   return  point ( a.x - b.x , a.y - b.y );
}
struct line{
    point a,b;
}T[ 58 ];
int n;
int cross( point a, point b){
      return a.x*b.y - b.x* a.y;
}

bool eq(point a,point b){
   if(a.x == b.x&&a.y == b.y) return true;
   else return false;
}
int maxv(int a, int b){
   return a>b?a:b;
}
int jj(int a){
   if(a>0) return 1;
   if(a<0) return -1;
   else return 0;
}
int main()
{
     int t ;
     scanf("%d",&t);
     while(t --){
          scanf("%d",&n);
        int num =0 ;
        for( int i = 0 ; i < n ; i++){
                int x1,x2,y1,y2;
             scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
             T[i].a=point(x1,y1);
             T[i].b= point(x2,y2);
              R[num++] = T[i].a;
              R[num++] =T[i].b;
        }
        int ans =0;
        for( int i = 0 ; i <num ; ++i){
            for(int j = i+1 ; j < num ; j++ ){
                   if(eq(R[i],R[j])) continue;
                  int ge = 0;
                  line an;
                  an.a = R[i];
                  an.b = R[j]-an.a;
                  for(int  k = 0 ; k < n ; k ++){

                         int t1 =jj(cross( an.b , T[k].a - an.a ));
                         int t2 =jj(cross( an.b , T[k].b - an.a ));

                          if(t1 *  t2 <= 0 ) ge++;
                  }
                  ans =maxv(ans,ge);
            }

        }

        printf("%d
",ans);
     }

    return 0;
}
View Code
原文地址:https://www.cnblogs.com/Opaser/p/3766405.html