poj 3304 Segments(计算几何基础)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 11593 | Accepted: 3657 |
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!
Source
【题意】
给出n条线段两个端点的坐标,问所有线段投影到一条直线上,如果这些所有投影至少相交于一点就输出Yes!,否则输出No!。
【思路】
如果有存在这样的直线,过投影相交区域作直线的垂线,该垂线必定与每条线段相交,问题转化为问是否存在一条线和所有线段相交
若存在一条直线与所有线段相机相交,此时该直线必定经过这些线段的某两个端点,所以枚举任意两个端点即可
【代码】
#include<cmath>
#include<cstdio>
#include<cstring>
#define FOR(a,b,c) for(int a=(b);a<(c);a++)
using namespace std; const int N = +;
const double eps = 1e-; int dcmp(double x) {
if(x<eps) return ; else return x<? -:;
} struct Line {
double x1,y1,x2,y2;
}L[N]; int n; double cross(double x1,double y1,double x2,double y2,double x,double y) {
return (x2-x1)*(y-y1)-(x-x1)*(y2-y1);
}
bool judge(double x1,double y1,double x2,double y2) {
if(!dcmp(x2-x1) && !dcmp(y2-y1)) return ;
FOR(i,,n) {
if(cross(x1,y1,x2,y2,L[i].x1,L[i].y1)*
cross(x1,y1,x2,y2,L[i].x2,L[i].y2)>eps) return ;
}
return ;
} int main() {
int T;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
FOR(i,,n)
scanf("%lf%lf%lf%lf",&L[i].x1,&L[i].y1,&L[i].x2,&L[i].y2);
if(n==) { puts("Yes!"); continue; }
bool ans=;
FOR(i,,n) {
FOR(j,i+,n) {
if(judge(L[i].x1,L[i].y1,L[j].x1,L[j].y1)) ans=;
if(judge(L[i].x1,L[i].y1,L[j].x2,L[j].y2)) ans=;
if(judge(L[i].x2,L[i].y2,L[j].x1,L[j].y1)) ans=;
if(judge(L[i].x2,L[i].y2,L[j].x2,L[j].y2)) ans=;
if(ans) break;
}
if(ans) break;
}
if(ans) puts("Yes!");
else puts("No!");
}
return ;
}
poj 3304 Segments(计算几何基础)的更多相关文章
- POJ 3304 Segments 判断直线和线段相交
POJ 3304 Segments 题意:给定n(n<=100)条线段,问你是否存在这样的一条直线,使得所有线段投影下去后,至少都有一个交点. 思路:对于投影在所求直线上面的相交阴影,我们可以 ...
- POJ 3304 Segments(计算几何:直线与线段相交)
POJ 3304 Segments 大意:给你一些线段,找出一条直线可以穿过全部的线段,相交包含端点. 思路:遍历全部的端点,取两个点形成直线,推断直线是否与全部线段相交,假设存在这种直线,输出Yes ...
- POJ 3304 Segments(判断直线与线段是否相交)
题目传送门:POJ 3304 Segments Description Given n segments in the two dimensional space, write a program, ...
- POJ 3304 Segments (判断直线与线段相交)
题目链接:POJ 3304 Problem Description Given n segments in the two dimensional space, write a program, wh ...
- POJ 3304 Segments 基础线段交判断
LINK 题意:询问是否存在直线,使得所有线段在其上的投影拥有公共点 思路:如果投影拥有公共区域,那么从投影的公共区域作垂线,显然能够与所有线段相交,那么题目转换为询问是否存在直线与所有线段相交.判断 ...
- poj 3304 Segments(计算直线与线段之间的关系)
Segments Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10921 Accepted: 3422 Descrip ...
- POJ 3304 Segments (直线和线段相交判断)
Segments Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7739 Accepted: 2316 Descript ...
- poj 3304 Segments
Segments 题意:给你100以内的n条线段,问你是否存在一条直线,使得题给的线段在这条直线上的“投影” 相交于一点: 思路: 1.先要将线段投影相交于一点转变为存在一条直线与所有的线段相交: 很 ...
- 判断直线与线段相交 POJ 3304 Segments
题意:在二维平面中,给定一些线段,然后判断在某直线上的投影是否有公共点. 转化,既然是投影,那么就是求是否存在一条直线L和所有的线段都相交. 证明: 下面给出具体的分析:先考虑一个特殊的情况,即n=1 ...
随机推荐
- 【实习记】2014-09-01从复杂到简单:一行命令区间查重+长整型在awk中的bug
9月1号,导出sql文件后,想到了awk,但很复杂.想到了用sed前期处理+python排序比较的区间查重法.编写加调试用了约3小时. 9月2号,编写C代码的sql语句过程中,发现排序可以交m ...
- phpredis
安装php的redis扩展: http://pecl.php.net/package/redis 也可以用PHP直接连redis: http://www.cnblogs.com/kudosharry/ ...
- java中移位运算符:<<、>>和>>>之间的比较
一.说明 <<:运算符将二进制位进行左移操作 >>:运算符将二进制位进行右移操作 >>>:运算符将用0填空高位 二.举例 /** * *----------c ...
- 【原创】解决国内Android SDK无法更新问题更新
使用代理,推荐使用shadowsock 在SDK Manage的tools-options填好代理 服务器地址127.0.0.1 端口1080
- uploadify 上传文件出现HTTP 404错误
今天在使用jquery.uploadify.js上传文件的时候,出现HTTP 404错误,此错误在上传较小文件时不会出现,在上传一个50M左右文件时出现此错误,经过测试和日志查看发现,根本没有进入后台 ...
- svg学习笔记(二)
SMIL animation演示代码集锦 <svg width="1400" height="1600" xmlns="http://www.w ...
- [转]iOS之浅谈纯代码控制UIViewController视图控制器跳转界面的几种方法
参考:http://www.mamicode.com/info-detail-469709.html 一.最普通的视图控制器UIViewContoller 一个普通的视图控制器一般只有模态跳转的功能( ...
- android 布局权重问题(转载)
//权重和父容器orientation有关 horizontal 指水平方向权重 android:layout_width vertical 指垂直方向权重 android:layout_he ...
- App 性能分析
关键因素: ---- Instrument 性能分析神器 1. 启动时间 应用启动时间长短对用户第一次体验至关重要,同时系统对应用的启动.恢复等状态的运行时间也有严格的要求,在应用超时的情况下系统会直 ...
- Android Camera 预览图像被拉伸变形的解决方法【转】
问题描述: 预览图像被拉伸变形 问题原因: 由于预览图像大小跟SurfaceView 大小不一致引起 解决方法: 获取系统支持的所有预览尺寸[getSupportedPictureSizes],然后再 ...