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
    */
  • 相关阅读:
    Android中对同一个TextView设置不同字体样式
    C++之new和malloc区别
    cocos2dx 在android平台打开文件问题
    基于物联网操作系统HelloX的智慧家庭体系架构
    密码学:SHA1加密算法详解
    Android中最简单的分享功能
    Android设置ProgressBar的前景和背景及其在多线程中的刷新
    【有明信息】虚实之间 ---关于企业架构是与非的探讨
    5.1.1 读取Redis 数据
    hdr(host), hdr_beg(host) , path_beg
  • 原文地址:https://www.cnblogs.com/cniwoq/p/7620446.html
Copyright © 2011-2022 走看看