POJ2074Line of Sight【直线相交判定+求交点】

来源:互联网 发布:华为4 a数据分区破坏 编辑:程序博客网 时间:2024/05/16 06:31

 

Language:
Line of Sight
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 3895 Accepted: 1216

Description

An architect is very proud of his new home and wants to be sure it can be seen by people passing by his property line along the street. The property contains various trees, shrubs, hedges, and other obstructions that may block the view. For the purpose of this problem, model the house, property line, and obstructions as straight lines parallel to the x axis: 

To satisfy the architect's need to know how visible the house is, you must write a program that accepts as input the locations of the house, property line, and surrounding obstructions and calculates the longest continuous portion of the property line from which the entire house can be seen, with no part blocked by any obstruction.

Input

Because each object is a line, it is represented in the input file with a left and right x coordinate followed by a single y coordinate: 
< x1 > < x2 > < y > 
Where x1, x2, and y are non-negative real numbers. x1 < x2 
An input file can describe the architecture and landscape of multiple houses. For each house, the first line will have the coordinates of the house. The second line will contain the coordinates of the property line. The third line will have a single integer that represents the number of obstructions, and the following lines will have the coordinates of the obstructions, one per line. 
Following the final house, a line "0 0 0" will end the file. 
For each house, the house will be above the property line (house y > property line y). No obstruction will overlap with the house or property line, e.g. if obstacle y = house y, you are guaranteed the entire range obstacle[x1, x2] does not intersect with house[x1, x2].

Output

For each house, your program should print a line containing the length of the longest continuous segment of the property line from which the entire house can be to a precision of 2 decimal places. If there is no section of the property line where the entire house can be seen, print "No View".

Sample Input

2 6 60 15 031 2 13 4 112 13 11 5 50 10 010 15 10 0 0

Sample Output

8.80No View

题意:给出一个房子(看成线段)的端点坐标,和一条路的两端坐标,给出一些障碍物(看成线段)的两端坐标。问在路上能看到完整房子的最大连续长度是多长。

解题思路:将障碍物按一个端点坐标排序然后用房子的右端端与障碍物的左端比较房子的右端和前一障碍物的右端比较的出在道路上的能看到的长度取最大长度即可

#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#define eps 1e-8using namespace std;struct Node{double x1,x2,y;}house,Pl,A[110];struct point{double x,y;};bool cmp(Node a,Node b){return a.x1<b.x1;}double MAX(double a,double b){return a>b?a:b;}double MIN(double a,double b){return a<b?a:b;}void findinter(point p1,point p2,point p3,point p4,point &inter){      inter=p1;      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));      inter.x+=(p2.x-p1.x)*ans;      inter.y+=(p2.y-p1.y)*ans;  }  int main(){int n,i,j;while(scanf("%lf%lf%lf",&house.x1,&house.x2,&house.y)){if(house.x1==0&&house.x2==0&&house.y==0)break;scanf("%lf%lf%lf",&Pl.x1,&Pl.x2,&Pl.y);scanf("%d",&n);for(i=0;i<n;++i){scanf("%lf%lf%lf",&A[i].x1,&A[i].x2,&A[i].y);}stable_sort(A,A+n,cmp);//按照障碍物的左端点排序int pos;double L=-1,temp;double LMAX=-1;point p1,p2,p3,p4,inter;p3.x=Pl.x1;p3.y=Pl.y;p4.x=Pl.x2;p4.y=Pl.y;for(i=0;i<=n;++i){//枚举每个障碍物double l,r;//在道路上的左右交点if(A[i].y>=house.y)continue;//不在房子和道路之间不考虑if(i==0){l=Pl.x1;}else {p1.x=house.x1;p1.y=house.y;p2.x=A[i-1].x2;p2.y=A[i-1].y;findinter(p1,p2,p3,p4,inter);//求出在道路上的左交点l=inter.x;}if(i==n){r=Pl.x2;}else {p1.x=house.x2;p1.y=house.y;p2.x=A[i].x1;p2.y=A[i].y;findinter(p1,p2,p3,p4,inter);//求出在道路上的右交点r=inter.x;}if(l<Pl.x1)l=Pl.x1;if(r>Pl.x2)r=Pl.x2;if(l<LMAX)l=LMAX;//说明之前障碍物能遮挡住该点所以应该改变左端点的值LMAX=MAX(LMAX,l);L=MAX(L,r-l);}if(L<=0)printf("No View\n");else printf("%.2lf\n",L);}return 0;}//多亏了discuss里的大神们提供的经典测试数据要不然估计我要wa到天亮了。//2 6 6//0 15 0//3//1 2 1//3 4 1//12 13 1//1 5 5//0 10 0//1//0 15 1//2 6 6//0 15 0//3//1 2 1//3 4 1//12 13 1//2 6 6我在这组数据上wa了好几次//0 15 0//4//1 2 1//3 4 1//12 13 1//1 5 2//2 6 6//0 15 0//2//0 5 3//6 15 3//2 6 6//0 15 0//2//6 10 1//0 2 1//2 6 6//0 15 0//1//2 6 7//2 6 6//0 15 0//1//2 6 7//2 6 6//0 15 0//1//4 4.5 5.5//2 6 6//0 15 0//16//0 1 3//1.5 2 3//2.5 3 3//3.5 4 3//4.5 5 3//5.5 6 3//6.5 7 3//7.5 8 3//8.5 9 3//9.5 10 3//10.5 11 3//11.5 12 3//12.5 13 3//13.5 14 3//14.5 15 3//15.5 16 3//2 6 6//0 15 0//16//0 1 .1//1.5 2 .1//2.5 3 .1//3.5 4 .1//4.5 5 .1//5.5 6 .1//6.5 7 .1//7.5 8 .1//8.5 9 .1//9.5 10 .1//10.5 11 .1//11.5 12 .1//12.5 13 .1//13.5 14 .1//14.5 15 .1//15.5 16 .1//2 6 6//0 15 0//14//0 1 3//1.5 2 3//2.5 3 3//3.5 4 3//4.5 5 3//5.5 6 3//8.5 9 3//9.5 10 3//10.5 11 3//11.5 12 3//12.5 13 3//13.5 14 3//14.5 15 3//15.5 16 3//2 6 6//0 4000000000 0//2//1 2 1 //15 16 3//2 6 6//0 15 1//5//1 1.5 6//17 18 1 //3 5 3//0 20 10//0 20 0.5////答案://8.80//No View//8.80//6.70//No View//4.00//15.00//15.00//No View//No View//0.44//1.00//3999999970.00//8.00



 

0 0
原创粉丝点击