半平面交uvaliva3890

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
struct pointt
{
	double x, y;
	pointt(double x, double y) :x(x), y(y)
	{}
	pointt()
	{}
};
pointt operator+(pointt a, pointt b)
{
	return pointt(a.x + b.x, b.y + a.y);
}
pointt operator-(pointt a, pointt b)
{
	return pointt(a.x - b.x, a.y - b.y);
}
pointt operator*(pointt a, double t)
{
	return pointt(a.x*t, a.y*t);
}
double operator*(pointt a, pointt b)
{
	return a.x*b.x + a.y*b.y;
}
double dot(pointt a, pointt b)
{
	return a.x*b.y - a.y*b.x;
}
double length(pointt a)
{
	return sqrt(a*a);
}
struct linee
{
	pointt p1, vec,verti;
	double arc;
	linee(pointt a, pointt b) :p1(a), vec(b - a)
	{
		arc = atan2(vec.y, vec.x);
		double l = length(vec);
		verti.x = -vec.y / l; verti.y = vec.x / l;
	}
	linee()
	{}
	bool operator<(linee b)
	{
		return arc < b.arc;
	}
};
bool isonleft(pointt a, linee b)
{
	pointt c = a - b.p1;
	return dot(c, b.vec) <= 0;
}
pointt inter(linee a, linee b)
{
	pointt c = a.p1 - b.p1;
	if (fabs(dot(b.vec, a.vec)) < 1e-10)return pointt(1000000000, 1000000000);
	double t = dot(c, b.vec) / dot(b.vec, a.vec);
	return a.p1 + a.vec*t;
}
linee realline[2000];
linee line[2000];
pointt point[2000];
int halfmat(linee *l, int n)
{
	sort(l, l + n);
	int first = 0;int  last = 0;
	realline[first] = l[0];
	for (int i=1; i < n; i++)
	{
		while ((last - first)>0 && !isonleft(point[last - 1], l[i]))last--;
		while ((last - first) > 0 && !isonleft(point[first], l[i]))first++;
		if (fabs(dot(l[i].vec, realline[last].vec)) < 1e-10)continue;
		realline[++last] = l[i];
		if (last - first > 0)point[last - 1] = inter(realline[last], realline[last - 1]);
	}
	while ((last - first) > 0 && !isonleft(point[last - 1], realline[first]))last--;
	//if ((last - first) > 0)point[last] = inter(l[first], l[last]);
	if ((last - first)<=1)
		return 0;
	else
		return 1;
}
pointt temp[2000];
int main()
{
	int n;
	while (scanf("%d", &n) && n != 0)
	{
		for (int i = 0; i < n; i++)
		{
			scanf("%lf%lf", &temp[i].x, &temp[i].y);
			if (i>0)
				line[i-1] = linee(temp[i - 1], temp[i]);
		}
		line[n - 1] = linee(temp[n - 1], temp[0]);
		double left = 0; double right = 20000; double mid;
		linee templine[2000];
		while ((right - left) > 1e-10)
		{
			//cout << right << endl;
			 mid = (right + left) / 2;
			for (int i = 0; i < n; i++)
			{
				templine[i] = line[i];
				templine[i].p1 = templine[i].p1 + templine[i].verti*mid;
			}
			int kind = halfmat(templine, n);
			if (kind)
				left = mid;
			else
				right = mid;
		}
		printf("%.6lf\n", right);
	}
}
终于写过一次了2333333!!以前都wa的啊QAQ半平面交的数据真的是不好构造啊!!!!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值