判断由红点和蓝点分别组成的两个凸包是否相离,是输出Yes,否输出No。
训练指南上的分析:
1.任取红凸包上的一条线段和蓝凸包上的一条线段,判断二者是否相交。如果相交(不一定是规范相交,有公共点就算相交),则无解
2.任取一个红点,判断是否在蓝凸包内。如果是,则无解。蓝点红凸包同理。
其中任何一个凸包退化成点或者线段时需要特判。
其实只需要把上面两个判断顺序颠倒一下,就可以不需要特判。
先判断点是否在凸包内,因为这个考虑了点在凸包边界上的情况,所以后面判凸包线段是否相交时直接用规范相交判断即可。
此时特殊情况包含在了上两种情况中,因此不需要特判。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <set>
#include <list>
#include <queue>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF 0x3f3f3f3f
const int N = 100005;
const double eps = 1e-10;
struct Point
{
double x,y;
Point(double x = 0,double y = 0):x(x),y(y){}
Point operator +(Point a)
{
return Point(x + a.x,y + a.y);
}
Point operator -(Point a)
{
return Point(x - a.x,y-a.y);
}
Point operator *(double p)
{
return Point(x * p,y * p);
}
Point operator /(double p)
{
return Point(x / p,y / p);
}
}P1[510],P2[510],ch1[510],ch2[510];
int dcmp(double x)
{
if(fabs(x) < eps)
return 0;
else
return x < 0 ? -1 : 1;
}
bool operator == (const Point &a,const Point &b)
{
return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
}
bool operator <(const Point &a,const Point &b)
{
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
double Dot(Point A,Point B)
{
return A.x * B.x + A.y * B.y;
}
double Length(Point A)
{
return sqrt(Dot(A,A));
}
double Angle(Point A,Point B)
{
return acos(Dot(A,B) / Length(A) / Length(B));
}
double Cross(Point A,Point B)
{
return A.x*B.y - A.y*B.x;
}
double torad(double deg)
{
return deg / 180 * acos(-1);
}
Point Rotate(Point A,double rad)
{
return Point(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
int isPointOnSegment(Point p,Point a1,Point a2)
{
return dcmp(Cross(a1-p,a2-p) == 0 && dcmp(Dot(a1-p,a2-p)) < 0);
}
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(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;
}
Point get(Point A,Point B,Point C)
{
Point v1 = C-B;
double a1 = Angle(A-B,v1);
//printf("%.6lf\n",a1);
v1 = Rotate(v1,a1/3);
//printf("%.6lf %.6lf\n",v1.x,v1.y);
Point v2 = B-C;
double a2 = Angle(A - C,v2);
//printf("%.6lf\n",a2);
v2 = Rotate(v2,-a2/3);
//printf("%.6lf %.6lf\n",v2.x,v2.y);
return GetLineIntersection(B,v1,C,v2);
}
double DistanceToSegment(Point P,Point A,Point B)
{
if(A == B)
return Length(P-A);
Point v1 = B - A;
Point v2 = P - A;
Point v3 = P - B;
if(dcmp(Dot(v1,v2)) < 0)
return Length(v2);
else if(dcmp(Dot(v1,v3)) > 0)
return Length(v3);
else
return fabs(Cross(v1,v2)) / Length(v1);
}
int ConvexHull(Point *p,int n,Point *ch)
{
sort(p,p+n);
int m = 0;
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--;
return m;
}
double PolygonArea(Point *p,int n)
{
double area = 0;
for(int i = 1; i < n-1; i++)
area += Cross(p[i]-p[0],p[i+1]-p[0]);
return area / 2;
}
int isPointInPolygon(Point p,Point *po,int n)
{
int wn = 0;
for(int i = 0; i < n; i++)
{
if(isPointOnSegment(p,po[i],po[(i+1)%n]))
return -1;
int k = dcmp(Cross(po[(i+1)%n]-po[i],p-po[i]));
int d1 = dcmp(po[i].y - p.y);
int d2 = dcmp(po[(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;
}
int checkConvexHullIntersection(Point *p,Point *q,int np,int nq)
{
for(int i = 0; i < np; i++)
if(isPointInPolygon(p[i],q,nq))
return 1;
for(int i = 0; i < nq; i++)
if(isPointInPolygon(q[i],p,np))
return 1;
for(int i = 0; i < np; i++)
for(int j = 0; j < nq; j++)
if(SegmentProperIntersection(p[i],p[(i+1)%np],q[j],q[(j+1)%nq]))
return 1;
return 0;
}
int main()
{
int t,c = 1,n,m;
//scanf("%d",&t);
double x,y;
while(scanf("%d%d",&n,&m) && (n+m))
{
for(int i = 0; i < n; i++)
{
scanf("%lf %lf",&x,&y);
P1[i] = (Point){x,y};
}
for(int i = 0; i < m; i++)
{
scanf("%lf %lf",&x,&y);
P2[i] = (Point){x,y};
}
int k1 = ConvexHull(P1,n,ch1);
int k2 = ConvexHull(P2,m,ch2);
if(checkConvexHullIntersection(ch1,ch2,k1,k2))
printf("No\n");
else
printf("Yes\n");
}
return 0;
}