POJ 3304 Segments (计算几何、判断直线与线段是否相交)

题目链接:http://poj.org/problem?id=3304


题意:

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


看样例,每组数据第一行n表示n条线段,下面n行每行给出一条线段。问是否存在一条直线使得所有线段在这条直线上的投影至少有一点重合。


参考博客:http://blog.sina.com.cn/s/blog_6635898a0100n2lv.html


思路:计算几何。这道题要思考到两点:
1:把问题转化为是否存在一条直线与每条线段都有交点。证明:若存在一条直线l和所有线段相交,作一条直线m和l垂直,则m就是题中要求的直线,所有线段投影的一个公共点即为垂足。
2:枚举两两线段的各一个端点,连一条直线,再判断剩下的线段是否都和这条直线有交点。证明:若有l和所有线段相交,则可保持l和所有线段相交,左右平移l到和某一线段交于端点停止(“移不动了”)。然后绕这个交点旋转。也是转到“转不动了”(和另一线段交于其一个端点)为止。这样就找到了一个新的l满足题意,而且经过其中两线段的端点。
ps:要学会这种从结果反过来证的思考方法....
我的判断线段与直线l是否相交的方法:
1:利用叉积的性质,判断线段的两个端点是否在直线的两边。
2:求线段所在的直线tmp,求tmp与l的交点p,由线段两端点到p的距离之和,与线段的距离比较,若相等则证明线段与直线相交。


抄模板:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
const double eps = 1e-8;
const double PI = acos(-1.0);
int sgn(double x) {
	if(fabs(x) < eps)return 0;
	if(x < 0)return -1;  
	else return 1; 
}
struct Point {  
	double x,y;
	Point(){}  
	Point(double _x,double _y) {   
		x = _x;y = _y;  
	}  
	Point operator -(const Point &b)const {
	   return Point(x - b.x,y - b.y);  
	}  
	double operator ^(const Point &b)const {
	   return x*b.y - y*b.x;  
	}    
	double operator *(const Point &b)const {   
		return x*b.x + y*b.y;  
	}  
	void transXY(double B) {   
		double tx = x,ty = y;   
		x = tx*cos(B) - ty*sin(B);   
		y = tx*sin(B) + ty*cos(B);  
	} 
};

struct Line {  
	Point s,e; 
	Line(){}  
	Line(Point _s,Point _e)  {
	   s = _s;e = _e;  
	} 
	pair<int,Point> operator &(const Line &b)const {
		Point res = s;
		if(sgn((s-e)^(b.s-b.e)) == 0) {
			if(sgn((s-b.e)^(b.s-b.e)) == 0) return make_pair(0,res);             
			else return make_pair(1,res);         
		} 
        double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
		res.x += (e.x-s.x)*t;         
		res.y += (e.y-s.y)*t;         
		return make_pair(2,res);     
	} 
};

double dist(Point a,Point b) {     
	return sqrt((a-b)*(a-b)); 
}

bool Seg_inter_line(Line l1,Line l2) {  //判断直线l1是否与线段l2相交 
	return sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= 0;
}

int main() {
	int t;
	scanf("%d", &t);
	while(t--) {
		vector<Line> line;
		vector<Point> point;
		int n;
		scanf("%d", &n);
		int i, j, k;
		for(i = 0; i < n; i++) {
			Point p1, p2;
			scanf("%lf %lf %lf %lf", &p1.x, &p1.y, &p2.x, &p2.y);
			point.push_back(p1);
			point.push_back(p2);
			line.push_back(Line(p1, p2));
		}
		int flag = 0;
		for(i = 0; i < point.size() && !flag; i++) {
			for(j = i + 1; j < point.size() && !flag; j++) { //枚举端点 
				if(dist(point[i], point[j]) < eps) continue;  //判断重点 
				Line l(point[i], point[j]);
				flag = 1;
				for(k = 0; k < line.size(); k++) {
					if(Seg_inter_line(l, line[k]) == 0) {
						flag = 0;
						break;	
					}
				}
			}
		}
		if(flag) puts("Yes!");
		else puts("No!");
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值