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.

题目大意:给n条线段,问是否存在一条直线穿过所有线段

思路:题目本身没什么难度,就是枚举所有穿过线段端点的直线即可,难是难在精度的控制。第一个要注意的地方是n=1的时候要特判,第二个要注意的是如果两点过于接近是不能作为枚举直线的,因为这样算出来的叉积过小,很容易小于EPS。注意这两点就没什么大问题了。

#include <cstdio>
#include <cmath> #define EPS 1e-10
#define Abs(x) x>0?x:-x typedef double TYPE;
//2D point
struct Point {
TYPE x, y;
Point() {}
Point(double xx, double yy): x(xx), y(yy) {}; bool operator < (const Point &B) const {
if(x == B.x) return y < B.y;
else return x < B.x;
}
};
//if return>0 then a is on the clockwise of b
TYPE cross(const Point &a, const Point &b, const Point &o) {
return (o.x - a.x) * (o.y - b.y) - (o.x - b.x) * (o.y - a.y);
}
//distance between a and b
inline double dist(Point a, Point b){
double x = a.x - b.x, y = a.y - b.y;
return sqrt(x * x + y * y);
}
//2D twp-point form segment
struct Seg {
Point st, ed;
Seg() {}
Seg(Point aa, Point bb):st(aa), ed(bb) {}
};
//if a straddle b
inline bool straddle(const Seg &A, const Seg &B) {
return cross(B.st, A.st, B.ed) * cross(B.st, A.ed, B.ed) < EPS;
} #define MAXN 110 int T, n;
Seg sg[MAXN]; inline bool check(Seg p) {
if(dist(p.st, p.ed) < EPS) return false;
for(int i = 0; i < n; ++i)
if(!straddle(sg[i], p)) return false;
return true;
} bool solve() {
for(int i = 0; i < n; ++i) for(int j = i+1; j < n; ++j) {
if(check(Seg(sg[i].st, sg[j].st))) return true;
if(check(Seg(sg[i].st, sg[j].ed))) return true;
if(check(Seg(sg[i].ed, sg[j].st))) return true;
if(check(Seg(sg[i].ed, sg[j].ed))) return true;
}
return false;
} int main() {
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
for(int i = 0; i < n; ++i) scanf("%lf%lf%lf%lf", &sg[i].st.x, &sg[i].st.y, &sg[i].ed.x, &sg[i].ed.y);
if(n == 1 || solve()) puts("Yes!");
else puts("No!");
}
}

  

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

  1. POJ 3304 Segments 基础线段交判断

    LINK 题意:询问是否存在直线,使得所有线段在其上的投影拥有公共点 思路:如果投影拥有公共区域,那么从投影的公共区域作垂线,显然能够与所有线段相交,那么题目转换为询问是否存在直线与所有线段相交.判断 ...

  2. POJ 3304 Segments (叉乘判断线段相交)

    <题目链接> 题目大意: 给出一些线段,判断是存在直线,使得该直线能够经过所有的线段.. 解题思路: 如果有存在这样的直线,过投影相交区域作直线的垂线,该垂线必定与每条线段相交,问题转化为 ...

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

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

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

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

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

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

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

    题目链接:POJ 3304 Problem Description Given n segments in the two dimensional space, write a program, wh ...

  7. poj 3304线段与直线相交

    http://poj.org/problem?id=3304 Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: ...

  8. poj 3304 Segments(解题报告)

    收获:举一反三:刷一道会一道 1:思路转化:(看的kuangbin的思路) 首先是在二维平面中:如果有很多线段能够映射到这个直线上并且至少重合于一点,充要条件: 是过这个点的此条直线的垂线与其他所有直 ...

  9. POJ 3304 Segments (直线和线段相交判断)

    Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7739   Accepted: 2316 Descript ...

随机推荐

  1. .Net core 使用Jenkins + Docker + Azure Devops 傻瓜式部署

    这几天一直在搞 Jenkins + Docker + Azure Devops 部署,因为这种方式部署真的非常的省心,而且速度快,方便快捷,等等无数优点,感谢我的单身领导的支持,当然也感谢 晓晨大神, ...

  2. Redis与Python进行交互

    安装包 安装Redis的有3种方式https://github.com/andymccurdy/redis-py 第一种:进⼊虚拟环境,联⽹安装包redis pip install redis 第二种 ...

  3. Kali Basic Configuration

    1:Kali Version root@kali-node01:~# cat /etc/os-release PRETTY_NAME="Kali GNU/Linux Rolling" ...

  4. WebService 学习笔记(一、概念及定义)

    定义 WebService是一种服务导向架构(SOA service-oriented architecture)的技术,通过标准的Web协议提供服务,目的是保证不同平台的应用服务可以互操作. Web ...

  5. ASP.NET MVC4.0 后台获取不大前台传来的file

    <td>选择图片</td> <td> <input type="file" id="uploadImg" name=& ...

  6. 使用js实现单向绑定

    详细解释单向绑定 参考资料 MDN addEventListener()定义与用法 <!DOCTYPE html> <html lang="en"> < ...

  7. python的基本知识

    1. python的简介    python的创始⼈人为吉多·范罗苏姆(Guido van Rossum).1989年年的圣诞节期间,吉多· 范罗苏姆为了了在阿姆斯特丹丹打发时间,决⼼心开发⼀个新的脚 ...

  8. Emgucv安装及使用

    Emgucv安装 最近有个客户联系我,希望我能够为他们做一个识别瓷砖花纹的软件.应用场景是这样的:现场会有一个摄像头去拍摄流水线上运输的瓷砖,如果检测这块瓷砖的花纹不符合要求,则需要给PLC或输出板卡 ...

  9. struts2学习笔记四

    一.contextMap中的数据操作 root根:List 元素1 元素2 元素3 元素4 元素5 contextMap:Map key value application Map key value ...

  10. Uber优步北京第四组奖励政策

    优步北京第四组: 定义为2015年7月20日至今激活的司机(以优步后台数据显示为准) 滴滴快车单单2.5倍,注册地址:http://www.udache.com/如何注册Uber司机(全国版最新最详细 ...