poj3304(计算几何+线段求交)

Segments
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8639 Accepted: 2631

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 integern ≤ 100 showing the number of segments. After that,n lines containing four real numbersx1y1x2y2 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 numbersa andb 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!

Source

 
    本题要求是否存在所有线段投影到某条直线上都有公共点。本题如果直接找投影直线再看所有线段在该直线上是否有公共点是很难的。可以换种思维。如果知道直线和公共点的集合(线段
 
或点),就可知过该点垂直于该直线的直线line应与所有直线相交。可以转换为找直线line,该直线的极限情况是两个端点都是线段的端点。注意重点的条件。
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;

const int MAXN=100+10;
int n;
const double eps=1e-8;

struct point
{
	double x,y;
	point()
	{};
	point(double _x,double _y)
	{
		x=_x;
		y=_y;
	}
};

struct line
{
	point st,en;
	line(){}
	line(point _st,point _en)
	{
		st=_st;
		en=_en;
	}
}MyLine[MAXN];


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

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

bool Check(point a,point b)
{
	if(GetDistance(a,b)<eps)
		return false;
	for(int i=0;i<n;i++)
	{
		if((IsLeft(a,b,MyLine[i].st)*IsLeft(a,b,MyLine[i].en))>eps)
			return false;
	}
	return true;
}

int main ()
{
	int cas;
	double x1,y1,x2,y2; 
	cin>>cas;
	while(cas--)
	{
		scanf("%d",&n);
		for(int i=0;i<n;i++)
		{
			scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
			MyLine[i]=line(point(x1,y1),point(x2,y2));
		}
		bool flag=false;
		if(1==n)
			flag=true;
		else
		{
			for(int i=0;i<n;i++)
			{
				for(int j=i+1;j<n;j++)
				{
					if(Check(MyLine[i].st,MyLine[i].en)||Check(MyLine[j].st,MyLine[i].en)
						||Check(MyLine[i].st,MyLine[j].en)||Check(MyLine[j].st,MyLine[j].en)
						||Check(MyLine[i].st,MyLine[j].st)||Check(MyLine[i].en,MyLine[j].en)
						)
					{
						flag=true;
						break;
					}
				}
				if(flag)
					break;
			}
		}
		if(flag)
			printf("Yes!\n");
		else 
			printf("No!\n");
	}
	system("pause");
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值