Pipe(poj1039线段相交)

题意:给你一个管道,看是光线是否从管道射出,管线没有反射,不能接触管道的边,但是能接触拐点

思路:光线穿过管道一定是经过两个拐点,一个上拐点,一个下拐点,所以枚举所有拐点,如果该直线可以穿过管道那么上方的点一定在直线的左边,下放的点一定在直线的右边,所以枚举每两个点,判断有没有上方的点在直线的右边,下方的点有没有在直线的左面,有则说明直线和管道有交点,求出交点,最后取最大值,如果线段在该线段之前的线段相交怎么说明这条直线不合法,一直枚举到最后

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct Point
{
    double x,y;
    Point(double x = 0,double y = 0):x(x),y(y){}
};
typedef Point Vector;
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,double p) { return Vector(a.x*p,a.y*p) ;}
Vector operator / (Vector a,double p) { return Vector(a.x/p,a.y/p) ;}
double Dot(Vector a,Vector b) { return a.x*b.x + a.y*b.y ;}
double Length(Vector a) { return sqrt(Dot(a,a)) ;}
double Cross(Vector a, Vector b) { return a.x*b.y - a.y*b.x ;}
const double eps = 1e-8;
int dcmp(double x)
{
    if(fabs(x) < eps) return 0;
    else return x < 0 ? -1 : 1;
}
bool operator == (Point a,Point b)
{
    return dcmp(a.x-b.x) == 0&& dcmp(a.y-b.y) == 0;
}
bool operator < (Point a,Point b)
{
    return a.x < b.x || (a.x == b.x && a.y < b.y);
}


bool Onsegment(Point p,Point a,Point b)
{
    return dcmp(Cross(b-a,p-a)) == 0 && dcmp(Dot(b-p,a-p)) < 0 || (p == a) || (p == b);
}

bool OnLine(Point p,Point a,Point b)
{
    return fabs(Cross(p-a,a-b)) / Length(b-a);
}

bool SegmentIntersection(Point a,Point b,Point c,Point d)
{
    double d1 = Cross(b-a,c-a),d2 = Cross(b-a,d-a),d3 = Cross(d-c,a-c),d4 = Cross(d-c,b-c);
    if(dcmp(d1)*dcmp(d2) < 0 || dcmp(d3)*dcmp(d4) < 0) return true;
    else if(dcmp(d1) == 0 && !OnLine(c,a,b) ) return true;
    else if(dcmp(d2) == 0 && !OnLine(d,a,b) ) return true;
    else if(dcmp(d3) == 0 && !OnLine(a,c,d) ) return true;
    else if(dcmp(d4) == 0 && !OnLine(b,c,d) ) return true;
    else return false;
}

bool Segmentsection(Point a,Point b,Point c,Point d)
{
    double d1 = Cross(b-a,c-a),d2 = Cross(b-a,d-a),d3 = Cross(d-c,a-c),d4 = Cross(d-c,b-c);
    if(dcmp(d1)*dcmp(d2) < 0 && dcmp(d3)*dcmp(d4) < 0) return true;
    else if(dcmp(d1) == 0 && Onsegment(c,a,b) ) return true;
    else if(dcmp(d2) == 0 && Onsegment(d,a,b) ) return true;
    else if(dcmp(d3) == 0 && Onsegment(a,c,d) ) return true;
    else if(dcmp(d4) == 0 && Onsegment(b,c,d) ) return true;
    else return false;
}
bool pointInpoly(Point p,Point a,Point b,Point c,Point d)
{
    double d1 = Cross(b-a,p-a);
    double d2 = Cross(d-c,p-c);
    double d3 = Cross(c-b,p-b);
    double d4 = Cross(a-d,p-d);
    return dcmp(d1)*dcmp(d2) > 0 && dcmp(d3)*dcmp(d4) > 0;
}

Point Segment(Point p,Vector v,Point q,Vector w)
{
    Vector u = p-q;
    double t = Cross(w,u) / Cross(v,w);
    return p + v*t;
}

double Max(double a,double b)
{
    return a > b ? a : b;
}
struct Line
{
    Point s,e;
    Line(Point s = 0,Point e = 0) :s(s),e(e){}
};
Point up[50],down[50];
int n;
double ans;
bool solve(Point a,Point b,int e)
{
    int sign,i;
    for( i = 0; i < n-1; i++)
    {
        if(dcmp( Cross(b-a,up[i]-a) ) < 0 || dcmp( Cross(b-a,up[i+1]-a) ) < 0 )
        {
            sign = 1;break;
        }
        if(  dcmp( Cross(b-a,down[i]-a) ) > 0 || dcmp( Cross(b-a,down[i+1]-a) ) > 0)
        {
            sign = 2;break;
        }
    }
    if(i == n-1)
        return true;
    if(i < e)
        return false;
    Point temp;
    if(sign == 1)
    {
        temp = Segment(a,b-a,up[i],up[i+1]-up[i]);
    }
    else
    {
        temp = Segment(a,b-a,down[i],down[i+1]-down[i]);
    }
    ans = Max(ans,temp.x);
    return false;
}
int main()
{
    while(scanf("%d",&n) != EOF)
    {
        if(n == 0) break;
        for(int i = 0; i < n; i++)
        {
            scanf("%lf%lf",&up[i].x,&up[i].y);
            down[i].x = up[i].x,down[i].y = up[i].y-1;
        }
        ans = -9999999;
        bool flag = false;
      
        for(int i = 0; i < n && !flag; i++)
        {
            for(int j = i+1; j < n; j++)
            {
                flag = solve(up[i],down[j],j);
                if(flag) break;
                flag = solve(down[i],up[j],j);
                if(flag) break;
            }
        }
        if(flag)
            printf("Through all the pipe.\n");
        else
            printf("%.2f\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值