POJ 3304 Segments (直線和線段相交判斷)


Segments
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7739   Accepted: 2316

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!

Source

 
 
題目大意:給出n條線段兩個端點的坐標,問所有線段投影到一條直線上,如果這些所有投影至少相交於一點就輸出Yes!,否則輸出No!。
解題思路:如果有存在這樣的直線,過投影相交區域作直線的垂線,該垂線必定與每條線段相交,問題轉化為問是否存在一條線和所有線段相交
 
直線肯定經過兩個端點。
枚舉端點,判斷直線和線段是否相交。
 
細節要注意,判斷重合點。
 
還有就是加入只有一條線段的話,剛好直線是過同一條直線的。
所以保險的做法是枚舉所有的兩個端點,包括同一條直線的。
 
/************************************************************
 * Author        : kuangbin
 * Email         : kuangbin2009@126.com 
 * Last modified : 2013-07-13 20:57
 * Filename      : POJ3304Segments.cpp
 * Description   : 
 * *********************************************************/

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>

using namespace std;

const double eps = 1e-8;
int sgn(double x)
{
    if(fabs(x) < eps)return 0;
    if(x < 0) return -1;
    return 1;
}
struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y)
    {
        x = _x;y = _y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x - b.x,y - b.y);
    }
    double operator ^(const Point &b)const
    {
        return x*b.y - y*b.x;
    }
    double operator *(const Point &b)const
    {
        return x*b.x + y*b.y;
    }
};
struct Line
{
    Point s,e;
    Line(){}
    Line(Point _s,Point _e)
    {
        s = _s;e = _e;
    }
};
double xmult(Point p0,Point p1,Point p2) //p0p1 X p0p2
{
    return (p1-p0)^(p2-p0);
}
bool Seg_inter_line(Line l1,Line l2) //判斷直線l1和線段l2是否相交
{
    return sgn(xmult(l2.s,l1.s,l1.e))*sgn(xmult(l2.e,l1.s,l1.e)) <= 0;
}
double dist(Point a,Point b)
{
    return sqrt( (b - a)*(b - a) );
}
const int MAXN = 110;
Line line[MAXN];
bool check(Line l1,int n)
{
    if(sgn(dist(l1.s,l1.e)) == 0 )return false;
    for(int i = 0;i < n;i++)
        if(Seg_inter_line(l1,line[i]) == false)
            return false;
    return true;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n;
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        double x1,y1,x2,y2;
        for(int i = 0; i < n;i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            line[i] = Line(Point(x1,y1),Point(x2,y2));
        }
        bool flag = false;
        for(int i = 0;i < n;i++)
            for(int j = 0; j < n;j++)
                if(check(Line(line[i].s,line[j].s),n) || check(Line(line[i].s,line[j].e),n)
                        || check(Line(line[i].e,line[j].s),n) || check(Line(line[i].e,line[j].e),n) )
                {
                    flag = true;
                    break;
                }
        if(flag)
            printf("Yes!\n");
        else printf("No!\n");
    }
    return 0;
}

 

 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM