POJ 3304 Segments(计算几何)

题意:给定一些线段,问能否找出一条直线,穿过所有线段

思路:如果存在一条直线,那么必然有一条直线是过已有的两点,那么就枚举两点,然后去判断是否跟所有线段有交点即可

代码:

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

struct Point {
    double x, y;
    Point() {}
    Point(double x, double y) {
        this->x = x;
        this->y = y;
    }
    void read() {
        scanf("%lf%lf", &x, &y);
    }
};

typedef Point Vector;

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

Vector operator - (Vector A, Vector 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);
}

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

const double eps = 1e-8;

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 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));} //向量夹角
double Cross(Vector A, Vector B) {return A.x * B.y - A.y * B.x;} //叉积
double Area2(Point A, Point B, Point C) {return Cross(B - A, C - A);} //有向面积

//向量旋转
Vector Rotate(Vector A, double rad) {
    return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
}

//判断3点共线
bool LineCoincide(Point p1, Point p2, Point p3) {
    return dcmp(Cross(p2 - p1, p3 - p1)) == 0;
}

//判断向量平行
bool LineParallel(Vector v, Vector w) {
    return Cross(v, w) == 0;
}

//判断向量垂直
bool LineVertical(Vector v, Vector w) {
    return Dot(v, w) == 0;
}

//计算两直线交点,平行,重合要先判断
Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) {
    Vector u = P - Q;
    double t = Cross(w, u) / Cross(v, w);
    return P + v * t;
}

//点到直线距离
double DistanceToLine(Point P, Point A, Point B) {
    Vector v1 = B - A, v2 = P - A;
    return fabs(Cross(v1, v2)) / Length(v1);
}

//点到线段距离
double DistanceToSegment(Point P, Point A, Point B) {
    if (A == B) return Length(P - A);
    Vector v1 = B - A, v2 = P - A, v3 = P - B;
    if (dcmp(Dot(v1, v2)) < 0) return Length(v2);
    else if (dcmp(Dot(v1, v3)) > 0) return Length(v3);
    else return fabs(Cross(v1, v2)) / Length(v1);
}

//点在直线上的投影点
Point GetLineProjection(Point P, Point A, Point B) {
    Vector v = B - A;
    return A + v * (Dot(v, P - A) / Dot(v, v));
}

//线段相交判定(规范相交)
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2) {
    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);
    //dcmp(c1) * dcmp(c2) == 0 || dcmp(c3) * dcmp(c4) == 0为不规范相交
    return dcmp(c1) * dcmp(c2) <= 0;// && dcmp(c3) * dcmp(c4) <= 0;
}

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

//n边形的面积
double PolygonArea(Point *p, int n) {
    double area = 0;
    for (int i = 1; i < n - 1; i++)
        area += Cross(p[i] - p[0], p[i + 1] - p[0]);
    return area / 2;
}

const int N = 105;

int t, n;

struct Line {
    Point a, b;
    void read() {
        a.read();
        b.read();
    }
} line[N];

bool judge(Point a, Point b) {
    if (dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0) return false;
    for (int i = 0; i < n; i++)
        if (!SegmentProperIntersection(a, b, line[i].a, line[i].b)) return false;
    return true;
}

bool gao() {
    for (int i = 0; i < n; i++) {
        if (judge(line[i].a, line[i].b)) return true;
        for (int j = 0; j < i; j++) {
            if (judge(line[i].a, line[j].a)) return true;
            if (judge(line[i].a, line[j].b)) return true;
            if (judge(line[i].b, line[j].a)) return true;
            if (judge(line[i].b, line[j].b)) return true;
        }
    }
    return false;
}

int main() {
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
            line[i].read();
        if (gao()) printf("Yes!\n");
        else printf("No!\n");
    }
    return 0;
}
时间: 2024-10-20 06:29:53

POJ 3304 Segments(计算几何)的相关文章

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

POJ 3304 Segments 大意:给你一些线段,找出一条直线能够穿过所有的线段,相交包括端点. 思路:遍历所有的端点,取两个点形成直线,判断直线是否与所有线段相交,如果存在这样的直线,输出Yes,但是注意去重. struct Point { double x, y; } P[210]; struct Line { Point a, b; } L[110]; double xmult(Point p1, Point p2, Point p) { return (p1.x-p.x)*(p2.

POJ 3304 Segments 判断直线和线段相交

POJ 3304  Segments 题意:给定n(n<=100)条线段,问你是否存在这样的一条直线,使得所有线段投影下去后,至少都有一个交点. 思路:对于投影在所求直线上面的相交阴影,我们可以在那里作一条线,那么这条线就和所有线段都至少有一个交点,所以如果有一条直线和所有线段都有交点的话,那么就一定有解. 怎么确定有没直线和所有线段都相交?怎么枚举这样的直线?思路就是固定两个点,这两个点在所有线段上任意取就可以,然后以这两个点作为直线,去判断其他线段即可.为什么呢?因为如果有直线和所有线段都相

POJ 3304 Segments 线段和直线的交点

C - Segments Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3304 Appoint description:  System Crawler  (2016-05-07) Description Given n segments in the two dimensional space, write a program, w

POJ 3304 Segments 基础线段交判断

LINK 题意:询问是否存在直线,使得所有线段在其上的投影拥有公共点 思路:如果投影拥有公共区域,那么从投影的公共区域作垂线,显然能够与所有线段相交,那么题目转换为询问是否存在直线与所有线段相交.判断相交先求叉积再用跨立定理.枚举每个线段的起始结束点作为直线起点终点遍历即可. /** @Date : 2017-07-12 14:35:44 * @FileName: POJ 3304 基础线段交判断.cpp * @Platform: Windows * @Author : Lweleth ([em

POJ 3304 Segments 【计算几何】【直线和线段的关系】

题目链接:http://poj.org/problem?id=3304 题目大意:T个case,每个case里面有N条线段,判断能否存在一条直线,使得所有的线段在这条直线上都能有公共点,如果存在输出Yes,否则输出No. 题目的意思可以变成,在N条直线的2*N个端点中选择两个点组成一条直线能否满足这个条件,暴力枚举即可,注意的一点是在枚举的2*n个点中每次选择的两个点要判断是不是重复的. #include<iostream> #include<stdio.h> #include&l

[POJ 3304]Segments

Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14918   Accepted: 4728 Description Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments

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

http://poj.org/problem?id=3304 等价于是否存在一条直线穿过所有线段 判断直线与线段是否相交: 首先用两点p1,p2确定了一条直线 在用p1,p2分别与计算线段两个端点计算叉乘即可 ,叉乘之积>0就说明线段两端点在直线的同侧,也就是直线不经过此线段 注意顺序不要搞反了 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #incl

POJ 3304 Segments[直线与线段相交]

Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13514   Accepted: 4331 Description Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments

POJ 3304 segments 线段和直线相交

Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14178   Accepted: 4521 Description Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments