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 ...
随机推荐
- 有向图tarjan算法求连通分量的粗浅讲解、证明, // hdu1269
打算开始重新复习一遍相关算法.对于有向图tarjan算法,通过学习过很多说法,结合自己的理解,下面给出算法自己的观点. 算法总模型是一个dfs,结合一个stack(存放当前尚未形成SCC的点集合),记 ...
- hdu - 3836 Equivalent Sets(强连通)
http://acm.hdu.edu.cn/showproblem.php?pid=3836 判断至少需要加几条边才能使图变成强连通 把图缩点之后统计入度为0的点和出度为0的点,然后两者中的最大值就是 ...
- Maven实战:Pom.xml详解
什么是pom? pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述了项目:包括配置文件:开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的u ...
- codechef Chef and Problems
终于补出这道:一直耽搁到现在 找到一个代码可读性很好的分块temp; 题意:给一个长度为n 的数组 A,Q次询问,区间相等数的最大范围是多少? 数据范围都是10e5; 当然知道分块了: 传统分块看各种 ...
- Access restriction: The method 'CharacterEncoder.encode(byte[])' is not API...
问题描述:Access restriction: The method 'CharacterEncoder.encode(byte[])' is not API... 解决方法:这种错误是eclips ...
- css设置背景图片自适应
CreateTime--2017年12月25日16:36:07 Author:Marydon 控制背景图片100%自适应填充布局 /* 控制背景图片100%自适应填充布局 */ body{ bac ...
- java开始到熟悉60
本次主题:多维数组 1,多维数组的初始话有三种:默认初始化.静态初始化.动态初始化. 这里只讲解静态初始化: 这里以二位数组为例,实际应用中,一维用得最多,二维次之,三维以及三维以上几乎很少使用,而且 ...
- uva 11468 - Substring(AC自己主动机+概率)
题目链接:uva 11468 - Substring 题目大意:给出一些字符和各自字符相应的选择概率.随机选择L次后得到一个长度为L的字符串,要求该字符串不包括随意一个子串的概率. 解题思路:构造AC ...
- javascript读取和改动原型特别须要注意的事儿,由于原型的读写不具有对等性
对于从原型对象继承而来的成员,其读和写具有内在的不正确等性.比方有一个对象A,假设它的原型对象是B.B的原型对象是null.假设我们须要读取A对象的name属性值,那么JS会优先在A中查找.假设找到了 ...
- 【iOS开发系列】cell切割线置顶
废话不多说,直接上代码: /** * tableViewCell切割线左側置顶 < 1 > */ -(void)viewDidLayoutSubviews { if ([_tableVie ...