POJ 3304 Segments(判断直线和线段相交)
来源:互联网 发布:java工程师待遇怎么样 编辑:程序博客网 时间:2024/05/21 07:56
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
321.0 2.0 3.0 4.04.0 5.0 6.0 7.030.0 0.0 0.0 1.00.0 1.0 0.0 2.01.0 1.0 2.0 1.030.0 0.0 0.0 1.00.0 2.0 0.0 3.01.0 1.0 2.0 1.0
Sample Output
Yes!Yes!No!
Source
原博:http://blog.csdn.net/wangjian8006
题目大意:给出n条线段两个端点的坐标,问所有线段投影到一条直线上,如果这些所有投影至少相交于一点就输出Yes!,否则输出No!。
解题思路:如果有存在这样的直线,过投影相交区域作直线的垂线,该垂线必定与每条线段相交,问题转化为问是否存在一条线和所有线段相交
若存在一条直线与所有线段相机相交,此时该直线必定经过这些线段的某两个端点,所以枚举任意两个端点即可。
这里要主要的地方就是,题目说如果两个点的距离小于1e-8就等价于一点,所以要考虑重点
/*Memory 200KTime 32MS */#include <iostream>#include <math.h>using namespace std;#define MAXM 110#define EPS 1e-8typedef struct{double x1,y1,x2,y2;}Segment;Segment segment[MAXM];int n;double distance(double x1,double y1,double x2,double y2){return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}double corss(double x1,double y1,double x2,double y2,double x,double y){return (x2-x1)*(y-y1)-(x-x1)*(y2-y1);}bool judge(double x1,double y1,double x2,double y2){int i;if(distance(x1,y1,x2,y2)<EPS) return 0;for(i=0;i<n;i++){if(corss(x1,y1,x2,y2,segment[i].x1,segment[i].y1)*corss(x1,y1,x2,y2,segment[i].x2,segment[i].y2)>EPS) return 0;}return 1;}int main(){int t,i,j,ans;scanf("%d",&t);while(t--){scanf("%d",&n);for(i=0;i<n;i++)scanf("%lf%lf%lf%lf",&segment[i].x1,&segment[i].y1,&segment[i].x2,&segment[i].y2);if(n==1) {printf("Yes!\n");continue;}ans=0;for(i=0;i<n && !ans;i++)for(j=i+1;j<n && !ans;j++){if(judge(segment[i].x1,segment[i].y1,segment[j].x1,segment[j].y1) ||judge(segment[i].x1,segment[i].y1,segment[j].x2,segment[j].y2) ||judge(segment[i].x2,segment[i].y2,segment[j].x1,segment[j].y1) ||judge(segment[i].x2,segment[i].y2,segment[j].x2,segment[j].y2))ans=1;}if(ans) printf("Yes!\n");else printf("No!\n");}return 0;
- poj 3304 Segments(判断线段和直线相交)
- POJ 3304 Segments(判断线段和直线是否相交)
- POJ 3304 Segments(判断直线和线段相交)
- POJ 3304 Segments (直线和线段相交判断)
- POJ 3304 Segments (直线和线段相交判断)
- poj 3304 Segments(叉积+直线和线段相交判断)
- POJ 3304 Segments (判断直线和线段是否相交)
- POJ 3304 Segments [判断线段和直线相交]
- poj 3304 Segments--直线和线段相交
- poj 3304 Segments(判断直线与线段相交)
- POJ 3304 Segments 判断直线与线段相交
- POJ 3304 - Segments【计算几何 - 直线线段相交判断】
- poj-3304-Segments-线段与直线相交
- POJ 3304 Segments(线段直线相交)
- POJ 3304 || Segments (线段,直线相交
- poj 3304 Segments 线段与直线相交
- Poj 3304 Segments 【线段于直线相交】
- 【线段直线相交】POJ 3304 Segments
- u-boot配置和编译过程详解
- Windows下Eclipse+PyDev安装Python开发环境
- 使用VS2012 的快捷键
- 详解JVM中的Minor GC、FullGC
- spring 数据连接泄漏
- POJ 3304 Segments(判断直线和线段相交)
- 关机/重启命令
- 开博客啦~~
- [Ruby笔记]4. ruby 读写文件 File open read write new
- 标准库类型——string
- Linux系统搭建SVN服务
- Nim
- Javascript基础知识盲点总结——对象
- Leek' music diary 1