POJ 3525 半平面交

#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
struct Point
{
	double x, y;
	Point(double x = 0, double y = 0): x(x), y(y) {}
};
typedef Point Vector;
typedef vector<Point> Polygon;
Vector operator +(Vector A, Vector B)//
{
	return Vector(A.x + B.x, A.y + B.y);
}
Vector operator -(Point A, Point B)//
{
	return Vector(A.x - B.x , A.y - B.y);
}
Vector operator *(Vector A, double p)//
{
	return Vector(A.x * p, A.y * p);
}
double Dot(Vector A, Vector B)
{
	return A.x * B.x + A.y * B.y;
}
double Length(Vector A)
{
	return sqrt(Dot(A, A));
}
struct Line
{
	Point P;
	Vector v;
	double ang;
	Line() {};
	Line(Point P, Vector v): P(P), v(v) {ang = atan2(v.y, v.x);}
	bool operator < (const Line& L) const
	{
		return ang < L.ang;
	}
};
Point P[200], poly[200];
Line L[200];
Vector v[200], v2[200];
const double eps = 1e-10;
double Cross(Vector A, Vector B)//
{
	return A.x * B.y - A.y * B.x;
}
Vector Normal(Vector A)
{
	double L = Length(A);
	return Vector(-A.y / L, A.x / L);
}
bool OnLeft(Line L, Point p)
{
	return Cross(L.v, p - L.P) > 0;
}
Point GetIntersection(Line a, Line b)
{
	Vector u = a.P - b.P;
	double t = Cross(b.v, u) / Cross(a.v, b.v);
	return a.P + a.v * t;
}
int HalfplaneIntersection(Line *L, int n, Point *poly)
{
	sort(L, L + n);
	int first, last;
	Point *p = new Point[n];
	Line *q = new Line[n];
	q[first = last = 0] = L[0];
	for (int i = 1; i < n; i++)
	{
		while (first < last && !OnLeft(L[i], p[last - 1])) last--;
		while (first < last && !OnLeft(L[i], p[first])) first++;
		q[++last] = L[i];
		if (fabs(Cross(q[last].v, q[last - 1].v)) < eps)
		{
			last--;
			if (OnLeft(q[last], L[i].P)) q[last] = L[i];
		}
		if (first < last) p[last - 1] = GetIntersection(q[last - 1], q[last]);
	}
	while (first < last && !OnLeft(q[first], p[last - 1]))last--;
	if (last - first <= 1) return 0;
	p[last] = GetIntersection(q[last], q[first]);
	int m = 0;
	for (int i = first; i <= last; i++) poly[m++] = p[i];
	return m;
}
int main(int argc, char const *argv[])
{
	int n;
	while (scanf("%d", &n) == 1 && n)
	{
		int m, x, y;
		for (int i = 0; i < n; i++)
		{
			scanf("%d%d", &x, &y);
			P[i] = Point(x, y);
		}
		for (int i = 0; i < n; i++)
		{
			v[i] = P[(i + 1) % n] - P[i];
			v2[i] = Normal(v[i]);
		}
		double left = 0, right = 20000;
		while (right - left > 1e-10)
		{
			double mid = left + (right - left) / 2;
			for (int i = 0; i < n; i++)
				L[i] = Line(P[i] + v2[i] * mid, v[i]);
			m = HalfplaneIntersection(L, n, poly);
			if (!m) right = mid;
			else left = mid;
		}
		printf("%.6lf\n", left + eps);
	}
	return 0;
}



求多边形核到边的最短距离,妥妥的二分加半平面交。这道题其实和LA 3890是一样的。http://blog.csdn.net/Tczxw/article/details/50783070


另外,半平面交讲得好的链接:http://www.cnblogs.com/ka200812/archive/2012/01/20/2328316.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值