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. Remember that ordinal parameters are 1-based!

    问题发生的原因是:hql语句里不需要参数,却添加了一个参数,删掉添加参数的语句就可以了!

  2. 一个简单的ORM制作(CURD操作类)

    SQL执行类 CURD操作类 其他酱油类 此篇是为上篇文章填坑的,不知道上篇砸过来的砖头够不够,不够的话请大家继续砸. CURD操作类负责将用户提供的条件转换为SQL语句,并提供给IHelper执行, ...

  3. WEBUS2.0 In Action - 搜索操作指南 - (3)

    上一篇:WEBUS2.0 In Action - 搜索操作指南(2) | 下一篇:WEBUS2.0 In Action - 搜索操作指南(4) 3. 评分机制 (Webus.Search.IHitSc ...

  4. 使用了BeanUtils的简单操作

    直接获取对象的某个值et.createCell(BeanUtils.getProperty(o, eh.getFieldName())); 简单为对象某个字段赋值c.setCellValue(Bean ...

  5. 【转】发布的QT程序无法显示图标和图片的问题

    在windows下编译好的QT程序在其他没有安装QT的机器上会出现图标和图片无法正常显示的问题. 这时我们可以通过以下方式来解决: 在release文件夹里创建plugins文件夹,并将QT安装目录下 ...

  6. <十二>面向对象分析之UML核心元素之节点和设备

    节点,设备

  7. Shell教程5-Shell运算符

    Bash 支持很多运算符,包括算数运算符.关系运算符.布尔运算符.字符串运算符和文件测试运算符. 原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr,expr 最 ...

  8. 支持 MBTiles 规范的预缓存

    SuperMap iServer 支持生成符合MBTiles规范的预缓存(MBTiles是由MapBox制定的一种将瓦片地图数据存储到SQLite数据库中并可快速使用,管理和分享的规范. 该规范由Ma ...

  9. Brush、Color、String相互转换

    using System.Windows.Media; 1.String转换成Color Color color = (Color)ColorConverter.ConvertFromString(s ...

  10. nginx upstream模块

    upstream模块 upstream模块 (100%) nginx模块一般被分成三大类:handler.filter和upstream.前面的章节中,读者已经了解了handler.filter. 利 ...