uvalive 3218ボリュームラップアルゴリズム

3787 ワード

#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double eps = 1e-9;
struct Point
{
	double x, y;
	Point(double x = 0, double y = 0): x(x), y(y) {}
};
typedef Point Vector;
double dcmp(double x)
{
	if (fabs(x) < eps)
		return 0;
	return x < 0 ? -1 : 1;
}
Vector operator + (const Point& A, const Point& B)
{
	return Vector(A.x + B.x, A.y + B.y);
}
Vector operator - (const Point& A, const Point& B)
{
	return Vector(A.x - B.x, A.y - B.y);
}
Vector operator * (const Point& A, double a)
{
	return Vector(A.x * a, A.y * a);
}
Vector operator / (const Point& A, double a)
{
	return Vector(A.x / a, A.y / a);
}
double Cross(const Vector& A, const Vector& B)
{
	return A.x * B.y - A.y * B.x;
}
double Dot(const Vector& A, const Vector& B)
{
	return A.x * B.x + A.y * B.y;
}
double Length(const Vector& A)
{
	return sqrt(Dot(A, A));
}
bool operator < (const Point& A, const Point& B)
{
	return A.x < B.x || (A.x == B.x && A.y < B.y);
}
bool operator == (const Point& A, const Point& B)
{
	return A.x == B.x && A.y == B.y;
}
Point GetLineIntersection(Point P, Point v, Point Q, Point w)
{
	Point u = P - Q;
	double t = Cross(w, u) / Cross(v, w);
	return P + v * t;
}
bool SegmentProperIntersection(const Point& a1, const Point& a2, const Point& b1, const Point& b2)
{
	double c1 = Cross(a2 - a1, b1 - a1);
	double c2 = Cross(a2 - a1, b2 - a1);
	double c3 = Cross(b2 - b1, a1 - b1);
	double c4 = Cross(b2 - b1, a2 - b1);
	return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
}
bool OnSegment(const Point& p, const Point& a1, const Point& a2)
{
	return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0;
}

const int N = 10005;
const int M = 205;
Point P[M], res[N], temp[M];
double ang[M], s;
int n, cnt;

void add(Point a, Point b)
{
	temp[cnt] = a;
	ang[cnt] = atan2(temp[cnt].y - b.y, temp[cnt].x - b.x) - s;
	while (dcmp(ang[cnt]) <= 0)
		ang[cnt] += 2 * PI;
	cnt++;
}

int main()
{
	while (scanf("%d", &n) == 1)
	{
		int minid = 0;
		for (int i = 0; i < n; i++)
		{
			scanf("%lf%lf", &P[i].x, &P[i].y);
			if (P[i] < P[minid])
				minid = i;
		}
		res[0] = P[minid];
		int num = 1;
		s = -PI;
		while (1)
		{
			cnt = 0;
			for (int i = 0; i < n; i++)
			{
				if (res[num - 1] == P[i])
				{
					add(P[(i + 1) % n], res[num - 1]);
					add(P[(i + n - 1) % n], res[num - 1]);
					break;
				}
			}
			for (int i = 0; i < n; i++)
			{
				if (OnSegment(res[num - 1], P[i], P[(i + 1) % n]))
				{
					add(P[(i + 1) % n], res[num - 1]);
					add(P[i], res[num - 1]);
				}
			}
			int id = 0;
			for (int i = 0; i < cnt; i++)
				if (ang[i] < ang[id])
					id = i;
			double minlen = 1e9;
			Point RP = temp[id], its;
			for (int i = 0; i < n; i++)
			{
				if (SegmentProperIntersection(temp[id], res[num - 1], P[i], P[(i + 1) % n]))
				{
					its = GetLineIntersection(temp[id], temp[id] - res[num - 1], P[i], P[i] - P[(i + 1) % n]);
					if (Length(its - res[num - 1]) < minlen)
					{
						minlen = Length(its - res[num - 1]);
						RP = its;
					}
				}
			}
			res[num] = RP;
			s = atan2(res[num - 1].y - res[num].y, res[num - 1].x - res[num].x);
			num++;
			if (res[num - 1] == res[0])
				break;
		}
		printf("%d
", num - 1); for (int i = 0; i < num - 1; i++) printf("%.4lf %.4lf
", res[i].x, res[i].y); } return 0; }

転載元:http://blog.csdn.net/hyczms/article/details/47155427
まず1つの起点、すなわち最も左下の角の点を選んで、更に第1の出辺、すなわち角度の最小点の辺を選んで、歩き始めて、その辺と交差するすべての点を見つけて、しかしその辺の起点と同じように交点ではありませんて、最も近い1つを見つけて、もし複数があれば、外に出る方向を見つけて最も右の1つに曲がって、絶えず探して、最初の起点に戻るまで.