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_线段判断是否相交的更多相关文章

  1. 判断线段和直线相交 POJ 3304

    // 判断线段和直线相交 POJ 3304 // 思路: // 如果存在一条直线和所有线段相交,那么平移该直线一定可以经过线段上任意两个点,并且和所有线段相交. #include <cstdio ...

  2. POJ 1410--Intersection(判断线段和矩形相交)

    Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16322   Accepted: 4213 Des ...

  3. poj 1410 Intersection (判断线段与矩形相交 判线段相交)

    题目链接 Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12040   Accepted: 312 ...

  4. HDU 1558 Segment set (并查集+线段非规范相交)

    题目链接 题意 : 如果两个线段相交就属于同一集合,查询某条线段所属集合有多少线段,输出. 思路 : 先判断与其他线段是否相交,然后合并. #include <cstdio> #inclu ...

  5. Inheritance - SGU 129(线段与多边形相交的长度)

    题目大意:给一个凸多边形(点不是按顺序给的),然后计算给出的线段在这个凸多边形里面的长度,如果在边界不计算. 分析:WA2..WA3...WA4..WA11...WA的无话可说,总之细节一定考虑清楚, ...

  6. poj 3304 Segments 线段与直线相交

    Segments Time Limit: 1000MS   Memory Limit: 65536K       Description Given n segments in the two dim ...

  7. POJ 1039 Pipe【经典线段与直线相交】

    链接: http://poj.org/problem?id=1039 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  8. poj3449(判断直线相交)

    题目链接:https://vjudge.net/problem/POJ-3449 题意:给出若干几何体,判断每个几何体与其它几何体的相交情况,并依次输出. 思路: 首先要知道的是根据正方形对角线的两个 ...

  9. POJ 3805 Separate Points (判断凸包相交)

    题目链接:POJ 3805 Problem Description Numbers of black and white points are placed on a plane. Let's ima ...

随机推荐

  1. Cloud Carousel

    <div class="carousel1" id="carousel1" > <a href="#"><im ...

  2. JMM规范

    JMM规范: The rules for happens-before are: Program order rule. Each action in a thread happens-before ...

  3. golang升级

    系统安装软件一般在/usr/share,可执行的文件在/usr/bin,配置文件可能安装到了/etc下等. 文档一般在 /usr/share 可执行文件 /usr/bin 配置文件 /etc lib文 ...

  4. Mac下的Bash配置文件冲突问题

    Mac下默认的Bash配置文件是~/.profile.有的软件安装时会生成~/.bash_profiel.有了这个文件.之前的.profiel就不会再被加载,需要手动把里面的文件内容转移到.bash_ ...

  5. C++中,用类和重载运算符写高精模板

    先放代码: #include<iostream> #include<cstdio> #include<cstring> using namespace std; s ...

  6. 想做微信小程序第三方代理,各位觉得一键生成平台能赚到钱吗?

    这几年生意不景气,这是很多人的共识.从2009年开始,各种专家就判断"明年经济是最差的一年."然后,这个明年,一直"明"到了2018年,到最后,我们发现,经济就 ...

  7. JS的事件绑定、事件流模型

    .t1 { background-color: #ff8080; width: 1100px; height: 40px } 一.JS事件 (一)JS事件分类 1.鼠标事件:click/dbclick ...

  8. springCloud系列 Config配置中心

    1.config服务的部署 2.yum文件的格式 大小写敏感 使用缩进表示层级关系 缩进时不允许使用Tab键,只允许使用空格. 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可 3.热部署 4.配 ...

  9. SIMATIC_STEP_V5.6使用报错

    SIMATIC_STEP_V5.6是西门子出来的一款能够在Windows 10上运行的step 7软件. 正常的安装步骤我们可以在百度上面进行一些查找,在这里我将我遇到的一个问题发出来. 在Windo ...

  10. Python3实现ICMP远控后门(下)之“Boss”出场

    ICMP后门 前言 第一篇:Python3实现ICMP远控后门(上) 第二篇:Python3实现ICMP远控后门(上)_补充篇 第三篇:Python3实现ICMP远控后门(中)之"嗅探&qu ...