Segments

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 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!
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

#define MAX(a,b) ((a)>(b) ? (a):(b))
#define MIN(a,b) ((a)<(b) ? (a):(b))

int n;

struct point{
	double x,y;
};

struct line{
	point a,b;
}lin[110];

double judge(point a,point b,point c)
{
	return (b.x-a.x)*(c.y-b.y)-(c.x-b.x)*(b.y-a.y);
}

double dis(point a,point b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

bool intersect(double x,double y,double x1,double y1)
{
	point a,b;
	a.x=x; a.y=y; b.x=x1; b.y=y1;
	if(dis(a,b)<1e-8) return 0;
	for(int i=0;i<n;i++)
	{
		if(judge(a,b,lin[i].a)*judge(a,b,lin[i].b)>0) return 0;
	}
	return 1;
}

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int i,j,flag=1;
		scanf("%d",&n);
		for(i=0;i<n;i++) scanf("%lf%lf%lf%lf",&lin[i].a.x,&lin[i].a.y,&lin[i].b.x,&lin[i].b.y);
		if(n==1) {printf("Yes!\n"); continue;}
		for(i=0;i<n&&flag;i++)
		 for(j=i+1;j<n&&flag;j++)
		 {
			if(intersect(lin[i].a.x,lin[i].a.y,lin[j].a.x,lin[j].a.y)||
			   intersect(lin[i].a.x,lin[i].a.y,lin[j].b.x,lin[j].b.y)||
			   intersect(lin[i].b.x,lin[i].b.y,lin[j].a.x,lin[j].a.y)||
			   intersect(lin[i].b.x,lin[i].b.y,lin[j].b.x,lin[j].b.y))
			{
				printf("Yes!\n");
				flag=0;
			}
		 }
		if(flag) printf("No!\n");
	}
	return 0;
}


發佈了105 篇原創文章 · 獲贊 3 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章