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.
题目大意:给n条线段,问是否存在一条直线穿过所有线段
思路:题目本身没什么难度,就是枚举所有穿过线段端点的直线即可,难是难在精度的控制。第一个要注意的地方是n=1的时候要特判,第二个要注意的是如果两点过于接近是不能作为枚举直线的,因为这样算出来的叉积过小,很容易小于EPS。注意这两点就没什么大问题了。
#include <cstdio> #include <cmath> #define EPS 1e-10 #define Abs(x) x>0?x:-x typedef double TYPE; //2D point struct Point { TYPE x, y; Point() {} Point(double xx, double yy): x(xx), y(yy) {}; bool operator < (const Point &B) const { if(x == B.x) return y < B.y; else return x < B.x; } }; //if return>0 then a is on the clockwise of b TYPE cross(const Point &a, const Point &b, const Point &o) { return (o.x - a.x) * (o.y - b.y) - (o.x - b.x) * (o.y - a.y); } //distance between a and b inline double dist(Point a, Point b){ double x = a.x - b.x, y = a.y - b.y; return sqrt(x * x + y * y); } //2D twp-point form segment struct Seg { Point st, ed; Seg() {} Seg(Point aa, Point bb):st(aa), ed(bb) {} }; //if a straddle b inline bool straddle(const Seg &A, const Seg &B) { return cross(B.st, A.st, B.ed) * cross(B.st, A.ed, B.ed) < EPS; } #define MAXN 110 int T, n; Seg sg[MAXN]; inline bool check(Seg p) { if(dist(p.st, p.ed) < EPS) return false; for(int i = 0; i < n; ++i) if(!straddle(sg[i], p)) return false; return true; } bool solve() { for(int i = 0; i < n; ++i) for(int j = i+1; j < n; ++j) { if(check(Seg(sg[i].st, sg[j].st))) return true; if(check(Seg(sg[i].st, sg[j].ed))) return true; if(check(Seg(sg[i].ed, sg[j].st))) return true; if(check(Seg(sg[i].ed, sg[j].ed))) return true; } return false; } int main() { scanf("%d", &T); while(T--) { scanf("%d", &n); for(int i = 0; i < n; ++i) scanf("%lf%lf%lf%lf", &sg[i].st.x, &sg[i].st.y, &sg[i].ed.x, &sg[i].ed.y); if(n == 1 || solve()) puts("Yes!"); else puts("No!"); } }