UVa10256

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1197

找判断凸包时候相离,书上说要判断线段是否相交,并且判断一个凸包内的点是否在另外一个凸包内。感觉如果把端点相交也计算在内的话,只要判断是否相交就好,但是总是却没有过,只好加上点在凸包内了。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

const double eps = 1e-10;

struct Point
{
    double x, y;

    Point() {}
    Point(double x, double y):x(x), y(y) {}

    bool operator < (const Point& a) const
    {
        if (x == a.x)
            return y < a.y;
        return x < a.x;
    }

    bool operator == (const Point& a) const
    {
        return x == a.x && y == a.y;
    }
};
typedef Point Vector;

Point P[510], convexR[510], convexB[510];

Vector operator - (Point A, Point B)
{
    return Vector(A.x-B.x, A.y-B.y);
}

double Cross(Vector A, Vector B)
{
    return A.x*B.y - A.y*B.x;
}

double Dot(Vector A, Vector B) //向量的点成
{
    return A.x*B.x + A.y*B.y;
}

int dcmp(double x)  //x与0比较大小(考虑到精度问题)
{
    if (fabs(x) < eps)
        return 0;
    else
        return x < 0 ? -1 : 1;
}

bool OnSegment(Point p, Point a1, Point a2)
{
    if (p == a1 || p == a2)
        return true;
    return dcmp(Cross(a1-p, a2-p)) == 0 && dcmp(Dot(a1-p, a2-p)) < 0;
}

bool SegmentProperIntersetion(Point a1, Point a2, Point b1,Point b2)
{
    if (OnSegment(a1,b1,b2)|| OnSegment(a2,b1,b2)|| OnSegment(b1,a1,a2)|| OnSegment(b2,a1,a2))
        return true;

    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) < 0 && dcmp(c3)*dcmp(c4) < 0;
}

int ConvexHull(Point *p, int n, Point *ch)
{
    sort(p, p+n);
    n = unique(p, p+n) - p;

    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];
    }

    return m;
}

bool isPointInPolygon(Point p, Point *poly, int n)
{
    int wn = 0;
    for (int i=0; i<n-1; i++)
    {
        if (OnSegment(p,poly[i],poly[i+1])) return true;
        int k = dcmp(Cross(poly[i+1]-poly[i], p-poly[i]));
        int d1 = dcmp(poly[i].y-p.y);
        int d2 = dcmp(poly[i+1].y-p.y);
        if (k > 0 && d1 <= 0 && d2 > 0) wn++;
        if (k < 0 && d2 <= 0 && d1 > 0) wn--;
    }
    if (wn != 0) return true;
    return false;
}

bool check(Point *con1, int n, Point *con2, int m)
{
    for (int i=1; i<n; i++)
        for (int j=1; j<m; j++)
            if (SegmentProperIntersetion(con1[i-1],con1[i],con2[j-1],con2[j]))
                return false;

    for (int i=0; i<n; i++)
        if (isPointInPolygon(con1[i], con2, m))
            return false;

    for (int i=0; i<m; i++)
        if (isPointInPolygon(con2[i], con1, n))
            return false;

    return true;
}

int main()
{
    int n, m;

    while (scanf("%d%d",&n, &m))
    {
        if (n == 0 && m == 0)
            break;

        for (int i=0; i<n; i++)
            scanf("%lf%lf",&P[i].x, &P[i].y);
        int cntR = ConvexHull(P,n,convexR);

        for (int i=0; i<m; i++)
            scanf("%lf%lf",&P[i].x, &P[i].y);
        int cntB = ConvexHull(P,m,convexB);

        if (check(convexR, cntR, convexB, cntB))
            printf("Yes\n");
        else
            printf("No\n");
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值