uva 10256(凸包)

题意:给出n个红点和m个蓝点的坐标,是否存在一条直线,使得取一个红点和一个蓝点都是在直线的异侧。
题解:明显是要判断两个凸包是否有交集,要判断两个凸包的边是否相交,还要判断红点是否在蓝点凸包内部,蓝点是否在红点凸包内部。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double PI = acos(-1);
const int N = 505;
const double INF = 1e9;
struct Point {
    double x, y;
    Point(double x = 0, double y = 0): x(x), y(y) {}
}Red[N], Blue[N], resr[N], resb[N];
int n, m;
double Sqr(double x) {
    return x * x;
}
Point operator + (Point A, Point B) {
    return Point(A.x + B.x, A.y + B.y);
}
Point operator - (Point A, Point B) {
    return Point(A.x - B.x, A.y - B.y);
}
Point operator * (Point A, double p) {
    return Point(A.x * p, A.y * p);
}
Point operator / (Point A, double p) {
    return Point(A.x / p, A.y / p);
}
//计算点积的正负  负值夹角为钝角
int dcmp(double x) {
    if (fabs(x) < 1e-9)
        return 0;
    return x < 0 ? -1 : 1;
}
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 dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}
//计算点积
double Dot(Point A, Point B) {
    return A.x * B.x + A.y * B.y;
}
//计算叉积,也就是数量积
double Cross(Point A, Point B) {
    return A.x * B.y - A.y * B.x;
}
//计算向量长度
double Length(Point A) {
    return sqrt(Dot(A, A));
}
//向量A旋转rad弧度,rad负值为顺时针旋转
Point Rotate(Point A, double rad) {
    return Point(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
}
//角度转化弧度
double torad(double deg) {
    return deg / 180.0 * PI;
}
//判断两个线段是否有交点(不包括端点)
bool SegmentProperIntersection(Point a1, Point a2, Point b1, 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;
}
//判断点p是否在线段a1--a2上(不包括端点)
bool OnSegment(Point p, Point a1, Point a2) {
    return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0;
}

int ConvexHull(Point* P, int cnt, Point* res) {
    sort(P, P + cnt);
    cnt = unique(P, P + cnt) - P;
    int m = 0;
    for (int i = 0; i < cnt; i++) {
        while (m > 1 && Cross(res[m - 1] - res[m - 2], P[i] - res[m - 2]) <= 0)
            m--;
        res[m++] = P[i];
    }
    int k = m;
    for (int i = cnt - 2; i >= 0; i--) {
        while (m > k && Cross(res[m - 1] - res[m - 2], P[i] - res[m - 2]) <= 0)
            m--;
        res[m++] = P[i];
    }
    if (cnt > 1)
        m--;
    return m;
}

bool isPointInPolygon(Point p, Point* res, int cnt) {
    int wn = 0;
    for (int i = 0; i < cnt; i++) {
        if (res[i] == p || res[(i + 1) % cnt] == p || OnSegment(p, res[i], res[(i + 1) % cnt]))
            return 1;
        int k = dcmp(Cross(res[(i + 1) % cnt] - res[i], p - res[i]));
        int d1 = dcmp(res[i].y - p.y);
        int d2 = dcmp(res[(i + 1) % cnt].y - p.y);
        if (k > 0 && d1 <= 0 && d2 > 0)
            wn++;
        if (k < 0 && d2 <= 0 && d1 > 0)
            wn--;
    }
    if (wn)
        return 1;
    return 0;
}

int main() {
    while (scanf("%d%d", &n, &m) == 2 && n + m) {
        int flag = 1;
        for (int i = 0; i < n; i++)
            scanf("%lf%lf", &Red[i].x, &Red[i].y);
        for (int i = 0; i < m; i++)
            scanf("%lf%lf", &Blue[i].x, &Blue[i].y);
        int cnt1 = ConvexHull(Red, n, resr);
        int cnt2 = ConvexHull(Blue, m, resb);
        for (int i = 0; i < cnt1; i++)
            if (isPointInPolygon(resr[i], resb, cnt2)) {
                flag = 0;
                break;
            }
        if (flag) {
            for (int i = 0; i < cnt2; i++)
                if (isPointInPolygon(resb[i], resr, cnt1)) {
                    flag = 0;
                    break;
                }
                if (flag) {
                    for (int i = 0; i < cnt1; i++) {
                        for (int j = 0; j < cnt2; j++) {
                        if (SegmentProperIntersection(resr[i], resr[(i + 1) % cnt1], resb[j], resb[(j + 1) % cnt2])) {
                            flag = 0;
                            break;
                        }
                    }
                    if (!flag)
                        break;
                    }
                }
        }       
        if (flag)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值