Segments
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8579   Accepted: 2608

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 (x1y1) and (x2y2) 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! 题目大意:问是否存在一条直线所有线段在它上面的投影至少有一个公共交点,等同与这条直线的垂线与所有线段都有交点。即求是否有一条与所有线段相交。两两枚举线段四个端点两两成四条直线,
若所有的线段的两个端点分别在直线的两边(只要不是在同一边就行,在直线上也可以),那么说明存在这么一条直线。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std; struct Point{
double x,y;
Point(){}
Point(double x,double y):x(x),y(y){}
}; struct Segment{
Point a,b;
}; typedef Point Vector;
Vector operator -(const Point &A,const Point &B){ return Vector(A.x-B.x,A.y-B.y);}
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-; int dcmp(double x)
{
if(fabs(x)<eps) return ;
else return x<?-:;
} bool operator == (const Point &a,const Point &b){
return (dcmp(a.x-b.x)== && dcmp(a.y-b.y)==);
}
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;}//叉积 vector<Segment> S; bool judge(Point a,Point b)
{
if(a == b) return false;//a,b属于同一个点,一个点不能确定一条直线
int i,n=S.size();
Vector v1=b-a,v2,v3;
for(i=;i<n;i++)
{
v2=S[i].a-a;
v3=S[i].b-a;
if(dcmp(Cross(v1,v2)*Cross(v1,v3)) > ) return false;
}
return true;
} bool solve()
{
int i,j,n=S.size();
for(i=;i<n;i++)
{
for(j=i+;j<n;j++)
if(judge(S[i].a,S[j].a) || judge(S[i].a,S[j].b) || judge(S[i].b,S[j].a) || judge(S[i].b,S[j].b))
return true;
}
return false;
}
int main()
{
int T,n,i;
Segment s;
scanf("%d",&T);
while(T--)
{
S.clear();
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%lf %lf %lf %lf",&s.a.x,&s.a.y,&s.b.x,&s.b.y);
S.push_back(s);
}
if(n==) printf("Yes!\n");
else if(solve()) printf("Yes!\n");
else printf("No!\n");
}
return ;
}

poj 3304 判断是否存在一条直线与所有线段相交的更多相关文章

  1. HDU 3492 (直线与所有线段相交) Segment

    题意: 给出n个线段,判断是否存在一条直线使得所有线段在直线上的射影的交非空. 分析: 如果我们找到一条与所有线段相交的直线,然后做一条与该直线垂直的直线,这些线段在直线上的射影就一定包含这个垂足. ...

  2. POJ 3304 /// 判断线段与直线是否相交

    题目大意: 询问给定n条线段 是否存在一条直线使得所有线段在直线上的投影存在公共点 这个问题可以转化为 是否存在一条直线与所有的线段同时相交 而枚举直线的问题 因为若存在符合要求的直线 那么必存在穿过 ...

  3. C - Segments POJ - 3304 (判断线段相交)

    题目链接:https://vjudge.net/contest/276358#problem/C 题目大意:给你n条线段,问你是否存在一条线段使得所有的线段在这条直线的投影至少具有一个交点? 具体思路 ...

  4. poj 3304 找一条直线穿过所有线段

    题目链接:http://poj.org/problem?id=3304 #include<cstdio> #include<cstring> #include<cmath ...

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

    题目大意:给出一些线段,然后判断这些线段的投影是否有可能存在一个公共点.   分析:如果这些线段的投影存在一个公共点,那么过这个公共点作垂线一定与所有的直线都想交,于是题目转化成是否存在一个直线可以经 ...

  6. Intersecting Lines - POJ 1269(判断平面上两条直线的关系)

    分析:有三种关系,共线,平行,还有相交,共线和平行都可以使用叉积来进行判断(其实和斜率一样),相交需要解方程....在纸上比划比划就出来了....   代码如下: ================== ...

  7. 判断线段和直线相交 POJ 3304

    // 判断线段和直线相交 POJ 3304 // 思路: // 如果存在一条直线和所有线段相交,那么平移该直线一定可以经过线段上任意两个点,并且和所有线段相交. #include <cstdio ...

  8. 简单几何(线段与直线的位置) POJ 3304 Segments

    题目传送门 题意:有若干线段,问是否存在一条直线,所有线段投影到直线上时至少有一个公共点 分析:有一个很好的解题报告:二维平面上线段与直线位置关系的判定.首先原问题可以转换为是否存在一条直线与所有线段 ...

  9. POJ 3304 Segments(直线)

    题目: Description Given n segments in the two dimensional space, write a program, which determines if ...

随机推荐

  1. ubuntu kylin 13.10 无法安装ia32-libs解决方案

    1.安装 Synaptic 2.sudo apt-get install synaptic 3.进入synaptic ,设置->软件库 4.点击  其他软件->添加 5.输入“deb ht ...

  2. JavaScript面试系列:JavaScript设计模式之桥接模式和懒加载

    我写的程序员面试系列文章 Java面试系列-webapp文件夹和WebContent文件夹的区别? 程序员面试系列:Spring MVC能响应HTTP请求的原因? Java程序员面试系列-什么是Jav ...

  3. UVA 1412 Fund Management (预处理+状压dp)

    状压dp,每个状态可以表示为一个n元组,且上限为8,可以用一个九进制来表示状态.但是这样做用数组开不下,用map离散会T. 而实际上很多九进制数很多都是用不上的.因此类似uva 1601 Mornin ...

  4. for..in...时,注意hasOwnProperty验证

    for..in...时,注意hasOwnProperty验证 var obj = { a: 10, b: 20 }; // 注意词句代码 Object.prototype.c = 30; var it ...

  5. C++类型强制转换<转>

    转载:http://www.cnblogs.com/goodhacker/archive/2011/07/20/2111996.html C风格的强制类型转换(Type Cast)很简单,不管什么类型 ...

  6. struts2的单个文件上传

    本文主要两种方式,一:通过 FileUtils.copyFile(file, savefile);方法复制:二:通过字节流方式复制 web.xml <?xml version="1.0 ...

  7. shell脚本,在指定目录下通过随机小写10个字母加固定字符串oldboy批量创建10个html文件。

    [root@localhost wyb]# cat test10.sh #!/bin/bash #使用for循环在/test10目录下通过随机小写10个字母加固定字符串oldboy批量创建10个htm ...

  8. NoSQL 之 Morphia 操作 MongoDB

    上两篇文章:http://www.cnblogs.com/hoojo/archive/2011/06/01/2066426.html http://www.cnblogs.com/hoojo/arch ...

  9. Clover KextsToPatch 使用方法 2015.10.21

    Clover KextsToPatch 使用方法 2015.10.21   前些天,因为 Thinkpad X230 BIOS 白名单限制,给她换了一块 ar9285 无线网卡,只是因为这块网卡正好可 ...

  10. UVa-1368-DNA序列

    这题的话,我们每次统计的话,是以列为外层循环,以行为内层循环,逐一按列进行比较. 统计完了之后,题目中要求说到要hamming值最小的,那我们就选用该列最多的字母就可以了,如果有数目相等的字母,那就按 ...