poj 3304 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 x1y1 x2 y2 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<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<stdlib.h>
using namespace std;
typedef long long ll;
const double eps=1e-8;
struct point{
	double xx,yy;
	point(double x=0,double y=0):xx(x),yy(y) {};
};
double Cross(point A,point B){
    return A.xx*B.yy-A.yy*B.xx;
}
int dcmp(double x){
    if(fabs(x)<eps)
		return 0;
    return x<0?-1:1;
}
point operator +(point A,point B) {return point(A.xx+B.xx,A.yy+B.yy); }
point operator -(point A,point B) {return point(A.xx-B.xx,A.yy-B.yy); }
point operator *(point A,double p) {return point(A.xx*p,A.yy*p); }
point operator /(point A,double p) {return point(A.xx/p,A.yy/p); }
bool operator ==(point A,point B) {return dcmp(A.xx-B.xx)==0&&dcmp(A.yy-B.yy)==0;}  
//-------------------------------------------------------------------------
struct Rectangle{
	point p1;
	point p2;
}a[105];
int n;

int judge(Rectangle t){
	if(t.p1==t.p2)  return 0;//必须要判断两点是否重合。 
	for(int i=0;i<n;i++){
		point v1,v2,v3,v4;
		v1=a[i].p1-t.p1;
		v2=a[i].p1-t.p2;
		
		v3=a[i].p2-t.p1;
		v4=a[i].p2-t.p2;
		if(dcmp(Cross(v1,v2)*Cross(v3,v4))>0)//如果线段的两点在分别直线的两边,则线段与直线一定相交。 
			return 0;
	}
	return 1;
}

int main()
{
	int T,flag;
	Rectangle t1,t2,t3,t4;
	scanf("%d",&T);
	while(T--){
		flag=0;
		scanf("%d",&n);
		for(int i=0;i<n;i++)
			scanf("%lf%lf%lf%lf",&a[i].p1.xx,&a[i].p1.yy,&a[i].p2.xx,&a[i].p2.yy);
		
		if(n==1)  {puts("Yes!");continue;}//必须加 
		
		for(int i=0;i<n;i++){
			for(int j=i+1;j<n;j++){
				t1.p1=a[i].p1;
				t1.p2=a[j].p1;
				
				t2.p1=a[i].p1;
				t2.p2=a[j].p2;
				
				t3.p1=a[i].p2;
				t3.p2=a[j].p1;
				
				t4.p1=a[i].p2;
				t4.p2=a[j].p2;
				
				//t5=a[i]   不需要加  
				if( judge(t1) || judge(t2) || judge(t3) || judge(t4)  )
					flag=1;
			}
		}
		if(flag)
			printf("Yes!\n");
		else
			printf("No!\n");
	}
	return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值