POJ_3304_Segments_线段判断是否相交
POJ_3304_Segments_线段判断是否相交
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!
在二维空间中给定n个线段,写一个程序,它决定是否存在一条线,这样在投射出这些段之后,所有的投影段至少有一个点是相同的。 假设存在这样的直线,那么过所有的投影的交点做这条直线的垂线一定和所有线段相交,问题转化为是否存在一条直线与所有线段相交。
并且这条线段能通过平移或旋转使它经过某两个线段的端点。于是只需要把所有点存起来,枚举直线。
注意这道题可能有重点。 代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef double f2;
#define eps 1e-8
struct Point {
f2 x,y;
Point() {}
Point(f2 x_,f2 y_) :
x(x_),y(y_) {}
Point operator + (const Point &p) {return Point(x+p.x,y+p.y);}
Point operator - (const Point &p) {return Point(x-p.x,y-p.y);}
Point operator * (f2 rate) {return Point(x*rate,y*rate);}
};
f2 dot(const Point &p1,const Point &p2) {return p1.x*p2.x+p1.y*p2.y;}
f2 cross(const Point &p1,const Point &p2) {return p1.x*p2.y-p1.y*p2.x;}
f2 fabs(f2 x) {return x>0?x:-x;}
int n;
Point a[120],b[120],c[220],p1,p2;
bool judge(f2 x,f2 y) {
if(fabs(x)<eps||fabs(y)<eps) return 1;
return (x<-eps&&y>eps)||(x>eps&&y<-eps);
}
bool check() {
int i;
Point t=p1-p2;
for(i=1;i<=n;i++) {
Point d1=a[i]-p2,d2=b[i]-p2;
f2 tmp1=cross(t,d1),tmp2=cross(t,d2);
if(!judge(tmp1,tmp2)) return 0;
}
return 1;
}
int main() {
int T;
scanf("%d",&T);
while(T--) {
int flg=0;
scanf("%d",&n);
int i,j;
for(i=1;i<=n;i++) {
scanf("%lf%lf%lf%lf",&a[i].x,&a[i].y,&b[i].x,&b[i].y);
c[i]=a[i];
c[i+n]=b[i];
}
for(i=1;i<=2*n;i++) {
for(j=i+1;j<=2*n;j++) {
if(fabs(c[i].x-c[j].x)>eps||fabs(c[i].y-c[j].y)>eps) {
p1=c[i]; p2=c[j];
if(check()) {
flg=1; break;
}
}
}
if(flg) break;
}
puts(flg?"Yes!":"No!");
}
}
POJ_3304_Segments_线段判断是否相交的更多相关文章
- 判断线段和直线相交 POJ 3304
// 判断线段和直线相交 POJ 3304 // 思路: // 如果存在一条直线和所有线段相交,那么平移该直线一定可以经过线段上任意两个点,并且和所有线段相交. #include <cstdio ...
- POJ 1410--Intersection(判断线段和矩形相交)
Intersection Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16322 Accepted: 4213 Des ...
- poj 1410 Intersection (判断线段与矩形相交 判线段相交)
题目链接 Intersection Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12040 Accepted: 312 ...
- HDU 1558 Segment set (并查集+线段非规范相交)
题目链接 题意 : 如果两个线段相交就属于同一集合,查询某条线段所属集合有多少线段,输出. 思路 : 先判断与其他线段是否相交,然后合并. #include <cstdio> #inclu ...
- Inheritance - SGU 129(线段与多边形相交的长度)
题目大意:给一个凸多边形(点不是按顺序给的),然后计算给出的线段在这个凸多边形里面的长度,如果在边界不计算. 分析:WA2..WA3...WA4..WA11...WA的无话可说,总之细节一定考虑清楚, ...
- poj 3304 Segments 线段与直线相交
Segments Time Limit: 1000MS Memory Limit: 65536K Description Given n segments in the two dim ...
- POJ 1039 Pipe【经典线段与直线相交】
链接: http://poj.org/problem?id=1039 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- poj3449(判断直线相交)
题目链接:https://vjudge.net/problem/POJ-3449 题意:给出若干几何体,判断每个几何体与其它几何体的相交情况,并依次输出. 思路: 首先要知道的是根据正方形对角线的两个 ...
- POJ 3805 Separate Points (判断凸包相交)
题目链接:POJ 3805 Problem Description Numbers of black and white points are placed on a plane. Let's ima ...
随机推荐
- 细说Web页面与本地电脑通讯
话说在很久很久以前.Web页面与客户的本地电脑Localhost通讯,有两种方式: 1.Flash 2.ActiveX控件 由于Flash本人不是很了解,也给出不了什么示例代码, 对于ActiveX控 ...
- Java 必看的 Spring 知识汇总!有比这更全的算我输!
往 期 精 彩 推 荐 [1]Java Web技术经验总结 [2]15个顶级Java多线程面试题及答案,快来看看吧 [3]面试官最喜欢问的十道java面试题 [4]从零讲JAVA ,给你一条清晰 ...
- SQL的几种连接:内连接、左联接、右连接、全连接、交叉连接
SQL连接可以分为内连接.外连接.交叉连接. 数据库数据: book表 stu表 1.内连接 ...
- HTML DOM - 导航
HTML DOM 节点列表长度 length 属性定义节点列表中节点的数量. 您可以使用 length 属性来循环节点列表: x=document.getElementsByTagName(" ...
- Oracle的网络监听配置
listener.ora.tnsnames.ora和sqlnet.ora这3个文件是关系oracle网络配置的3个主要文件,都是放在$ORACLE_HOME\network\admin目录下.其中li ...
- [C#网络应用编程]2、对线程的管理
在System.Threading命名空间下,有一个Thread类,用于对线程进行管理,如创建线程.启动线程.终止线程.合并线程.让线程休眠等 Thread类 (假设Thread firTh = ne ...
- 解决miner.start() 返回null
下面我列举了三种解决方案,前面两张是某两位大佬试出来的,第三种是我蒙出来的. 第一种解决方案 转载自 http://mp.weixin.qq.com/s/AB9vj1EzOgWT7JyryfQhDg ...
- windows SSH Tunnel实施日记
1.准备条件:SSH跳板服务器一个.软件:Putty,CCProxy 2.putty建立SSH Tunnel:先在session那儿把服务器地址填好,到Tunnel界面上,选Dynamics和Auto ...
- html5 兼容版本 video
<!-- first try HTML5 playback: if serving as XML, expand `controls` to `controls="controls&q ...
- Stack和Vector源码分析
Stack和Vector源码分析 Stack和Vector源码分析stack源码分析1.Stack是什么2.Stack的结构图3.Stack继承关系4.Stack的主要方法5.Stack源码Vecto ...