Segments POJ3304(计算几何+叉乘的应用)

SegmentsPOJ - 3304

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 (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 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!
分析:
1.如果有一条直线通过所有的线段,那么这条直线的垂线就是答案

2.在所有线段端点所确定的直线里面,一定存在答案,如果不存在,那么就输出“No!”

关于上面两点简单证明一下:

1.垂足就满足题目条件

2.假设有一条通过所有线段的直线,那么小心的平移之,总可以与一个线段的端点重合,那么以这个端点为中心,小心的旋转直线,总可以正好过另一个端点,可能是这条线段的,也很可能是其他线段的,那么这个时候,这条仍然过所有线段的直线就过两个端点了

枚举思路:

先枚举同一条线段上的两个端点,在枚举两条线段上的端点的组合,而且注意如果两个端点是重点(横纵坐标都小于1e-8),那么就放掉这种情况,因为一个点是没办法确定一条直线

代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const double eps=1e-8;
struct Vector{
	double x,y;
	Vector(){}
	Vector(double x,double y):x(x),y(y){}
};
typedef Vector point;
Vector operator + (Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);};
Vector operator - (Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);};
Vector operator * (Vector a,Vector b){return Vector(a.x*b.x,a.y*b.y);};
Vector operator / (Vector a,Vector b){return Vector(a.x/b.x,a.y/b.y);};
double dot(Vector a,Vector b){
	return a.x*b.x+a.y*b.y;
}
double cross(Vector a,Vector b){
	return a.x*b.y-a.y*b.x;
}
struct segment{
	point p1,p2;
	segment(){}
	segment(point p1,point p2):p1(p1),p2(p2){} 
}ss[110];
int main(){
	int t;cin>>t;
	while(t--){
		int n;cin>>n;
		for(int i=0;i<n;i++){
			cin>>ss[i].p1.x>>ss[i].p1.y>>ss[i].p2.x>>ss[i].p2.y;
		}
		int flag=0;
		for(int i=0;i<n;i++){
			if(abs(ss[i].p1.x-ss[i].p2.x)<eps&&abs(ss[i].p1.y-ss[i].p2.y)<eps)continue;
			flag=1;
			Vector v=ss[i].p1-ss[i].p2;
			for(int j=0;j<n;j++){
				if(j==i)continue;
				Vector v1=ss[j].p1-ss[i].p2,v2=ss[j].p2-ss[i].p2;
				if(cross(v1,v)*cross(v2,v)>eps){
					flag=0;
					break;
				}
			} 
		}
		if(flag){
			cout<<"Yes!"<<endl;
			continue;
		}
		for(int i=0;i<n;i++){
			for(int j=i+1;j<n;j++){
				point p11=ss[i].p1,p12=ss[i].p2,p21=ss[j].p1,p22=ss[j].p2;
				if(abs(p11.x-p21.x)>eps&&abs(p11.y-p21.y)>eps){
					Vector v=p11-p21;
					flag=1;
					for(int k=0;k<n;k++){
						if(k==i||k==j)continue;
						Vector v1=ss[k].p1-p21,v2=ss[k].p2-p21;
						if(cross(v1,v)*cross(v2,v)>eps){
							flag=0;
							break;
						}
					}
					if(flag)break;
				}
				if(abs(p11.x-p22.x)>eps&&abs(p11.y-p22.y)>eps){
					Vector v=p11-p22;
					flag=1;
					for(int k=0;k<n;k++){
						if(k==i||k==j)continue;
						Vector v1=ss[k].p1-p22,v2=ss[k].p2-p22;
						if(cross(v1,v)*cross(v2,v)>eps){
							flag=0;
							break;
						}
					}
					if(flag)break;
				}
				if(abs(p12.x-p21.x)>eps&&abs(p12.y-p21.y)>eps){
					Vector v=p12-p21;
					flag=1;
					for(int k=0;k<n;k++){
						if(k==i||k==j)continue;
						Vector v1=ss[k].p1-p21,v2=ss[k].p2-p21;
						if(cross(v1,v)*cross(v2,v)>eps){
							flag=0;
							break;
						}
					}
					if(flag)break;
				}
				if(abs(p12.x-p22.x)>eps&&abs(p12.y-p22.y)>eps){
					Vector v=p12-p22;
					flag=1;
					for(int k=0;k<n;k++){
						if(k==i||k==j)continue;
						Vector v1=ss[k].p1-p22,v2=ss[k].p2-p22;
						if(cross(v1,v)*cross(v2,v)>eps){
							flag=0;
							break;
						}
					}
					if(flag)break;
				}
			}
			if(flag)break;
		}
		if(flag){
			cout<<"Yes!"<<endl;
			continue;
		}
		cout<<"No!"<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41333528/article/details/80529713