POJ 2074 线段相交 视线问题

0
0
0
1. 云栖社区>
2. 博客>
3. 正文

## POJ 2074 线段相交 视线问题

prime7 2013-04-15 15:43:00 浏览618

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef double PointType;
struct point
{
PointType x,y;
};
struct line
{
point l,r;
};
int ansnum,n,num;
void getcoor(line &ans,double x1,double x2,double y)  //构建点坐标
{
ans.l.x=x1,ans.r.x=x2;
ans.l.y=ans.r.y=y;
}
point getpoint(point a,point b)   //求在路上的交点
{
point wans;
double k,b1;
if(a.x==b.x)
wans.x=a.x;
else
k=(a.y-b.y)/(a.x-b.x),
b1=a.y-k*a.x,
return wans;
}
void getans(line a)  //将路上不可视区域转化成线段
{
ans[ansnum].l=getpoint(a.l,house.r);
ans[ansnum].r=getpoint(a.r,house.l);
ansnum++;
}
int cmp(line a,line b)
{
return a.l.x<b.l.x;
}
int main()
{
double x1,x2,y;
while(~scanf("%lf%lf%lf",&x1,&x2,&y),(x1||x2||y))
{
num=ansnum=0;
getcoor(house,x1,x2,y);
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%lf%lf%lf",&x1,&x2,&y);
getcoor(data[num++],x1,x2,y),getans(data[num-1]);
}
if(n==0)
{
continue;
}
sort(ans,ans+ansnum,cmp);
double ansmax=0,bj=ans[0].r.x;
for(int i=0; i<ansnum; i++)
{
if(ans[i].l.x>bj)
ansmax=max(ansmax,ans[i].l.x-bj);
bj=max(ans[i].r.x,bj);
}
if(ansmax==0)
puts("No View");
else
printf("%.2f\n",ansmax);
}
return 0;
}

prime7
+ 关注