UVa 10256 - The Great Divide 判断凸包相交

时间:2023-03-08 21:55:32
UVa 10256 - The Great Divide 判断凸包相交

模板敲错了于是WA了好几遍……

判断由红点和蓝点分别组成的两个凸包是否相离,是输出Yes,否输出No。

训练指南上的分析:

1.任取红凸包上的一条线段和蓝凸包上的一条线段,判断二者是否相交。如果相交(不一定是规范相交,有公共点就算相交),则无解

2.任取一个红点,判断是否在蓝凸包内。如果是,则无解。蓝点红凸包同理。

其中任何一个凸包退化成点或者线段时需要特判。

其实只需要把上面两个判断顺序颠倒一下,就可以不需要特判。

先判断点是否在凸包内,因为这个考虑了点在凸包边界上的情况,所以后面判凸包线段是否相交时直接用规范相交判断即可。

此时特殊情况包含在了上两种情况中,因此不需要特判。

 #include <cstdio>
#include <cmath>
#include <algorithm> using namespace std; const int MAXN = ; const double eps = 1e-; struct Point
{
double x, y;
Point( double x = , double y = ):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 );
} Vector operator*( Vector A, double p ) //向量数乘
{
return Vector( A.x * p, A.y * p );
} Vector operator/( Vector A, double p ) //向量数除
{
return Vector( A.x / p, A.y / p );
} bool operator<( const Point& A, const Point& B ) //两点比较
{
return A.x < B.x || ( A.x == B.x && A.y < B.y );
} int dcmp( double x ) //控制精度
{
if ( fabs(x) < eps ) return ;
else return x < ? - : ;
} bool operator==( const Point& a, const Point& b ) //两点相等
{
return dcmp( a.x - b.x ) == && dcmp( a.y - b.y ) == ;
} double Dot( Vector A, Vector B ) //向量点乘
{
return A.x * B.x + A.y * B.y;
} double Length( Vector A ) //向量模
{
return sqrt( Dot( A, A ) );
} double Angle( Vector A, Vector B ) //向量夹角
{
return acos( Dot(A, B) / Length(A) / Length(B) );
} double Cross( Vector A, Vector B ) //向量叉积
{
return A.x * B.y - A.y * B.x;
} double Area2( Point A, Point B, Point C ) //向量有向面积
{
return Cross( B - A, C - A );
} Vector Rotate( Vector A, double rad ) //向量旋转
{
return Vector( A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad) );
} Vector Normal( Vector A ) //向量单位法向量
{
double L = Length(A);
return Vector( -A.y / L, A.x / L );
} Point GetLineIntersection( Point P, Vector v, Point Q, Vector w ) //两直线交点
{
Vector u = P - Q;
double t = Cross( w, u ) / Cross( v, w );
return P + v * t;
} double DistanceToLine( Point P, Point A, Point B ) //点到直线的距离
{
Vector v1 = B - A, v2 = P - A;
return fabs( Cross( v1, v2 ) ) / Length(v1);
} double DistanceToSegment( Point P, Point A, Point B ) //点到线段的距离
{
if ( A == B ) return Length( P - A );
Vector v1 = B - A, v2 = P - A, v3 = P - B;
if ( dcmp( Dot(v1, v2) ) < ) return Length(v2);
else if ( dcmp( Dot(v1, v3) ) > ) return Length(v3);
else return fabs( Cross( v1, v2 ) ) / Length(v1);
} Point GetLineProjection( Point P, Point A, Point B ) // 点在直线上的投影
{
Vector v = B - A;
return A + v*( Dot(v, P - A) / Dot( v, v ) );
} bool SegmentProperIntersection( Point a1, Point a2, Point b1, Point b2 ) //线段相交,交点不在端点
{
double c1 = Cross( a2 - a1, b1 - a1 ), c2 = Cross( a2 - a1, b2 - a1 ),
c3 = Cross( b2 - b1, a1 - b1 ), c4 = Cross( b2 - b1, a2 - b1 );
return dcmp(c1)*dcmp(c2) < && dcmp(c3) * dcmp(c4) < ;
} bool OnSegment( Point p, Point a1, Point a2 ) //点在线段上,不包含端点
{
return dcmp( Cross(a1 - p, a2 - p) ) == && dcmp( Dot( a1 - p, a2 - p ) ) < ;
} double toRad( double deg ) //角度转弧度
{
return deg / 180.0 * acos( -1.0 );
} int ConvexHull( Point *p, int n, Point *ch ) //求凸包
{
sort( p, p + n );
n = unique( p, p + n ) - p;
int m = ;
for ( int i = ; i < n; ++i )
{
while ( m > && Cross( ch[m - ] - ch[m - ], p[i] - ch[m - ] ) <= ) --m;
ch[m++] = p[i];
} int k = m;
for ( int i = n - ; i >= ; --i )
{
while ( m > k && Cross( ch[m - ] - ch[m - ], p[i] - ch[m - ] ) <= ) --m;
ch[m++] = p[i];
} if ( n > ) --m;
return m;
} int isPointInPolygon( Point p, Point *poly, int n ) //判断一点是否在凸包内
{
int wn = ; for ( int i = ; i < n; ++i )
{
Point& p1 = poly[i], p2 = poly[ (i + )%n ];
if ( p == p1 || p == p2 || OnSegment( p, p1, p2 ) ) return -; //在边界上
int k = dcmp( Cross( p2 - p1, p - p1 ) );
int d1 = dcmp( p1.y - p.y );
int d2 = dcmp( p2.y - p.y );
if ( k > && d1 <= && d2 > ) ++wn;
if ( k < && d2 <= && d1 > ) --wn;
} if ( wn ) return ; //内部
return ; //外部
} double PolygonArea( Point *p, int n ) //多边形有向面积
{
double area = ;
for ( int i = ; i < n - ; ++i )
area += Cross( p[i] - p[], p[i + ] - p[] );
return area / 2.0;
} bool checkConvexHullIntersection( Point *a, Point *b, int na, int nb )
{
for ( int i = ; i < na; ++i )
if ( isPointInPolygon( a[i], b, nb ) ) return true; for ( int i = ; i < nb; ++i )
if ( isPointInPolygon( b[i], a, na ) ) return true; for ( int i = ; i < na; ++i )
for ( int j = ; j < nb; ++j )
if ( SegmentProperIntersection(a[i], a[ (i + ) % na ], b[j], b[ (j + ) % nb ] ) ) return true; return false;
} Point M[MAXN], chM[MAXN];
Point C[MAXN], chC[MAXN]; int main()
{
int Mn, Cn;
while ( scanf( "%d%d", &Mn, &Cn ), Mn || Cn )
{
for ( int i = ; i < Mn; ++i )
scanf( "%lf%lf", &M[i].x, &M[i].y ); for ( int i = ; i < Cn; ++i )
scanf( "%lf%lf", &C[i].x, &C[i].y ); int Mcnt = ConvexHull( M, Mn, chM );
int Ccnt = ConvexHull( C, Cn, chC ); if ( checkConvexHullIntersection( chM, chC, Mcnt, Ccnt ) ) puts("No");
else puts("Yes");
}
return ;
}