POJ 2074 | 线段相交
#include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #define eps 1e-8 using namespace std; bool dcmp(double x,double y) { if (fabs(x-y)>eps) return 1; return 0; } struct point { double x,y; point () {}; point (double _x,double _y) { x=_x,y=_y; } point operator + (const point &a)const { return point(x+a.x,y+a.y); } point operator - (const point &a) const { return point(x-a.x,y-a.y); } double operator * (const point &a)const { return x*a.y-a.x*y; } double dot (const point &a,const point &b) { return a.x*b.x+a.y*b.y; } bool operator < (const point &a)const { return x<a.x; } bool operator == (const point &a)const { return dcmp(x,a.x) && dcmp(y,a.y); } }; struct range { double x1,x2,y; bool operator < (const range &a)const { return x1<a.x1; } }house,block[1010],road; bool cmp(range a,range b) { return a.x1<b.x1; } double findinter(point p1,point p2,point p3,point p4){ double ans=((p1.x-p3.x)*(p3.y-p4.y)-(p1.y-p3.y)*(p3.x-p4.x))/((p1.x-p2.x)*(p3.y-p4.y)-(p1.y-p2.y)*(p3.x-p4.x)); return p1.x+(p2.x-p1.x)*ans; } double getIntersect(point a,point b,point c,point d) { double A1=b.y-a.y,B1=a.x-b.x,C1=(b.x-a.x)*a.y-(b.y-a.y)*a.x; double A2=d.y-c.y,B2=c.x-d.x,C2=(d.x-c.x)*c.y-(d.y-c.y)*c.x; return (C2*B1-C1*B2)/(A1*B2-A2*B1); } int n; int main() { // freopen("1.out","w",stdout); while (scanf("%lf%lf%lf",&house.x1,&house.x2,&house.y)!=0) { if (house.x1==0 && house.x2==0 && house.y==0) break; scanf("%lf%lf%lf",&road.x1,&road.x2,&road.y); scanf("%d",&n); for (int i=0;i<n;i++) scanf("%lf%lf%lf",&block[i].x1,&block[i].x2,&block[i].y); sort(block,block+n,cmp); point p1,p2,p3,p4; p3=point(road.x1,road.y); p4=point(road.x2,road.y); double ans=-1,Lmax=-1.0; for (int i=0;i<=n;i++) { double l,r; if (block[i].y>=house.y) continue; if (i==0) l=road.x1; else { p1=point(house.x1,house.y); p2=point(block[i-1].x2,block[i-1].y); l=findinter(p1,p2,p3,p4); } if (i==n) r=road.x2; else { p1=point(house.x2,house.y); p2=point(block[i].x1,block[i].y); r=findinter(p1,p2,p3,p4); } if (l<road.x1) l=road.x1; if (r>road.x2) r=road.x2; if (l<Lmax) l=Lmax; Lmax=max(Lmax,l); ans=max(ans,r-l); } if (ans<=0) puts("No View"); else printf("%.2f\n",ans); } return 0; }