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. 关于linux防火墙

    1.防火墙是什么:防火墙就是用于实现Linux下访问控制的功能的,它分为硬件的或者软件的防火墙两种.无论是在哪个网络中,防火墙工作的地方一定是在网络的边缘.而我们的任务就是需要去定义到底防火墙如何工作 ...

  2. OC实现带弹跳动画按钮的界面控制器view

    很多应用都有带弹跳动画发布界面,这里用一个 UIViewController 实现这种效果,外界只要 modal出不带动画这个控制器就可以实现 #import "BSPublishVC.h& ...

  3. Go中string转[]byte的陷阱

    Go中string转[]byte的陷阱html {overflow-x: initial !important;}#write, body { height: auto; }#write, #writ ...

  4. 解决distinct与order by 的冲突

    sql="select distinct id from test order by otherfield desc" 需要找到不同的id,同时又想让记录按fbsj排序.但是这样一 ...

  5. 一个简单而实用的JQ插件——lazyload.js图片延迟加载插件

      前  言 Cherish 看多了炫酷的插件之后再来看这么一个小清新的东西,是不是突然感觉JQ插件感觉很友好了,简单强大最重要的是实用. 这篇文章将详细讲解一下lazyload.js的用法 lazy ...

  6. ns3构建2 core fat tree出错

    我用ns3构建fat tree以下是我的拓扑结构: 在我用ns3构建完这个拓扑结构,并且加上此行代码: 但是运行的时候报错了.报的错误是: 解决办法是:进入到/src/internet/model/g ...

  7. 类设计:设计卖车的4S店

    class Car(object): # 定义车的方法 def move(self): print('---车在移动---') def stop(self): print('---停车---') # ...

  8. flush()清空文件缓存区

    # 缓冲区:cpu 一级缓存 二级缓存 三级缓存 import time f =open('2.txt','a+' ,encoding='utf-8') f.write('helloworld\n') ...

  9. CentOS7搭建LAMP实战

    环境配置从官网下载稳定的源码包解压预编译编译编译安装启动服务 环境配置 # yum install -y vim wget links //安装一下基本工具# systemctl stop firew ...

  10. NetCore版RPC框架NewLife.ApiServer

    微服务和消息队列的基础都是RPC框架,比较有名的有WCF.gRPC.Dubbo等,我们的NewLife.ApiServer建立在网络库NewLife.Net之上,支持.Net Core,追求轻量级和高 ...