uva 10256 凸包

#include <bits/stdc++.h>
using namespace std;
int n, m;
double x, y;
const double eps = 1e-10;
int dcmp(double x)
{
	if (fabs(x) < eps) return 0;
	else return x < 0 ? -1 : 1;
}
struct Point
{
	double x, y;
	Point(double x = 0, double y = 0): x(x), y(y) {}
};
typedef Point Vector;
Vector operator-(Vector A, Vector B)
{
	return Vector(A.x - B.x, A.y - B.y);
}
Vector operator+(Vector A, Vector B)
{
	return Vector(A.x + B.x, A.y + B.y);
}
bool operator == (const Point&a, const Point &b)
{
	return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}
double Dot(Vector A, Vector B)
{
	return A.x * B.x + A.y * B.y;
}
double Cross(Vector A, Vector B)
{
	return A.x * B.y - A.y * B.x;
}
bool operator<(const Point&a, const Point &b)
{
	return a.x < b.x || (a.x == b.x && a.y < b.y);
}
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2)
{
	double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1);
	double c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
	return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
}
bool OnSegment(Point p, Point a1, Point a2)
{
	return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0;
}
typedef vector<Point> Polygon;
int isPointInPolygon(Point p, Polygon poly)
{
	int wn = 0;
	int n = poly.size();
	for (int i = 0; i < n; i++)
	{
		if ( poly[i] == p || poly[(i + 1) % n] == p || OnSegment(p,  poly[i], poly[(i + 1) % n])) return -1;
		int k = dcmp(Cross(poly[(i + 1) % n] - poly[i], p - poly[i]));
		int d1 = dcmp(poly[i].y - p.y);
		int d2 = dcmp(poly[(i + 1) % n].y - p.y);
		if (k > 0 && d1 <= 0 && d2 > 0) wn++;
		if (k < 0 && d2 <= 0 && d1 > 0) wn--;
	}
	if (wn != 0) return 1;
	return 0;
}
vector<Point>ConvexHull(vector<Point>p)
{
	sort(p.begin(), p.end());
	p.erase(unique(p.begin(), p.end()), p.end());
	int n = p.size();
	int m = 0;
	vector<Point>ch(n + 1);
	for (int i = 0; i < n; i++)
	{
		while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--;
		ch[m++] = p[i];
	}
	int k = m;
	for (int i = n - 2; i >= 0; i--)
	{
		while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--;
		ch[m++] = p[i];
	}
	if (n > 1) m--;
	ch.resize(m);
	return ch;
}
bool ConvexPolygonDisjoint(const Polygon p1, const Polygon p2)
{
	int c1 = p1.size();
	int c2 = p2.size();
	for (int i = 0; i < c1; i++)
		if (isPointInPolygon(p1[i], p2)) return false;
	for (int i = 0; i < c2; i++)
		if (isPointInPolygon(p2[i], p1)) return false;
	for (int i = 0; i < c1; i++)
		for (int j = 0; j < c2; j++)
			if (SegmentProperIntersection(p1[i], p1[(i + 1) % c1], p2[j], p2[(j + 1) % c2]))  return false;
	return true;
}
int main(int argc, char const *argv[])
{
	while (scanf("%d%d", &n, &m) != EOF)
	{
		if (n == 0 && m == 0) break;
		std::vector<Point> p1, p2;
		for (int i = 0; i < n; i++)
		{
			scanf("%lf%lf", &x, &y);
			p1.push_back(Point(x, y));
		}
		for (int i = 0; i < m; i++)
		{
			scanf("%lf%lf", &x, &y);
			p2.push_back(Point(x, y));
		}
		printf("%s\n", (ConvexPolygonDisjoint(ConvexHull(p1), ConvexHull(p2)) ? "Yes" : "No"));
	}
	return 0;
}


求出两凸包,暴力判断是否相交,

退化为点或线段的情况一样处理,只要碰到另一个就是NO。

你可能感兴趣的:(uva 10256 凸包)