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-;
int sgn(double x)
{
if(fabs(x) < eps)return ;
if(x < ) return -;
return ;
}
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)) <= ;
}
double dist(Point a,Point b)
{
return sqrt( (b - a)*(b - a) );
}
const int MAXN = ;
Line line[MAXN];
bool check(Line l1,int n)
{
if(sgn(dist(l1.s,l1.e)) == )return false;
for(int i = ;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 = ; 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 = ;i < n;i++)
for(int j = ; 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 ;
}
 
 
 
 

POJ 3304 Segments (直线和线段相交判断)的更多相关文章

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

    Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13514   Accepted: 4331 Descrip ...

  2. POJ 3304 Segments(线的相交判断)

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

  3. POJ 1039 Pipe(直线和线段相交判断,求交点)

    Pipe Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8280   Accepted: 2483 Description ...

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

    POJ 3304  Segments 题意:给定n(n<=100)条线段,问你是否存在这样的一条直线,使得所有线段投影下去后,至少都有一个交点. 思路:对于投影在所求直线上面的相交阴影,我们可以 ...

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

    题目传送门:POJ 3304 Segments Description Given n segments in the two dimensional space, write a program, ...

  6. 判断直线与线段相交 POJ 3304 Segments

    题意:在二维平面中,给定一些线段,然后判断在某直线上的投影是否有公共点. 转化,既然是投影,那么就是求是否存在一条直线L和所有的线段都相交. 证明: 下面给出具体的分析:先考虑一个特殊的情况,即n=1 ...

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

    POJ 3304 Segments 大意:给你一些线段,找出一条直线可以穿过全部的线段,相交包含端点. 思路:遍历全部的端点,取两个点形成直线,推断直线是否与全部线段相交,假设存在这种直线,输出Yes ...

  8. poj 3304(直线与线段相交)

    传送门:Segments 题意:线段在一个直线上的摄影相交 求求是否存在一条直线,使所有线段到这条直线的投影至少有一个交点 分析:可以在共同投影处作原直线的垂线,则该垂线与所有线段都相交<==& ...

  9. poj 3304 Segments (题意理解出错,错误的只枚举了过线段的直线)

    //枚举过每一条线段的直线, //再判断其他线段的点在直线上或被直线穿过 //即求直线与线段相交(叉积) #include<stdio.h> #include<math.h> ...

随机推荐

  1. Bootstrap_组件

    一.Glyphicons 字体图标 1.所有可用的图标查看:http://v3.bootcss.com/components/ 2.获取字体图标:我们已经在 环境安装 章节下载了 Bootstrap ...

  2. 51nod1556 计算

    ans[n]=ans[n-1]*3-m[n-2];YY一下可以懂的.减掉的就是往下走的情况不符合正整数的情况.m是默慈金数. #include<cstdio> #include<cs ...

  3. Windows系统下Memcached缓存系列二:CouchbaseClient(c#客户端)的详细试用,单例模式

    在上一篇文章里面 ( Windows系统下Memcached缓存系列一:Couchbase(服务器端)和CouchbaseClient(c#客户端)的安装教程 ),我们介绍了服务器端的安装和客户端的安 ...

  4. [Swift系列]003- 函数

    [基础] Swift函数格式: 1.定义格式: func   函数名(参数名1:数据类型,... ,参数名n:数据类型) -> (返回值类型1,...,返回值类型n){ ///函数体内语句 } ...

  5. BZOJ 1821 部落划分

    kruskal.第k-1大的边. 其实prim会更快. #include<iostream> #include<cstdio> #include<cstring> ...

  6. IClassSchemaEdit修改要素类信息

    private void ChangeFeatureClassAliasName(IFeatureClass pFeatureClass, string aliasName) { ISchemaLoc ...

  7. Symfony2学习笔记之表单

    对于一个Web开发者来说,处理HTML表单是一个最为普通又具挑战的任务.Symfony2集成了一个Form组件,让处理表单变的容易起来.在这一节里,我们将从基础开始创建一个复杂的表单,学习表单类库中最 ...

  8. informatica9.5.1资源库为machine in exclusive mode(REP_51821)

    错误信息: [PCSF_10007]Cannot connect to repository [Rs_RotKang] because [REP_51821]Repository Service is ...

  9. 【初识——最大流】 hdu 1532 Drainage Ditches(最大流) USACO 93

    最大流首次体验感受—— 什么是最大流呢? 从一个出发点(源点),走到一个目标点(汇点),途中可以经过若干条路,每条路有一个权值,表示这条路可以通过的最大流量. 最大流就是从源点到汇点,可以通过的最大流 ...

  10. C++ STL知识点小结

    1.capacity(容量)与size(长度)的区别. size(长度)指容器当前拥有的元素个数. capacity(容量)指容器在必须分配新存储空间之前可以存储的元素总数.