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 x1y1x2y2 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<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const double eps=1e-8;
bool f;
int n;
struct point
{
double x,y;
};
struct vector
{
point start,end;
};
point p;
vector line[1000];
double multi(point p1,point p2,point p0)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
bool fun(point a,point b)
{
if(fabs(a.x-b.x)<eps&&fabs(a.y-b.y)<eps)
return false;
for(int i=0;i<n;i++)
{
if(multi(line[i].start,a,b)*multi(line[i].end,a,b)>eps)
return false;
}
return true;
}
int main()
{
int t;
cin>>t;
while(t--)
{
f=false;
cin>>n;
for(int i=0;i<n;i++)
cin>>line[i].start.x>>line[i].start.y>>line[i].end.x>>line[i].end.y;
if(n<3)
f=true;
for(int i=0;i<n&&!f;i++)
{
// if(fun(line[i].end,line[i].start))
// f=true;
for(int j=i+1;j<n&&!f;j++)
if(fun(line[i].start,line[j].end)||
fun(line[i].end,line[j].start)||
fun(line[i].end,line[j].end)||
fun(line[i].start,line[j].start))
f=true;
}
if(!f)
printf("No!\n");
else
printf("Yes!\n");
}
return 0;
}