poj 3304 Segments(叉积+直线和线段相交判断)
来源:互联网 发布:数据可视化怎么做 编辑:程序博客网 时间:2024/05/21 06:31
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个线段,问是否存在一条直线,使得这n条线段在该直线上的投影存在至少一个公共点?
思路:假设存在,那么从该公共点出发做一条与该直线垂直的直线,这条直线必定与这些所有线段相交。所以我们就可以将问题转化为求是否存在一条直线与这些所有线段相交。n<=100,所以我们可以去枚举所有线段的两个端点假设为所求直线,然后用叉积去判断该直线是否与所有线段相交。
注意:可能会枚举到重合的点,特殊考虑下。
代码如下
#include<iostream>#include<set>#include<vector>#include<algorithm>#include<cstring>#include<cstdio>#include<cmath>using namespace std;typedef long long ll;const int N=105;const double eps=1e-8;int n;int dcmp(double x) { if(fabs(x)<eps) return 0; return x<0?-1:1;}struct Point { double x,y; Point() {} Point(double _x,double _y) { x=_x; y=_y; } Point operator-(const Point &b) const { return Point(x-b.x,y-b.y); } double operator *(const Point &b)const { return x*b.x + y*b.y; } double operator ^(const Point &b)const { return x*b.y - y*b.x; }};struct Line { Point a,b; Line() {} Line(Point _a,Point _b) { a=_a; b=_b; }};Line line[N];double dist(Point a,Point b){ return sqrt((b-a)*(b-a));//返回两个点之间的距离 } double xmult(Point p0,Point p1,Point p2) { return (p1-p0)^(p2-p0);}bool check(Line L1,Line L2)//判断直线L1和线段L2是否相交 { return dcmp(xmult(L2.a,L1.a,L1.b))*dcmp(xmult(L2.b,L1.a,L1.b))<=0;}bool solve(Line L1){ if(dcmp(dist(L1.a,L1.b))==0) return false;//如果所选的两个点重合,即不能构成一条直线 for(int i=0;i<n;i++) if(check(L1,line[i])==false) return false; return true; }int main() { int T; scanf("%d",&T); while(T--) { scanf("%d",&n); double x1,y1,x2,y2; for(int i=0;i<n;i++) { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); line[i]=Line(Point(x1,y1),Point(x2,y2)); } int f=0; for(int i=0;i<n;i++) if(!f) for(int j=0;j<n;j++) { if(solve(Line(line[i].a,line[j].a))||solve(Line(line[i].a,line[j].b))|| solve(Line(line[i].b,line[j].a))||solve(Line(line[i].b,line[j].b))) { f=1; break; } } if(f) printf("Yes!\n"); else printf("No!\n"); } return 0;}
- poj 3304 Segments(叉积+直线和线段相交判断)
- poj 3304 Segments(判断线段和直线相交)
- POJ 3304 Segments(判断线段和直线是否相交)
- POJ 3304 Segments(判断直线和线段相交)
- POJ 3304 Segments (直线和线段相交判断)
- POJ 3304 Segments (直线和线段相交判断)
- POJ 3304 Segments (判断直线和线段是否相交)
- POJ 3304 Segments [判断线段和直线相交]
- poj 3304 Segments--直线和线段相交
- poj 3304 Segments(判断直线与线段相交)
- POJ 3304 Segments 判断直线与线段相交
- POJ 3304 - Segments【计算几何 - 直线线段相交判断】
- poj-3304-Segments-线段与直线相交
- POJ 3304 Segments(线段直线相交)
- POJ 3304 || Segments (线段,直线相交
- poj 3304 Segments 线段与直线相交
- Poj 3304 Segments 【线段于直线相交】
- 【线段直线相交】POJ 3304 Segments
- 第一天开博客,随便写点啥
- Linux下 环境变量/etc/profile、/etc/bashrc、~/.bashrc的区别
- Java练习题10
- web阶段总结-课堂笔记
- 设计模式【桥接模式Bridge Pattern】
- poj 3304 Segments(叉积+直线和线段相交判断)
- 本次考试总结(2017.10.8比赛)
- java memcached客户端,模糊查询/批量删除/查询所有的key
- 上课 第五周
- AUTO ML and Bayes Op
- Bootstrap-课堂笔记
- 直接插入排序及其优化
- PAT 甲级 1105. Spiral Matrix (25)
- java类集--排序及重复元素说明