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 x1y1 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!

题意:

已知N条线段,问是否存在直线X,使得这些线段在直线上的投影有公共点。

思路:

转化一下,就是问是是否存在直线X,使得X的垂线L与每个线段都有公共点。如果存在L,那么把其中一些满足条件的L平移,

可以使得它结果两个已知线段的端点。

注意一下:

需要判断是否两点重合,开始我用的方法是fabs(x1-x2)<eps,fabs(y1-y2)<eps则重合,改为dist<=eps则重合就AC了。

#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
const double eps=1e-;
int N;
struct Cpoint
{
double x,y;
Cpoint(){}
Cpoint(double xx,double yy):x(xx),y(yy){}
};
struct Cline
{
Cpoint a,b;
Cline(){}
Cline(Cpoint aa,Cpoint bb):a(aa),b(bb){}
};
struct Vector
{
double x,y;
Vector(){}
Vector(double xx,double yy):x(xx),y(yy){}
double friend operator ^(Vector a,Vector b){
return a.x*b.y-b.x*a.y;
}
};
double dist(Cpoint a,Cpoint b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
Cline L[maxn];
bool check(Cpoint w, Cpoint v)
{
if(dist(w,v)<=eps) return false;// 判断两点重合,开始用点直接比较WA了。
Vector base(w.x-v.x,w.y-v.y);
for(int i=;i<=N;i++){
Vector e(L[i].a.x-v.x,L[i].a.y-v.y);
Vector p(L[i].b.x-v.x,L[i].b.y-v.y);
if((base^e)*(base^p)>eps) return false;
} return true;
}
bool find()
{
for(int i=;i<=N;i++)
for(int j=;j<=N;j++){
if(check(L[i].a,L[j].a)) return true;
if(check(L[i].a,L[j].b)) return true;
if(check(L[i].b,L[j].a)) return true;
if(check(L[i].b,L[j].b)) return true;
}
return false;
}
int main()
{
int T; double x,y,xx,yy;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
for(int i=;i<=N;i++){
scanf("%lf%lf%lf%lf",&x,&y,&xx,&yy);
L[i]=Cline(Cpoint(x,y),Cpoint(xx,yy));
}
if(find()) printf("Yes!\n");
else printf("No!\n");
}
return ;
}

POJ3304:Segments (几何:求一条直线与已知线段都有交点)的更多相关文章

  1. hdu 1086 You can Solve a Geometry Problem too 求n条直线交点的个数

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  2. C++ 根据两点式方法求直线并求两条直线的交点

    Line.h #pragma once //Microsoft Visual Studio 2015 Enterprise //根据两点式方法求直线,并求两条直线的交点 #include"B ...

  3. poj3304(是否存在一条直线与所有给出线段相交

    题意:给出n条线段,问你是否存在一条直线让他与所有线段相交. 思路:枚举两条直线的起点和终点做一条直线,看他是否与所有线段相交. #include<cstdio> #include< ...

  4. 一条直线上N个线段所覆盖的总长度

    原文:http://blog.csdn.net/bxyill/article/details/8962832 问题描述: 现有一直线,从原点到无穷大. 这条直线上有N个线段.线段可能相交. 问,N个线 ...

  5. Intersecting Lines--POJ1269(判断两条直线的关系 && 求两条直线的交点)

    http://poj.org/problem?id=1269 我今天才知道原来标准的浮点输出用%.2f   并不是%.2lf  所以wa了好几次 题目大意:   就给你两个线段 然后求这两个线段所在的 ...

  6. 求两条直线相交点 AS3代码

    ,); ,); ,); ,); var p:Point = new Point(); trace(checkPoint()) function checkPoint() { if (p1Start.x ...

  7. AS3 求两条直线的交点

    //粘贴到帧上运行即可 var p1Start:Point = new Point(0,0); var p1End:Point = new Point(50,50); var p2Start:Poin ...

  8. POJ3304 Segments 【线段直线相交】

    题意: 给出n条线段两个端点的坐标,问所有线段投影到一条直线上,如果这些所有投影至少相交于一点就输出Yes!,否则输出No!. 思路: 计算几何.这道题要思考到两点: 1:把问题转化为是否存在一条直线 ...

  9. poj 3304 判断是否存在一条直线与所有线段相交

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

随机推荐

  1. android图片上传

    package com.example.center; import java.io.ByteArrayOutputStream;import java.io.InputStream; import ...

  2. hdu4619 / 最大独立集

    题意,一个矩阵,上面可以横放或者竖着放骨牌(1X2)保证横的与横的不重叠,竖的和竖的不重叠,求拿掉最小的牌,使所有的都不重叠. 分析:一看,不重叠就是没有边,拿最少,就是留最多,最大独立集啊!二分图, ...

  3. 激活win10系统的方法(亲测)

    WIN+X 按A (或者点击左下角有个windows小图标“鼠标右键”选择选择“命令提示符号(管理员)”) 输入下面命令,回车(一行按一个回车键)slmgr.vbs /upkslmgr /ipk W2 ...

  4. C# 编程中的堆栈(Stack)和队列(Queue)

    一.什么是堆?(Heap)      堆是无序的,是一片不连续的内存域,由用户自己来控制和释放,如果用户自己不释放的话,当内存达到一定的特定值时,通过垃圾回收器(GC)来回收.      是程序运行期 ...

  5. 快速掌握RabbitMQ(二)——四种Exchange介绍及代码演示

    在上一篇的最后,编写了一个C#驱动RabbitMQ的简单栗子,了解了C#驱动RabbitMQ的基本用法.本章介绍RabbitMQ的四种Exchange及各种Exchange的使用场景. 1 direc ...

  6. [TJOI2019]唱、跳、rap和篮球_生成函数_容斥原理_ntt

    [TJOI2019]唱.跳.rap和篮球 这么多人过没人写题解啊 那我就随便说说了嗷 这题第一步挺套路的,就是题目要求不能存在balabala的时候考虑正难则反,要求必须存在的方案数然后用总数减,往往 ...

  7. DDCTF2019逆向分析前俩题WriteUP

    DDCTF2019 笔者做了前俩道题.冷不丁过去一个月了.现在在此做一下WriteUp:题目链接: 1:题目1 2:题目2 reverse1:writeup: 1.程序打开后如下所示 2.查壳结果为U ...

  8. 项目整理--Echarts前端后台的贯通写法

    项目整理–Echarts前端后台的贯通写法 注:下面所有内容建立在FH admin开源框架和eharts插件基础上,建议观看本案例者进行了解. 业务逻辑 绘制两张图表.分别显示城市空间库和其它数据仓库 ...

  9. Intel Edison —— 控制GPIO接口,网页显示传感器数值

    前言 原创文章,转载引用务必注明链接. 因为是使用Typora(markdown)写好然后复制到论坛的,推荐直接访问我的网站以获得更好地阅读体验. Intel XDK IoT 开发环境很久之前就上手了 ...

  10. onlyOffice 开发相关 总结

    onlyOffice 服务端 客户端 相关开发整理 功能: 所有客户端都可用 云端部署服务 查看 预览 doc ppt excel 编辑 权限控制 开发技术准备 用户服务器端 提供保存接口 用户浏览器 ...