Segments
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15335 | Accepted: 4862 |
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
321.0 2.0 3.0 4.04.0 5.0 6.0 7.030.0 0.0 0.0 1.00.0 1.0 0.0 2.01.0 1.0 2.0 1.030.0 0.0 0.0 1.00.0 2.0 0.0 3.01.0 1.0 2.0 1.0
Sample Output
Yes!Yes!No!
Source
1 //2017-08-30 2 #include3 #include 4 #include 5 #include 6 #include 7 8 using namespace std; 9 10 const int N = 110; 11 const double EPS = 1e-8; 12 13 struct Point{ 14 double x, y; 15 Point(){} 16 Point(double _x, double _y):x(_x), y(_y){} 17 Point(const Point &p):x(p.x), y(p.y){} 18 //a-b为向量ba 19 Point operator- (const Point &b) const { 20 return Point(x-b.x, y-b.y); 21 } 22 //向量叉积 23 double operator^ (const Point &b) const { 24 return x*b.y - y*b.x; 25 } 26 //向量点积 27 double operator* (const Point &b) const { 28 return x*b.x + y*b.y; 29 } 30 }; 31 32 struct Line{ 33 Point a, b; 34 Line(){} 35 Line(Point _a, Point _b):a(_a), b(_b){} 36 void set_a_b(const Point &_a, const Point &_b){ 37 a = _a; 38 b = _b; 39 } 40 }seg[N]; 41 42 //三态函数 43 int sgn(double x){ 44 if(fabs(x) < EPS)return 0; 45 if(x < 0)return -1; 46 else return 1; 47 } 48 49 //input:Point a;Point b 50 //output:distance a、b两点间的距离 51 //note:该函数取名distance会编译错误 52 double dist(Point a, Point b){ 53 return sqrt((a-b)*(a-b)); 54 } 55 56 //input:l1 直线l1;l2 线段l2 57 //output:true 直线l1与线段l2相交;false 直线l1与线段l2不想交 58 bool seg_inter_line(Line l1, Line l2){ 59 return sgn((l2.a-l1.b)^(l1.a-l1.b))*sgn((l2.b-l1.b)^(l1.a-l1.b)) <= 0; 60 } 61 62 int n; 63 //input:直线line 64 //output:true 直线与所有线段都相交 65 bool all_inter(Line line){ 66 for(int i = 0; i < n; i++) 67 if(!seg_inter_line(line, seg[i])) 68 return false; 69 return true; 70 } 71 72 bool check(){ 73 Line line; 74 for(int i = 0; i < n; i++){ 75 for(int j = 0; j < n; j++){ 76 if(i == j)continue; 77 if(dist(seg[i].a, seg[j].a) > EPS){ 78 line.set_a_b(seg[i].a, seg[j].a); 79 if(all_inter(line))return true; 80 } 81 if(dist(seg[i].a, seg[j].b) > EPS){ 82 line.set_a_b(seg[i].a, seg[j].b); 83 if(all_inter(line))return true; 84 } 85 if(dist(seg[i].b, seg[j].a) > EPS){ 86 line.set_a_b(seg[i].b, seg[j].a); 87 if(all_inter(line))return true; 88 } 89 if(dist(seg[i].b, seg[j].b) > EPS){ 90 line.set_a_b(seg[i].b, seg[j].b); 91 if(all_inter(line))return true; 92 } 93 } 94 } 95 return false; 96 } 97 98 int main() 99 {100 std::ios::sync_with_stdio(false);101 //freopen("inputC.txt", "r", stdin);102 int T;103 cin>>T;104 while(T--){105 cin>>n;106 for(int i = 0; i < n; i++){107 cin>>seg[i].a.x>>seg[i].a.y>>seg[i].b.x>>seg[i].b.y;108 }109 if(check() || n == 1 || n == 2)cout<<"Yes!"<