【POJ - 3304 Segments】 直线线段相交判断
2017-08-24 16:20
441 查看
C - 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个线段,问是否存在一条直线,使得所有线段在直线上的投影有交集。
分析:题意可以进行一个很奇妙的转化。如果存在这样的一条直线,那么过这条直线做垂线,这条垂线一定和所有线段相交。可以简单地在纸上画一下试试,画出三条线段,找到一条与这三条线段相交的直线,做这条直线的垂线,会发现所有线段的投影都在那条垂线上,那条垂线就是我们要找到的。那么这条直线一定经过两个端点,所以我们枚举端点,判断和所有的直线相交情况,注意要把重合的点去掉。
代码如下:
#include <map> #include <cmath> #include <queue> #include <stack> #include <vector> #include <cstdio> #include <string> #include <cstring> #include <iostream> #include <algorithm> #define LL long long using namespace std; const int MX = 105; const int mod = 1e9 + 7; const int 4000 INF = 2e9 + 5; const double eps = 1e-8; int n; struct Point{ double x, y; Point(){} Point(double _x, double _y){ x = _x; y = _y; } }; struct Line{ Point s, e; Line(){} Line(Point x, Point y){ s = x; e = y; } }line[MX]; int sgn(double x){ if(fabs(x) < eps) return 0; if(x < 0) return -1; return 1; } double multi(Point p0, Point p1, Point p2){ return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y); } double dis(Point a, Point b){ return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } bool intersect(Line l1, Line l2){ return sgn(multi(l2.s, l1.s, l1.e)) * sgn(multi(l2.e, l1.s, l1.e)) <= 0; } int check(Line l){ if(sgn(dis(l.e, l.s)) == 0) return 0; for(int i = 0; i < n; i++){ if(!intersect(l, line[i])) return 0; } return 1; } 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 flag = 0; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ if(check(Line(line[i].s, line[j].s)) || check(Line(line[i].s, line[j].e)) || check(Line(line[i].e, line[j].s)) || check(Line(line[i].e, line[j].e))){ flag = 1; break; } } if(flag) break; } if(flag) printf("Yes!\n"); else printf("No!\n"); } return 0; }
相关文章推荐
- POJ 3304 Segments(判断直线和线段相交)
- poj 3304 Segments(叉积+直线和线段相交判断)
- poj 3304 Segments(判断线段和直线相交)
- poj 3304 Segments(判断直线与线段相交)
- POJ 3304 Segments (直线和线段相交判断)
- Segments - POJ 3304 (判断直线与线段是否相交)
- 判断直线与线段相交 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 [判断线段和直线相交]