Segments

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15335   Accepted: 4862

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!

Source

 
询问是否存在一条直线与所有线段相交
暴力线段的断点构成直线,判断是否可行。
 //2017-08-30
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath> using namespace std; const int N = ;
const double EPS = 1e-; struct Point{
double x, y;
Point(){}
Point(double _x, double _y):x(_x), y(_y){}
Point(const Point &p):x(p.x), y(p.y){}
//a-b为向量ba
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 a, b;
Line(){}
Line(Point _a, Point _b):a(_a), b(_b){}
void set_a_b(const Point &_a, const Point &_b){
a = _a;
b = _b;
}
}seg[N]; //三态函数
int sgn(double x){
if(fabs(x) < EPS)return ;
if(x < )return -;
else return ;
} //input:Point a;Point b
//output:distance a、b两点间的距离
//note:该函数取名distance会编译错误
double dist(Point a, Point b){
return sqrt((a-b)*(a-b));
} //input:l1 直线l1;l2 线段l2
//output:true 直线l1与线段l2相交;false 直线l1与线段l2不想交
bool seg_inter_line(Line l1, Line l2){
return sgn((l2.a-l1.b)^(l1.a-l1.b))*sgn((l2.b-l1.b)^(l1.a-l1.b)) <= ;
} int n;
//input:直线line
//output:true 直线与所有线段都相交
bool all_inter(Line line){
for(int i = ; i < n; i++)
if(!seg_inter_line(line, seg[i]))
return false;
return true;
} bool check(){
Line line;
for(int i = ; i < n; i++){
for(int j = ; j < n; j++){
if(i == j)continue;
if(dist(seg[i].a, seg[j].a) > EPS){
line.set_a_b(seg[i].a, seg[j].a);
if(all_inter(line))return true;
}
if(dist(seg[i].a, seg[j].b) > EPS){
line.set_a_b(seg[i].a, seg[j].b);
if(all_inter(line))return true;
}
if(dist(seg[i].b, seg[j].a) > EPS){
line.set_a_b(seg[i].b, seg[j].a);
if(all_inter(line))return true;
}
if(dist(seg[i].b, seg[j].b) > EPS){
line.set_a_b(seg[i].b, seg[j].b);
if(all_inter(line))return true;
}
}
}
return false;
} int main()
{
std::ios::sync_with_stdio(false);
//freopen("inputC.txt", "r", stdin);
int T;
cin>>T;
while(T--){
cin>>n;
for(int i = ; i < n; i++){
cin>>seg[i].a.x>>seg[i].a.y>>seg[i].b.x>>seg[i].b.y;
}
if(check() || n == || n == )cout<<"Yes!"<<endl;
else cout<<"No!"<<endl;
} return ;
}

POJ3304(KB13-C 计算几何)的更多相关文章

  1. poj3304 Segments【计算几何】

    C - Segments POJ - 3304 最近开始刷计算几何了 公式好多完全不会 数学不行 几何不行 记忆力不行 当机 查的题解 就当复习吧 这套专题拿来熟悉一下计算几何模板 #include ...

  2. poj3304计算几何直线与线段关系

    Given n segments in the two dimensional space, write a program, which determines if there exists a l ...

  3. 计算几何——线段和直线判交点poj3304

    #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #i ...

  4. ACM/ICPC 之 计算几何入门-叉积-to left test(POJ2318-POJ2398)

    POJ2318 本题需要运用to left test不断判断点处于哪个分区,并统计分区的点个数(保证点不在边界和界外),用来做叉积入门题很合适 //计算几何-叉积入门题 //Time:157Ms Me ...

  5. HDU 2202 计算几何

    最大三角形 Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  6. ACM 计算几何中的精度问题(转)

    http://www.cnblogs.com/acsmile/archive/2011/05/09/2040918.html 计算几何头疼的地方一般在于代码量大和精度问题,代码量问题只要平时注意积累模 ...

  7. hdu 2393:Higher Math(计算几何,水题)

    Higher Math Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. sdut 2603:Rescue The Princess(第四届山东省省赛原题,计算几何,向量旋转 + 向量交点)

    Rescue The Princess Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Several days ago, a b ...

  9. [知识点]计算几何I——基础知识与多边形面积

    // 此博文为迁移而来,写于2015年4月9日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vxaq.html 1.前言 ...

随机推荐

  1. WINDOWS平台下的栈溢出攻击从0到1(篇幅略长但非常值得一看)

    到1的这个过程.笔者也希望能够通过这些技术分享帮助更多的朋友走入到二进制安全的领域中.2.文章拓扑由于本篇文章的篇幅略长,所以笔者在这里放一个文章的拓扑,让大家能够在开始阅读文章之前对整个文章的体系架 ...

  2. Linux删除目录下的文件的10种方法

    看到了一遍文章,便突发奇想的想起Linux中删除目录下的所有文件的方法:整理了几个,如有不足,还望读者不吝赐教! 删除当前目录下的文件 1.rm -f * #最经典的方法,删除当前目录下的所有类型的文 ...

  3. C标准库pow函数精度问题。

    #include <stdio.h> int main () { int temp,i; double a=2.4568; unsigned ]; ;i<;i++) { temp=( ...

  4. Java - Junit单元测试框架

    简介 Junit : http://junit.org/ JUnit是一个开放源代码的Java语言单元测试框架,用于编写和运行可重复的测试. 多数Java的开发环境都已经集成了JUnit作为单元测试的 ...

  5. SQL 将一列多行数据合并为一行

    原表数据: 期望结果: 使用STUFF + FOR XML PATH即可实现以上效果 执行以下SQL: , , '') AS Course FROM Student AS T 可以看到输出结果与期望结 ...

  6. main函数中如何等待协程运行完毕

    使用channel同步 package main import ( "fmt" ) func printNumber(num int, c chan struct{}) { fmt ...

  7. odoo开发笔记 -- 异常信息处理汇总

    1 Traceback (most recent call last): File , in _handle_exception return super(JsonRequest, self)._ha ...

  8. kindEditor 使用

    1. kindEditor简介: KindEditor是一套开源的HTML可视化编辑器,主要用于让用户在网站上获得所见即所得编辑效果.     主要特点: 1. 体积小,加载速度快,但功能十分丰富.2 ...

  9. linux TOP命令各参数详解【转载】

    实时监控或查看系统资源使用情况的工具——TOP top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. 下面详细介绍它的使用方法: ( ...

  10. 工具-infer,静态代码检测

    1.工具介绍 Infer 是一个静态程序分析工具,可以对 Java.C 和 Objective-C 程序进行分析,此工具是用 OCaml写成的.https://github.com/facebook/ ...