POJ3304:Segments (几何:求一条直线与已知线段都有交点)
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 x1y1 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条线段,问是否存在直线X,使得这些线段在直线上的投影有公共点。
思路:
转化一下,就是问是是否存在直线X,使得X的垂线L与每个线段都有公共点。如果存在L,那么把其中一些满足条件的L平移,
可以使得它结果两个已知线段的端点。
注意一下:
需要判断是否两点重合,开始我用的方法是fabs(x1-x2)<eps,fabs(y1-y2)<eps则重合,改为dist<=eps则重合就AC了。
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
const double eps=1e-;
int N;
struct Cpoint
{
double x,y;
Cpoint(){}
Cpoint(double xx,double yy):x(xx),y(yy){}
};
struct Cline
{
Cpoint a,b;
Cline(){}
Cline(Cpoint aa,Cpoint bb):a(aa),b(bb){}
};
struct Vector
{
double x,y;
Vector(){}
Vector(double xx,double yy):x(xx),y(yy){}
double friend operator ^(Vector a,Vector b){
return a.x*b.y-b.x*a.y;
}
};
double dist(Cpoint a,Cpoint b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
Cline L[maxn];
bool check(Cpoint w, Cpoint v)
{
if(dist(w,v)<=eps) return false;// 判断两点重合,开始用点直接比较WA了。
Vector base(w.x-v.x,w.y-v.y);
for(int i=;i<=N;i++){
Vector e(L[i].a.x-v.x,L[i].a.y-v.y);
Vector p(L[i].b.x-v.x,L[i].b.y-v.y);
if((base^e)*(base^p)>eps) return false;
} return true;
}
bool find()
{
for(int i=;i<=N;i++)
for(int j=;j<=N;j++){
if(check(L[i].a,L[j].a)) return true;
if(check(L[i].a,L[j].b)) return true;
if(check(L[i].b,L[j].a)) return true;
if(check(L[i].b,L[j].b)) return true;
}
return false;
}
int main()
{
int T; double x,y,xx,yy;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
for(int i=;i<=N;i++){
scanf("%lf%lf%lf%lf",&x,&y,&xx,&yy);
L[i]=Cline(Cpoint(x,y),Cpoint(xx,yy));
}
if(find()) printf("Yes!\n");
else printf("No!\n");
}
return ;
}
POJ3304:Segments (几何:求一条直线与已知线段都有交点)的更多相关文章
- hdu 1086 You can Solve a Geometry Problem too 求n条直线交点的个数
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- C++ 根据两点式方法求直线并求两条直线的交点
Line.h #pragma once //Microsoft Visual Studio 2015 Enterprise //根据两点式方法求直线,并求两条直线的交点 #include"B ...
- poj3304(是否存在一条直线与所有给出线段相交
题意:给出n条线段,问你是否存在一条直线让他与所有线段相交. 思路:枚举两条直线的起点和终点做一条直线,看他是否与所有线段相交. #include<cstdio> #include< ...
- 一条直线上N个线段所覆盖的总长度
原文:http://blog.csdn.net/bxyill/article/details/8962832 问题描述: 现有一直线,从原点到无穷大. 这条直线上有N个线段.线段可能相交. 问,N个线 ...
- Intersecting Lines--POJ1269(判断两条直线的关系 && 求两条直线的交点)
http://poj.org/problem?id=1269 我今天才知道原来标准的浮点输出用%.2f 并不是%.2lf 所以wa了好几次 题目大意: 就给你两个线段 然后求这两个线段所在的 ...
- 求两条直线相交点 AS3代码
,); ,); ,); ,); var p:Point = new Point(); trace(checkPoint()) function checkPoint() { if (p1Start.x ...
- AS3 求两条直线的交点
//粘贴到帧上运行即可 var p1Start:Point = new Point(0,0); var p1End:Point = new Point(50,50); var p2Start:Poin ...
- POJ3304 Segments 【线段直线相交】
题意: 给出n条线段两个端点的坐标,问所有线段投影到一条直线上,如果这些所有投影至少相交于一点就输出Yes!,否则输出No!. 思路: 计算几何.这道题要思考到两点: 1:把问题转化为是否存在一条直线 ...
- poj 3304 判断是否存在一条直线与所有线段相交
Segments Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8579 Accepted: 2608 Descript ...
随机推荐
- 另一篇xtion、kinect选择比较(openni下)
小小Xtion开箱测评!!2012年03月12日 19:52:55 原文:http://page.renren.com/601107241/note/811764499 ASUS Xtion Pro ...
- Linux下使用curl进行http请求(转)
curl在Linux下默认已经安装,Windows需要自行安装. 下载地址:https://curl.haxx.se/download.html Windows离线版本:链接:http://pan.b ...
- Android6.0权限管理以及使用权限该注意的地方
Android 6.0 Marshmallow首次增加了执行时权限管理,这对用户来说,能够更好的了解.控 制 app 涉及到的权限.然而对开发人员来说却是一件比較蛋疼的事情.须要兼容适配,并保证程序功 ...
- vue2.0 常用的 UI 库
1.mint-ui 安装: npm install mint-ui --save 使用: main.js // MintUI组件库 import MintUI from 'mint-ui' impor ...
- 二:redis 的hash类型相关操作
=====================二种:hash类型================== 介绍:redis -> hash是一个string类型的field和value的映射表 hash ...
- [ACM] POJ 3253 Fence Repair (Huffman树思想,优先队列)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25274 Accepted: 8131 Des ...
- C#压缩或解压(rar和zip文件)
/// <summary> /// 解压RAR和ZIP文件(需存在Winrar.exe(只要自己电脑上可以解压或压缩文件就存在Winrar.exe)) /// </summary&g ...
- HashMap源代码学习笔记
HashMap的底层主要是基于数组和链表来实现的,它之所以有相当快的查询速度主要是由于它是通过计算散列码来决定存储的位置. HashMap中主要是通过key的hashCode来计算hash值的 ...
- Java中常见的注解
Java中常见的注解 1.JDK自带的注解@Override @Deprecated @Suppvisewarnings 常见第三方注解 Spring:@Autowired @Service ...
- LZMA C# SDK 结合 UPK 打包压缩 多目录 Unity3d实例
上篇 LZMA C# SDK 子线程压缩与解压缩 Unity3d实例 讲了怎样使用 LZMA C# SDK 来对文件进行压缩与解压,当中提到 对于多目录能够先打包成 UPK 然后再 LZMA 压缩 ...