POJ 3304 Segments (线段和直线相交 + 思维)

Posted willems

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 3304 Segments (线段和直线相交 + 思维)相关的知识,希望对你有一定的参考价值。

题目: 传送门

 

题意: 给你n条线段的两个端点,问所有线段投影到一条直线上,这些投影至少相交于一点,就输出Yes!,否则就是 No!

 

题解: 

 

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#define LL long long
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF INT_MAX
#define inf LLONG_MAX
#define PI acos(-1)
using namespace std;

const int N = 310;

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 p) { return Vector(A.x * p, A.y * p); }
///向量/数=向量
Vector operator / (Vector A, double p) { return Vector(A.x / p, A.y / p); }

const double eps = 1e-10;
int dcmp(double x) {
    if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;
}

bool operator < (const Point& a, const Point& b) {
    return a.x == b.x ? a.y < b.y : a.x < b.x;
}

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 Length(Vector A) { return sqrt(Dot(A, A)); } /// 计算向量长度
double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); } /// 向量A、B夹角
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } /// 叉积
Vector Rotate(Vector A, double rad) { /// 向量旋转, rad 是弧度, 逆时针旋转 rad
    return Vector(A.x*cos(rad) - A.y*sin(rad), A.x*sin(rad) + A.y*cos(rad));
}

Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) { /// 求两直线交点,请确保 P + tv 和 Q + tw 有唯一交点。 当且仅当Cross(v, w)非0
    Vector u = P - Q;
    double t = Cross(w, u) / Cross(v, w);
    return P + v * t;
}

bool SegmentProperInsection(Point a1, Point a2, Point b1, Point b2) { /// 判断线段是否相交
    double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1);
    double c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
    return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
}

bool OnSegment(Point p, Point a1, Point a2) { /// 点p是否在线段a1,a2上(不包含端点)
    return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0;
}

bool LSProperInsection(Point a1, Point a2, Point b1, Point b2) { /// 判断直线b1b2是否和线段a1a2相交
    return dcmp(Cross(b1 - a1, b2 - a1)) * dcmp(Cross(b1 - a2, b2 - a2)) <= 0;
}

Point P[N], Q[N];
int n;

bool judge(Point a, Point b) {
    if(dcmp(Length(b - a)) == 0) return false;
     rep(i, 1, n) if(LSProperInsection(P[i], Q[i], a, b) == false) return false;
     return true;
}

void solve() {
    scanf("%d", &n);
    rep(i, 1, n) scanf("%lf %lf %lf %lf", &P[i].x, &P[i].y, &Q[i].x, &Q[i].y);
    rep(i, 1, n) rep(j, 1, n)
        if(judge(P[i], P[j]) || judge(P[i], Q[j]) || judge(Q[i], P[j]) || judge(Q[i], Q[j]))
            { puts("Yes!"); return ; }
    puts("No!"); return ;
}

int main() {
    int _; scanf("%d", &_);
    while(_--) solve();
    return 0;
}

 

以上是关于POJ 3304 Segments (线段和直线相交 + 思维)的主要内容,如果未能解决你的问题,请参考以下文章

POJ3304 Segments 线段直线相交

POJ 3304 Segments(计算几何:直线与线段相交)

Segments---poj3304(判断直线与线段的位置关系)

POJ 3304 Segments (叉乘判断线段相交)

POJ 3304 Segments (线段和直线相交 + 思维)

POJ 3304 Segments(判断直线与线段是否相交)