POJ 2074 Line of Sight (计算几何--视线遮挡问题)

题目链接:http://poj.org/problem?id=2074

题意:(这道题本身不难,,但是我一开始题意读错,就一直脑壳疼....)题目先给了两条线段,L1表示房子,L2表示道路,然后给出n条线段表示障碍物(所有线段均平行x轴),然后问道路上最大的能看到整个房子的连续道路区间

思路:1.只考虑y坐标在L1和L2之间的障碍物

           2.求出每个障碍物遮挡的道路盲区区间

           3.把所有区间排序,求最大未被覆盖的区间长度即可

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;

typedef long long ll;
const int maxn = 1e5 + 5;
const double eps = 1e-8;


//点或向量
typedef struct Point
{
	double x, y;
	Point(double a = 0, double b = 0)
	{
		x = a, y = b;
	}
	const Point operator+(const Point &p)const
	{
		return Point(x + p.x, y + p.y);
	}
	const Point operator-(const Point &p)const
	{
		return Point(x - p.x, y - p.y);
	}
	const Point operator/(double a)const
	{
		return Point(x / a, y / a);
	}
	//点乘
	const double operator*(const Point &p)const
	{
		return x * p.x + y * p.y;
	}
	//×乘
	const double operator^(const Point &p)const
	{
		return x * p.y - y * p.x;
	}
	//长度
	const double len()
	{
		return sqrt(x*x + y * y);
	}
}Point;

//线段
typedef struct Line
{
	Point a, b;
}Line;

//精度比较
int cmp(double x, double y)
{
	if (fabs(x - y) < eps)return 0;
	if (x - y < 0)return -1;
	return 1;
}

//向量夹角[0,pi]
double angle(Point x, Point y)
{
	return acos((x*y) / (x.len()) / y.len());
}

//判断点是否在线段上
bool onsegement(Point p, Line s)
{
	return cmp((s.a - p) ^ (s.b - p), 0) == 0 && cmp((s.a - p)*(s.b - p), 0) <= 0;
}

//判断两线段是否相交
bool inter(Line x, Line y)
{
	double c1 = (x.b - x.a) ^ (y.a - x.a), c2 = (x.b - x.a) ^ (y.b - x.a),
		c3 = (y.b - y.a) ^ (x.a - y.a), c4 = (y.b - y.a) ^ (x.b - y.a);
	if (onsegement(x.a, y) || onsegement(x.b, y) || onsegement(y.a, x) || onsegement(y.b, x))return 1;
	return cmp(c1, 0)*cmp(c2, 0) < 0 && cmp(c3, 0)*cmp(c4, 0) < 0;
}

//求两直线交点
Point getlineinter(Point x1, Point x2, Point y1, Point y2)
{
	x2 = x2 - x1;
	x2 = x2 / x2.len();
	y2 = y2 - y1;
	y2 = y2 / y2.len();
	Point u = x1 - y1;
	double t = (y2^u) / (x2^y2);
	return x1 + Point(x2.x*t, x2.y*t);
}



Line house, proper;
Line obs;
int n;
pair<double, double> p[maxn];

int main()
{
	double x1, x2, y;
	while (scanf("%lf%lf%lf", &x1, &x2, &y))
	{
		if (cmp(x1, 0) == 0 && cmp(x2, 0) == 0 && cmp(y, 0) == 0)
		{
			break;
		}
		Point st, ed;
		st.x = x1, ed.x = x2;
		st.y = ed.y = y;
		house.a = st, house.b = ed;
		scanf("%lf%lf%lf", &x1, &x2, &y);
		st.x = x1, ed.x = x2;
		st.y = ed.y = y;
		proper.a = st, proper.b = ed;
		scanf("%d", &n);
		int cnt = 0;
		for (int i = 0; i < n; i++)
		{
			scanf("%lf%lf%lf", &x1, &x2, &y);
			if (cmp(y , house.a.y)>=0 || cmp(y , proper.a.y)<=0)continue;
			st.x = x1, ed.x = x2;
			st.y = ed.y = y;
			obs.a = st, obs.b = ed;
			Point L, R;
			L = getlineinter(obs.a, house.b, proper.a, proper.b);
			R = getlineinter(obs.b, house.a, proper.a, proper.b);
			if (cmp(L.x, proper.b.x) >= 0 || cmp(R.x, proper.a.x) <= 0)continue;
			L.x = max(L.x, proper.a.x), R.x = min(R.x, proper.b.x);
			p[cnt++] = pair<double, double>(L.x, R.x);
		}
		double ans = 0;
		p[cnt++] = pair<double, double>(proper.b.x, proper.b.x);
		sort(p, p + cnt);
		double pre = proper.a.x;
		for (int i = 0; i < cnt; i++)
		{
			if (cmp(p[i].first , pre)>0)
			{
				ans = max(ans, p[i].first - pre);
			}
			pre = max(p[i].second, pre);
		}
		if (cmp(ans, 0) == 0)
			printf("No View\n");
		else
		    printf("%.2lf\n", ans);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值