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);
}
}
}