poj2074 (intersection of the Line)

Topic link: https: //vjudge.net/problem/POJ-2074

The meaning of problems: given L1 (Housing Line), L2 (properity line), and a number of L [i] (obstructions line), find the longest continuous segment L2, such that the section can be seen completely L1 (line of sight is not L [ i] occlusion).

Ideas:

  Simple geometry problems. Of L [i], calculated occlusion interval, assuming Line (L1.e, L [i] .s) and L2 is the intersection of the abscissa t1, Line (L1.s, L [i] .e) and L2 T2 abscissa intersection, then L [i] is the occlusion interval [t1, t2], the x-coordinate is obtained by sorting all the blocked sections, again traversed to determine the longest interval unblocking.

AC code:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
using namespace std;

const double eps=1e-8;
const double inf=1e20;
int sgn(double x){
    if(abs(x)<eps) return 0;
    if(x<0) return -1;
    return 1;
}

struct Point{
    double x,y;
    Point(){}
    Point(double xx,double yy):x(xx),y(yy){}
    Point operator + (const Point& b)const{
        return Point(x+b.x,y+b.y);
    }
    Point operator - (const Point& b)const{
        return Point(x-b.x,y-b.y);
    }
    double operator * (const Point& b)const{
        return x*b.x+y*b.y;
    }
    double operator ^ ( const Point & B) const {
         return X * by-BX * y; 
    } 
    // rotation about the origin angle B (radians), the x, y changes 
    void transXY ( Double B) {
         Double TX = x, TY = Y; 
        X = COS TX * (B) * -TY SiN (B); 
        Y = SiN TX * (B) * TY + COS (B); 
    } 
}; 

struct Line { 
    Point S, E; 
    Line () { } 
    Line (SS Point, Point EE) { 
        S = SS, E = EE; 
    } 
    // two lines intersect to find the intersections
     @A value of 0 indicates the first straight line coincides, expressed as a parallel, intersecting represents 2
     // only the first value of 2, the intersection meaningful 
    pair < int , Point> operator & ( const Line B &) const { 
        Point RES = S;
         IF (SGN ((SE) ^ (BE-BS)) == 0 ) 
        { 
            IF (SGN ((sb.e) ^ (BE-BS)) == 0 )
                 return the make_pair ( 0 , RES) ; // overlap 
            the else  return the make_pair ( . 1 , RES); // parallel 
        }
         Double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
        res.x += (e.x-s.x)*t;
        res.y += (e.y-s.y)*t;
        return make_pair(2,res);
    }
};

double dis(Point a,Point b){
    return sqrt((b-a)*(b-a));
}

typedef pair<double,double> PDD;
const int maxn=1005;
Line L[maxn],L1,L2;
double x1,x2,y,ans;
int n,cnt,cnt2;
PDD pdd[maxn];

bool cmp(PDD p1,PDD p2){
    if(p1.first==p2.first)
        return p1.second<p2.second;
    return p1.first<p2.first;
}

int main(){
    while(scanf("%lf%lf%lf",&x1,&x2,&y),sgn(x1)||sgn(x2)||sgn(y)){
        L1.s.x=x1,L1.e.x=x2,L1.s.y=L1.e.y=y;
        scanf("%lf%lf%lf",&x1,&x2,&y);
        L2.s.x=x1,L2.e.x=x2,L2.s.y=L2.e.y=y;
        scanf("%d",&n);
        cnt=cnt2=0;
        for(int i=0;i<n;++i){
            scanf("%lf%lf%lf",&x1,&x2,&y);
            if(y>=L1.s.y||y<=L2.s.y) continue;
            L[++cnt].s.x=x1,L[cnt].e.x=x2,L[cnt].s.y=L[cnt].e.y=y;
        }
        for(int i=1;i<=cnt;++i){
            double t1=(L2&(Line(L1.e,L[i].s))).second.x;
            double t2=(L2&(Line(L1.s,L[i].e))).second.x;
            if(t2<L2.s.x||t1>L2.e.x) continue;
            if(t1<L2.s.x) t1=L2.s.x;
            if(t2>L2.e.x) t2=L2.e.x;
            pdd[++cnt2].first=t1,pdd[cnt2].second=t2;
        }
        sort(pdd+1,pdd+cnt2+1,cmp);
        ans=L2.e.x-pdd[cnt2].second;
        double t1=L2.s.x;
        int j=1;
        while(1){
            while(pdd[j].first<t1){
                t1=max(t1,pdd[j].second);
                ++j;
            }
            if(j>cnt2) break;
            if(pdd[j].first>=t1){
                ans=max(ans,pdd[j].first-t1);
                t1=pdd[j].second;
                ++j;
            }
            if(j>cnt2) break;
        }
        if(sgn(ans)!=0)
            printf("%.2f\n",ans);
        else
            printf("No View\n");
    }
}

 

Guess you like

Origin www.cnblogs.com/FrankChen831X/p/11824289.html