Segments POJ

来源:互联网 发布:插画培训班 知乎 编辑:程序博客网 时间:2024/06/05 15:51

判读线段与直线相交

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


原创粉丝点击