poj3304 Segments

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 (x1y1) and (x2y2) 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;
 } 

你可能感兴趣的:(poj3304 Segments)