首页 > 代码库 > POJ 3304 segments 线段和直线相交
POJ 3304 segments 线段和直线相交
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14178 | Accepted: 4521 |
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 integern ≤ 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!
Source
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<sstream> #include<algorithm> #include<queue> #include<deque> #include<iomanip> #include<vector> #include<cmath> #include<map> #include<stack> #include<set> #include<fstream> #include<memory> #include<list> #include<string> using namespace std; typedef long long LL; typedef unsigned long long ULL; #define MAXN 103 #define N 21 #define MOD 1000000 #define INF 1000000009 const double eps = 1e-8; const double PI = acos(-1.0); /* 所有线段投射到给定线段上取交集,如果交集距离大于eps 存在!s */ int sgn(double x) { if (fabs(x) < eps) return 0; if (x < 0) return -1; else return 1; } struct Point { double x, y; Point() {} Point(double _x, double _y) :x(_x), y(_y) {} Point operator - (const Point& r)const { return Point(x - r.x, y - r.y); } double operator ^(const Point& r)const { return x*r.y - y*r.x; } double operator * (const Point& r)const { return x*r.x + y*r.y; } }; double dist(Point a, Point b) { return sqrt((a - b)*(a - b)); } struct Line { Point s, e; Line() {} Line(Point _a, Point _B) :s(_a), e(_B) {} }; vector<Line> v; bool Seg_inter_line(Line l1, Line l2) { return sgn((l2.s - l1.e) ^ (l1.s - l1.e))*sgn((l2.e - l1.e) ^ (l1.s - l1.e)) <= 0; } bool check(Line l) { if (sgn(dist(l.s, l.e)) == 0) return false; for (int i = 0; i < v.size(); i++) if (!Seg_inter_line(l, v[i])) return false; return true; } int main() { int T, n; scanf("%d", &T); while (T--) { v.clear(); double x1, y1, x2, y2; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2); v.push_back(Line(Point(x1, y1), Point(x2, y2))); } bool f = false; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (check(Line(v[i].s, v[j].s)) || check(Line(v[i].s, v[j].e)) || check(Line(v[i].e, v[j].s)) || check(Line(v[i].e, v[j].e))) { f = true; break; } } } if (f) printf("Yes!\n"); else printf("No!\n"); } return 0; }
POJ 3304 segments 线段和直线相交