Segments POJ - 3304

判读线段与直线相交

#include <cmath>
#include <cstdio>
using namespace std;
/*
投影在一条线上,有交点,等价于有一条线可以穿过所有的线段
读入所有线段,然后选取坐标原点以及一条线段,然后旋转,判断是否
与其他的线段相交
看了题解,枚举线段端点,如果有以其中任意两点为端点的线段与所有线段香蕉,就符合题意
*/

const double ESP = 1e-10;
int dcmp(double x) {
    if (fabs(x) < ESP) {
        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-(Point A, Point B) {
    return Vector(A.x - B.x, A.y - B.y);
}

Vector operator*(Vector A, double k) {
    return Vector(A.x * k, A.y * k);
}

Vector operator/(Vector A, double k) {
    return Vector(A.x / k, A.y / k);
}

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(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 OnSegment(Point A, Point B, Point P) {
    return dcmp(Cross(A - P, B - P)) == 0;
}

//直线AB与线段CD非规范相交
bool LineIntersection(Point A, Point B, Point C, Point D) {
    double c1 = Cross(B - A, C - A), c2 = Cross(B - A, D - A);
    return OnSegment(A, B, C) || OnSegment(A, B, D) || (dcmp(c1) * dcmp(c2)) < 0; //点在线上 或者 C, D在AB线的两侧
}

const int MAXN = 200 + 5;
Point points[MAXN];
int cases, n;

bool slove() {

    for (int i = 0; i < n - 1; ++i) {
        //枚举线段
        for (int j = i + 1; j < n; ++j) {

            if (points[i] == points[j]) {
                continue;
            }

            bool ok = true;
            for (int k = 0; ok && k < n - 1; k += 2) {
                //判断两线段是否相交(非规范即可)
                //printf("%d=%d=%d\n", i, j, k);
                if (!LineIntersection(points[i], points[j], points[k], points[k + 1])) {
                    ok = false;
                    break;
                    //continue;
                }
            }
            if (ok) {
                return true;
            }
        }
    }
    return false;
}

int main() {
    scanf("%d", &cases);
    while (cases--) {
        scanf("%d", &n);
        n *= 2;
        for (int i = 0; i < n; i += 2) {
            scanf("%lf%lf%lf%lf", &points[i].x, &points[i].y, &points[i + 1].x, &points[i + 1].y);
        }
        printf("%s!\n", slove() ? "Yes" : "No");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值