poj1939(计算几何)

http://poj.org/problem?id=1039

题意:在宽度为1的管里射条光线最远射多长?

思路:看了网上的题解,才明白叉积是能用来求相交的

#include <iostream>
#include <cstdio>
using namespace std;

struct node
{
	double x;
	double y;
};

node a[200];

double max(double a, double b){
	return a>b?a:b;
}

node down(node p){
	p.y--;
	return p;
}

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

bool check(node a, node b, node c, node d){
	double d1 = xmult(a, b, c);
	double d2 = xmult(a, b, d);
	if (d1*d2 <= 0) return true;
	return false;
}

bool checkl(node a, node b, node c, node d){
	double d1 = xmult(a, b, c);
	double d2 = xmult(a, b, d);
	if (d1*d2 >= 0) return false;
	return true;
}

node getPoint(node a, node b, node c, node d){
	node temp = a;
	double t = ((a.x-c.x)*(c.y-d.y)-(a.y-c.y)*(c.x-d.x))/
				((a.x-b.x)*(c.y-d.y)-(a.y-b.y)*(c.x-d.x));
	temp.x += (b.x-a.x)*t;
	temp.y += (b.y-a.y)*t;
	return temp;
}

int main(){
	int n, m, k;
	node t;
	double ans;
	while(cin >> n && n != 0){
		ans = 0;
		for (int i = 0; i < n; i++){
			cin >> a[i].x >> a[i].y;
 		}
 		double ans = a[0].x;

		for (int i = 0; i < n; i++) //up
		for (int j = 0; j < n; j++){
			if (check(a[i], down(a[j]), a[0], down(a[0]))){
				for (k = 1; k < n; k++){
					if (!check(a[i], down(a[j]), a[k], down(a[k]))){
						if (checkl(a[i], down(a[j]), a[k], a[k-1])){
							t = getPoint(a[i], down(a[j]), a[k], a[k-1]);
							ans = max(t.x, ans);
							break;
						}
						if (checkl(a[i], down(a[j]), down(a[k]), down(a[k-1]))){
							t = getPoint(a[i], down(a[j]), down(a[k]), down(a[k-1]));
							ans = max(t.x, ans);
							break;
						}
						ans = max(ans, a[k-1].x);
						break;
					}
				}
				if (k == n){
					ans = a[n-1].x+1;
				}
			
			}
		}
		if (ans >= a[n-1].x){
			printf("Through all the pipe.\n");
		}else {
			printf("%.2f\n", ans);
		}
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值