POJ - 3304 -- Segments【直线与线段相交 】

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 x1 y1 x2 y2 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!

题意:

给我们n条线段,问是否存在一条直线,将这些线段投影到直线上有至少有一个公共的交点。

思路:

我们可以转化思考,构造一条直线,如果这条直线交于全部的线段,那么这个投影点就是这些交点,而这条直线必定经过这些线段得两个端点。枚举任意两个端点,判断是否存在与全部线段相交即可。
1.判断直线(ab)与线段(cd)相交
在这里插入图片描述
根据上面的两种情况,我们直接可以用向量的叉积来判断是否相交了。
2.为什么这条直线必定经过这些线段上的两个端点呢?
在这里插入图片描述
我们假设将这些线段旋转平移成这个样子,要使得这个投影点O一直存在,那么随便平移,不能超过两个端点。

AC代码
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define eps 1e-8 
#define inf 99999999
const int maxn = 105;
struct Point{
	double x,y;
	Point(double x=0,double y=0):x(x),y(y){}
}p[maxn];
typedef Point Vector;

int dcmp(double x){
	if(fabs(x)<eps) 
		return 0;
	else
		return x<0?-1:1;
}
Vector operator + (Vector A, Vector B) {return Vector(A.x+B.x,A.y+B.y);}
Vector operator - (Point A, Point B) {return Vector(A.x-B.x,A.y-B.y);}
Vector operator * (Vector A, double p) {return Vector(A.x*p,A.y*p);}
Vector operator / (Vector A, double p) {return Vector(A.x/p,A.y/p);}
bool operator == (const Point& a, const Point& b){return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0;}

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 Line{
	Point v,p;
	Line(){}
	Line(Point v,Point p):v(v),p(p){}
	Point point(double t) {
        return v+(p-v)*t;
    }
};
typedef Line Segment;
Segment line[maxn];

bool LineIntersection(Point a, Point b, Point c, Point d){//直线ab与线段cd相交 
	return Cross(b - a, c - a) * Cross(b - a, d - a) <= 0;
} 
bool cheak(Point a, Point b, int n){
	for (int i = 0; i < n; ++i){
		if(! LineIntersection(a, b, line[i].v, line[i].p))	return false;
	}
	return true;
}
void solve(){
	int n, cnt = 0;
	double x1, y1, x2, y2;
	Point a,b;
	scanf("%d", &n);
	for (int i = 0; i < n; ++i){
		scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
		p[cnt++] = Point(x1, y1);
		p[cnt++] = Point(x2, y2);
		line[i] = Line(Point(x1, y1), Point(x2, y2));
	}
	bool flag = false;
	for (int i = 0; i < cnt; ++i){
		flag = false;
		for (int j = i + 1; j < cnt; ++j){
			if (p[i] == p[j])	continue;
			if (cheak(p[i], p[j], n))	flag = true;
			if (flag)	break; 
		}
		if (flag)	break;
	} 
	if (flag)	puts("Yes!");
	else	puts("No!");
}
int main(){
	int t;
	scanf("%d", &t);
	for (int i = 0; i < t; ++i)	solve();
	return 0;
}
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星空皓月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值