判断线段相交并求交点

版权声明:转我原创记得说你是我的脑残粉哟 https://blog.csdn.net/zjy2015302395/article/details/78089692
const double precision = 1e-6;
const double inf = (1<<30);
const int maxn = 1e6+10;
int n;
struct Tpoint{
    double x;
    double y;
    Tpoint(double x = 0, double y = 0) : x(x) , y(y) { }

    void print(){
        printf(" (%.3f,%.3f) \n",x,y);
    }
}l11,l12,l21,l22,l31,l32,l41,l42,bl11,bl12,bl21,bl22,bl31,bl32,bl41,bl42;
Tpoint operator + (Tpoint A, Tpoint B) { return Tpoint(A.x+B.x, A.y+B.y); }
Tpoint operator - (Tpoint A, Tpoint B) { return Tpoint(A.x-B.x, A.y-B.y); }
Tpoint operator * (Tpoint A, double p) { return Tpoint(A.x*p, A.y*p); }
Tpoint operator / (Tpoint A, double p) { return Tpoint(A.x/p, A.y/p); }
struct Tline{
    Tpoint a,b;
    Tline(Tpoint x,Tpoint y):a(x),b(y){};
};
Tpoint bzx,bys,szx,sys,a[maxn],b[maxn];
int line[maxn];
pii check[5],inter[5];
pdi ans[maxn*4];
int anscnt = 0;

double crossprod(Tpoint a,Tpoint b,Tpoint c){
  return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}

bool intersection(Tpoint a,Tpoint b,Tpoint c,Tpoint d){
  if(max(a.x,b.x)>=min(c.x,d.x) &&
     max(c.x,d.x)>=min(a.x,b.x) &&
     max(a.y,b.y)>=min(c.y,d.y) &&
     max(c.y,d.y)>=min(a.y,b.y) &&
     crossprod(a,b,c)*crossprod(a,b,d)<=0 &&
     crossprod(c,d,a)*crossprod(c,d,b)<=0)return 1;
  return 0;
}
double det(Tpoint a,Tpoint b){
    return a.x*b.y-a.y*b.x;
}
pii GetLineIntersection(Tpoint A, Tpoint B, Tpoint C, Tpoint D) {
    Tline a = Tline(A,B);
    Tline b = Tline(C,D);
    double s1 = det(a.a-b.a,b.b-b.a);
    double s2 = det(a.b-b.a,b.b-b.a);
    Tpoint res ;
    res = (a.b*s1-a.a*s2)/(s1-s2);
    return make_pair(res.x,res.y);
}

void sov(){
    for(int i = 0 ; i < n ; i++){
        int cnt = 0;
        if(intersection(a[i],b[i],l11,l12) ) {inter[cnt++] = GetLineIntersection(a[i],b[i],l11,l12);pii ans1=inter[cnt-1];flag1 = true;cout << "1: ( "<<ans1.first<<" , "<<ans1.second<<" )"<<endl;}
        if(intersection(a[i],b[i],l21,l22) ) {inter[cnt++] = GetLineIntersection(a[i],b[i],l21,l22);pii ans1=inter[cnt-1];flag2 = true;cout << "2: ( "<<ans1.first<<" , "<<ans1.second<<" )"<<endl;}
        if(intersection(a[i],b[i],l31,l12) ) {inter[cnt++] = GetLineIntersection(a[i],b[i],l31,l32);pii ans1=inter[cnt-1];flag3 = true;cout << "3: ( "<<ans1.first<<" , "<<ans1.second<<" )"<<endl;}
        if(intersection(a[i],b[i],l41,l42) ) {inter[cnt++] = GetLineIntersection(a[i],b[i],l41,l42);pii ans1=inter[cnt-1];flag4 = true;cout << "4: ( "<<ans1.first<<" , "<<ans1.second<<" )"<<endl;}
        if(intersection(a[i],b[i],bl11,bl12) ) {inter[cnt++] = GetLineIntersection(a[i],b[i],bl11,bl12);pii ans1=inter[cnt-1];flag5 = true;cout << "5: ( "<<ans1.first<<" , "<<ans1.second<<" )"<<endl;}
        if(intersection(a[i],b[i],bl21,bl22) ) {inter[cnt++] = GetLineIntersection(a[i],b[i],bl21,bl22);pii ans1=inter[cnt-1];flag6 = true;cout << "6: ( "<<ans1.first<<" , "<<ans1.second<<" )"<<endl;}
        if(intersection(a[i],b[i],bl31,bl32) ) {inter[cnt++] = GetLineIntersection(a[i],b[i],bl31,bl32);pii ans1=inter[cnt-1];flag7 = true;cout << "7: ( "<<ans1.first<<" , "<<ans1.second<<" )"<<endl;}
        if(intersection(a[i],b[i],bl41,bl42) ) {inter[cnt++] = GetLineIntersection(a[i],b[i],bl41,bl42);pii ans1=inter[cnt-1];flag8 = true;cout << "8: ( "<<ans1.first<<" , "<<ans1.second<<" )"<<endl;}
    }
}
阅读更多

没有更多推荐了,返回首页