poj 3304 Segments(计算几何基础)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 3304 Segments(计算几何基础)相关的知识,希望对你有一定的参考价值。

 

 
Segments

 

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11593   Accepted: 3657

 

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!

 

Source

 

 

【题意】

  给出n条线段两个端点的坐标,问所有线段投影到一条直线上,如果这些所有投影至少相交于一点就输出Yes!,否则输出No!。
【思路】

  如果有存在这样的直线,过投影相交区域作直线的垂线,该垂线必定与每条线段相交,问题转化为问是否存在一条线和所有线段相交
  若存在一条直线与所有线段相机相交,此时该直线必定经过这些线段的某两个端点,所以枚举任意两个端点即可

 

 

【代码】

 

 1 #include<cmath>
 2 #include<cstdio>
 3 #include<cstring>
 4 #define FOR(a,b,c) for(int a=(b);a<(c);a++)
 5 using namespace std;
 6 
 7 const int N = 100+10;
 8 const double eps = 1e-8;
 9 
10 int dcmp(double x) {
11     if(x<eps) return 0; else return x<0? -1:1;
12 }
13 
14 struct Line {
15     double x1,y1,x2,y2;
16 }L[N];
17 
18 int n;
19 
20 double cross(double x1,double y1,double x2,double y2,double x,double y) {
21     return (x2-x1)*(y-y1)-(x-x1)*(y2-y1);
22 }
23 bool judge(double x1,double y1,double x2,double y2) {
24     if(!dcmp(x2-x1) && !dcmp(y2-y1)) return 0;
25     FOR(i,0,n) {
26         if(cross(x1,y1,x2,y2,L[i].x1,L[i].y1)*
27             cross(x1,y1,x2,y2,L[i].x2,L[i].y2)>eps) return 0;
28     }
29     return 1;
30 }
31 
32 int main() {
33     int T;
34     scanf("%d",&T);
35     while(T--) {
36         scanf("%d",&n);
37         FOR(i,0,n) 
38             scanf("%lf%lf%lf%lf",&L[i].x1,&L[i].y1,&L[i].x2,&L[i].y2);
39         if(n==1) { puts("Yes!"); continue; }
40         bool ans=0;
41         FOR(i,0,n) {
42             FOR(j,i+1,n) {
43                 if(judge(L[i].x1,L[i].y1,L[j].x1,L[j].y1)) ans=1;
44                 if(judge(L[i].x1,L[i].y1,L[j].x2,L[j].y2)) ans=1;
45                 if(judge(L[i].x2,L[i].y2,L[j].x1,L[j].y1)) ans=1;
46                 if(judge(L[i].x2,L[i].y2,L[j].x2,L[j].y2)) ans=1;
47                 if(ans) break;
48             }
49             if(ans) break;
50         }
51         if(ans) puts("Yes!");
52         else puts("No!");
53     }
54     return 0;
55 }

 

以上是关于poj 3304 Segments(计算几何基础)的主要内容,如果未能解决你的问题,请参考以下文章

POJ 3304 Segments [计算几何,判断直线和线段相交]

POJ 3304 Segments(计算几何:直线与线段相交)

poj3304 Segments

POJ3304 Segments 线段直线相交

POJ 3304 Segments 基础线段交判断

POJ 3304 Segments(判断直线与线段是否相交)