zoukankan      html  css  js  c++  java
  • POJ-3304Segments[计算几何]

    Segments

    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 on it, all projected segments have at least one point in common.

    Input

    Input begins with a number T showing the number of test cases and then, T test cases follow. Each test case begins with a line containing a positive integer n ≤ 100 showing the number of segments. After that, n lines containing four real numbers x1 y1 x2 y2 follow, in which (x1, y1) and (x2, y2) are the coordinates of the two endpoints for one of the segments.

    Output

    For each test case, your program must output "Yes!", if a line with desired property exists and must output "No!" otherwise. You must assume that two floating point numbers a and b are equal if |a - b| < 10-8.

    Sample Input

    3
    2
    1.0 2.0 3.0 4.0
    4.0 5.0 6.0 7.0
    3
    0.0 0.0 0.0 1.0
    0.0 1.0 0.0 2.0
    1.0 1.0 2.0 1.0
    3
    0.0 0.0 0.0 1.0
    0.0 2.0 0.0 3.0
    1.0 1.0 2.0 1.0

    Sample Output

    Yes!
    Yes!
    No!
    题意:
    求n条线段在一条直线上的投影是否有交点
    思路:
    假设存在这样的一个交点,那么过交点做投影直线的垂线,必定和所有的线段相交,然后就将问题化为构造一条和所有线段相交的直线。通过每次枚举两个线段的
    端点作出直线。
    #include "stdio.h"
    #include "iostream"
    #include "algorithm"
    #include "cmath"
    #include "string.h"
    using namespace std;
    const int maxn = 100 + 10;
    const double eps = 1e-8;
    struct Point {
        double x, y;
        Point(double x=0.0, double y=0.0) :x(x), y(y) {}
    };
    typedef Point Vector;
    Vector operator - (Point A,Point B) {return Vector(A.x-B.x,A.y-B.y);}
    int dcmp(double x) {if (fabs(x) < eps) return 0;return x < 0? -1: 1;} 
    double Dot(Vector A,Vector B) {return A.x*B.x+A.y*B.y;}
    double Length(Point A) {return sqrt(Dot(A,A));}
    double Cross(Vector A,Vector B) {return A.x*B.y-A.y*B.x;}
    
    int N;
    Point p[maxn][4];
    bool getcross(Point L, Point S) {
        if (dcmp(Length(S - L)) == 0) return false;
        for (int i = 0; i < N; i++) {
            if (dcmp(Cross(S-L, p[i][0]-L)*Cross(S-L, p[i][1]-L))>0) return false;
    
        }
        return true;
    }
    void solve() {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (getcross(p[j][0],p[i][0])||getcross(p[j][1],p[i][0])||
                    getcross(p[j][1],p[i][1])||getcross(p[j][0],p[i][1])) {
                    printf("Yes!
    "); return ;
                }
            }
        }
        printf("No!
    ");
    }
    int main(int argc, char const *argv[])
    {
        int T;
        scanf("%d", &T);
        while (T--) {
            scanf("%d", &N);
            for (int i = 0; i < N; i++) {
                scanf("%lf%lf%lf%lf", &p[i][0].x, &p[i][0].y, &p[i][1].x, &p[i][1].y);
            }
            solve();
        }
        return 0;
    }
    /*
    1
    6
    75 93 42 45
    47 44 87 30
    10 55 36 14
    32 81 73 57
    29 84 75 23
    18 38 99 95
    */
  • 相关阅读:
    2018年你需要知道的13个JavaScript工具库
    JavaScript一团乱,这是好事
    5大JavaScript前端框架简介
    大型Vuex应用程序的目录结构
    Github被微软收购,这里整理了16个替代品
    如何使用@vue/cli 3.0在npm上创建,发布和使用你自己的Vue.js组件库
    TensorFlow入门教程
    想成为顶级开发者吗?亲自动手实现经典案例
    2018年最值得关注的30个Vue开源项目
    SQL Server 合并复制遇到identity range check报错的解决 (转载)
  • 原文地址:https://www.cnblogs.com/cniwoq/p/7620446.html
Copyright © 2011-2022 走看看