Pick-up sticks POJ - 2653(暴力,线段是否相交)

#include
#include
#include
using namespace std;
const int maxn=1e5+7;
struct Point
{
    double x,y;
    Point(double _x=0,double _y=0):x(_x),y(_y){}
    friend Point operator-(Point p1,Point p2)
    {
        return Point(p1.x-p2.x,p1.y-p2.y);
    }
};
struct Line
{
    Point A,B;
    Line(Point _A=Point(0,0),Point _B=Point(0,0)):A(_A),B(_B){}
}line[maxn];
bool book[maxn];
double det(Point P1,Point P2)
{
    return P1.x*P2.y-P1.y*P2.x;
}
bool is_inter(Line &L1,Line &L2)
{
    if(min(L1.A.x,L1.B.x)<=max(L2.A.x,L2.B.x)&&
       min(L2.A.x,L2.B.x)<=max(L1.A.x,L1.B.x)&&
       min(L1.A.y,L1.B.y)<=max(L2.A.y,L2.B.y)&&
       min(L2.A.y,L2.B.y)<=max(L1.A.y,L1.B.y)&&
       det(L1.A-L2.A,L2.B-L2.A)*det(L1.B-L2.A,L2.B-L2.A)<=0&&
       det(L2.A-L1.A,L1.B-L1.A)*det(L2.B-L1.A,L1.B-L1.A)<=0)
       return true;
    return false;
}
int main()
{
    int n;
    double x1,y1,x2,y2;
    while(scanf("%d",&n)&&n)
    {
        memset(book,false,sizeof book);
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            line[i]=Line(Point(x1,y1),Point(x2,y2));
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                if(is_inter(line[i],line[j]))
                {
                    book[i]=true;
                    break;
                }
            }
        }
        printf("Top sticks: ");
        bool f=false;
        for(int i=1;i<=n;i++)
        {
            if(!book[i])
            {
                if(f)
                    printf(", ");
                printf("%d",i);
                f=true;
            }
        }
        printf(".\n");
    }
    return 0;
}

你可能感兴趣的:(几何,jihe)