leetcode335

坐标席判断线段是否相交

class Solution {
public:
    bool isSelfCrossing(vector<int>& distance) {
        if(distance.size()<4){
            return false;
        }
        vector<pair<int, int>> s = {
            make_pair(0, 0),
            make_pair(0,distance[0]),
            make_pair(-distance[1],distance[0]),
            make_pair(-distance[1],distance[0]-distance[2]),
            make_pair(0, 0),
            make_pair(0, 0),
            make_pair(0, 0),
            make_pair(0, 0),
            make_pair(0, 0),
            make_pair(0, 0)
            };
        for(int i=3;i<5&&i<distance.size();i++){
            //右上坐标
            if((i+1)%4==1){
                s[6].first=s[0].first;
                s[6].second=s[0].second+distance[i];
                if(isCrossing(s[0],s[6],s[1],s[2])||s[6]==s[4]){
                    return true;
                }
                s[1]=s[6];
            //右下坐标
            }else{
                s[6].first=s[3].first+distance[i];
                s[6].second=s[3].second;
                if(isCrossing(s[3],s[6],s[0],s[1])){
                    return true;
                }
                s[4]=s[0];
                s[0]=s[6];
            }
        }
        if(distance.size()<5){
            return false;
        }
        s[0] = make_pair(0, 0);
        s[1] = make_pair(0,distance[0]);
        s[2] = make_pair(-distance[1],distance[0]);
        s[3] = make_pair(-distance[1],distance[0]-distance[2]);
        s[4] = make_pair(-distance[1]+distance[3],distance[0]-distance[2]);
        s[5] = make_pair(-distance[1]+distance[3],distance[0]-distance[2]+distance[4]);
        //cout<<"------"<<endl;
        //for(int i=0;i<6;i++){
        //    cout<<"("<<s[i].first<<","<<s[i].second<<")"<<endl;
        //}
        //内外圈
        bool signal=true;//默认外圈
        if(distance[2]<=distance[0]||distance[3]<=distance[1]){
            signal=false;
            cout<<1<<endl;
        }
        int cru=0,last,next;
        int point,left,right;//判断是否入内圈
        for(int i=5;i<distance.size();i++){
            if(signal){
                last=cru==0?5:cru-1;
                next=cru==5?0:cru+1;
                //
                if((i+1)%4==1){
                    //cout<<1<<endl;
                    s[6]=make_pair(s[last].first, s[last].second+distance[i]);
                    if(isCrossing(s[last],s[6],s[cru],s[next])){
                        //cout<<"cru:"<<cru<<endl;
                        //cout<<"("<<s[last].first<<","<<s[last].second<<")"<<endl;
                        //cout<<"("<<s[6].first<<","<<s[6].second<<")"<<endl;
                        //cout<<"("<<s[cru].first<<","<<s[cru].second<<")"<<endl;
                        //cout<<"("<<s[next].first<<","<<s[next].second<<")"<<endl;
                        return true;
                    }
                    point=s[6].first;
                    left=s[cru].first;
                    right=s[next].first;
                    if(
                       (s[6].first>=s[cru].first&&s[6].first<=s[next].first)||
                       (s[6].first<=s[cru].first&&s[6].first>=s[next].first)
                    ){
                        signal=false;
                    }
                    s[cru]=s[6];
                    if(!signal){
                        s[6]=s[0];
                        s[7]=s[1];
                        s[8]=s[2];
                        s[9]=s[3]; 
                        //赋值
                        s[1]=s[cru];
                        s[0]=s[last<4?last+6:last];
                        last=last==0?5:last-1;
                        s[3]=s[last<4?last+6:last];
                        last=last==0?5:last-1;
                        s[2]=s[last<4?last+6:last];
                    }
                //
                }else if((i+1)%4==2){
                    //cout<<2<<endl;
                    s[6]=make_pair(s[last].first-distance[i], s[last].second);
                    if(isCrossing(s[last],s[6],s[cru],s[next])){
                        //cout<<"cru:"<<cru<<endl;
                        //cout<<"("<<s[last].first<<","<<s[last].second<<")"<<endl;
                        //cout<<"("<<s[6].first<<","<<s[6].second<<")"<<endl;
                        //cout<<"("<<s[cru].first<<","<<s[cru].second<<")"<<endl;
                        //cout<<"("<<s[next].first<<","<<s[next].second<<")"<<endl;
                        return true;
                    }
                    if(
                       (s[6].second>=s[cru].second&&s[6].second<=s[next].second)||
                       (s[6].second<=s[cru].second&&s[6].second>=s[next].second)
                    ){
                        signal=false;
                    }
                    s[cru]=s[6];
                    if(!signal){
                        s[6]=s[0];
                        s[7]=s[1];
                        s[8]=s[2];
                        s[9]=s[3]; 
                        //赋值
                        s[2]=s[cru];
                        s[1]=s[last<4?last+6:last];
                        last=last==0?5:last-1;
                        s[0]=s[last<4?last+6:last];
                        last=last==0?5:last-1;
                        s[3]=s[last<4?last+6:last];
                    }
                //
                }else if((i+1)%4==3){
                    //cout<<3<<endl;
                    s[6]=make_pair(s[last].first, s[last].second-distance[i]);
                    if(isCrossing(s[last],s[6],s[cru],s[next])){
                        //cout<<"cru:"<<cru<<endl;
                        //cout<<"("<<s[last].first<<","<<s[last].second<<")"<<endl;
                        //cout<<"("<<s[6].first<<","<<s[6].second<<")"<<endl;
                        //cout<<"("<<s[cru].first<<","<<s[cru].second<<")"<<endl;
                        //cout<<"("<<s[next].first<<","<<s[next].second<<")"<<endl;
                        return true;
                    }
                    if(
                       (s[6].first>=s[cru].first&&s[6].first<=s[next].first)||
                       (s[6].first<=s[cru].first&&s[6].first>=s[next].first)
                    ){
                        signal=false;
                    }
                    s[cru]=s[6];
                    if(!signal){
                        s[6]=s[0];
                        s[7]=s[1];
                        s[8]=s[2];
                        s[9]=s[3]; 
                        //赋值
                        s[3]=s[cru];
                        s[2]=s[last<4?last+6:last];
                        last=last==0?5:last-1;
                        s[1]=s[last<4?last+6:last];
                        last=last==0?5:last-1;
                        s[0]=s[last<4?last+6:last];
                    }
                //
                }else{
                    //cout<<4<<endl;
                    s[6]=make_pair(s[last].first+distance[i], s[last].second);
                    if(isCrossing(s[last],s[6],s[cru],s[next])){
                        //cout<<"cru:"<<cru<<endl;
                        //cout<<"("<<s[last].first<<","<<s[last].second<<")"<<endl;
                        //cout<<"("<<s[6].first<<","<<s[6].second<<")"<<endl;
                        //cout<<"("<<s[cru].first<<","<<s[cru].second<<")"<<endl;
                        //cout<<"("<<s[next].first<<","<<s[next].second<<")"<<endl;
                        return true;
                    }
                    if(
                       (s[6].second>=s[cru].second&&s[6].second<=s[next].second)||
                       (s[6].second<=s[cru].second&&s[6].second>=s[next].second)
                    ){
                        signal=false;
                    }
                    s[cru]=s[6];
                    if(!signal){
                        s[6]=s[0];
                        s[7]=s[1];
                        s[8]=s[2];
                        s[9]=s[3]; 
                        //赋值
                        s[0]=s[cru];
                        s[3]=s[last<4?last+6:last];
                        last=last==0?5:last-1;
                        s[2]=s[last<4?last+6:last];
                        last=last==0?5:last-1;
                        s[1]=s[last<4?last+6:last];
                    }
                }
            //cout<<"------"<<endl;
            //for(int i=0;i<6;i++){
            //    cout<<"("<<s[i].first<<","<<s[i].second<<")"<<endl;
            //}
            cru=next;
            }else{
                //右上坐标
                if((i+1)%4==1){
                    s[6].first=s[0].first;
                    s[6].second=s[0].second+distance[i];
                    if(isCrossing(s[0],s[6],s[1],s[2])){
                        return true;
                    }
                    s[1]=s[6];
                //左上坐标
                }else if((i+1)%4==2){
                    s[6].first=s[1].first-distance[i];
                    s[6].second=s[1].second;
                    if(isCrossing(s[1],s[6],s[2],s[3])){
                        return true;
                    }
                    s[2]=s[6];
                //左下坐标
                }else if((i+1)%4==3){
                    s[6].first=s[2].first;
                    s[6].second=s[2].second-distance[i];
                    if(isCrossing(s[2],s[6],s[3],s[0])){
                        return true;
                    }
                    s[3]=s[6];
                //右下坐标
                }else{
                    s[6].first=s[3].first+distance[i];
                    s[6].second=s[3].second;
                    if(isCrossing(s[3],s[6],s[0],s[1])){
                        return true;
                    }
                    s[0]=s[6];
                }
            }
            
        }
        return false;
    }
    bool isCrossing(pair<int, int> &s1,pair<int, int> &s2,pair<int, int> &s3,pair<int, int> &s4) {
        if(s2==s3){
            return true;
        }
        if(min(s1.first,s2.first)<=max(s3.first,s4.first) && min(s3.second,s4.second)<=max(s1.second,s2.second)&&min(s3.first,s4.first)<=max(s1.first,s2.first) && min(s1.second,s2.second)<=max(s3.second,s4.second)) 
      return true;
        double u,v,w,z;//分别记录两个向量
        u=((double)s3.first-(double)s1.first)*((double)s2.second-(double)s1.second)-((double)s2.first-(double)s1.first)*((double)s3.second-(double)s1.second);
        v=((double)s4.first-(double)s1.first)*((double)s2.second-(double)s1.second)-((double)s2.first-(double)s1.first)*((double)s4.second-(double)s1.second);
        w=((double)s1.first-(double)s3.first)*((double)s4.second-(double)s3.second)-((double)s4.first-(double)s3.first)*((double)s1.second-(double)s3.second);
        z=((double)s2.first-(double)s3.first)*((double)s4.second-(double)s3.second)-((double)s4.first-(double)s3.first)*((double)s2.second-(double)s3.second);
        return (u*v<=0.00000001 && w*z<=0.00000001);
    }
};

 

posted @ 2021-11-01 11:00  巴啦啦大魔王  阅读(28)  评论(0编辑  收藏  举报