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 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 x2y2 follow, in which (x1, y1) and (x2, y2) are the coordinates of the two endpoints for one of the segments.
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.
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
Yes! Yes! No!
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
//#include<bits/stdc++.h>
#define eps 1e-8
using namespace std;
struct Line
{
double x1,y1;
double x2,y2;
}line[110];
double dis(double x1,double y1,double x2,double y2)
{
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double cross(double x1,double y1,double x2,double y2,double x,double y)
{
return (x2-x1)*(y-y1)-(x-x1)*(y2-y1);
}
int t,n;
bool jude(double x1,double y1,double x2,double y2)
{
if(dis(x1,y1,x2,y2)<eps) return 0;
for(int i=0;i<n;i++)
{
if(cross(x1,y1,x2,y2,line[i].x1,line[i].y1)*
cross(x1,y1,x2,y2,line[i].x2,line[i].y2)>eps)
return 0;
}
return 1;
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%lf%lf%lf%lf",&line[i].x1,&line[i].y1,&line[i].x2,&line[i].y2);
}
if(n<3)
{
printf("Yes!\n");
continue;
}
int flag=0;
for(int i=0;i<n&&!flag;i++)
{
for(int j=i+1;j<n&&!flag;j++)
{
if(jude(line[i].x1,line[i].y1,line[j].x1,line[j].y1)||
jude(line[i].x1,line[i].y1,line[j].x2,line[j].y2)||
jude(line[i].x2,line[i].y2,line[j].x1,line[j].y1)||
jude(line[i].x2,line[i].y2,line[j].x2,line[j].y2))
flag=1;
}
}
if(flag)
printf("Yes!\n");
else
printf("No!\n");
}
}