hdu_1086

发现自己连向量的点乘都不会算了

 

跨立实验

 

#include <cstdio>



#define MAXN 100



struct point

{

    double x, y;

    point operator - (point temp) const

    {

        point res;

        res.x = temp.x - x;

        res.y = temp.y - y;

        return res;

    }

    double operator * (point temp) const

    {

        return x*temp.y - temp.x*y;

    }

};



int IsIntersect(point a1, point a2, point b1, point b2)

{

    double d1 = (a2-a1)*(b1-a1);

    double d2 = (a2-a1)*(b2-a1);

    double d3 = (b2-b1)*(a1-b1);

    double d4 = (b2-b1)*(a2-b1);

    if(d1*d2<=0 && d3*d4<=0)

        return 1;

    return 0;

}



point be[MAXN+10], en[MAXN+10];



int main(int argc, char const *argv[])

{

    // freopen("in", "r", stdin);

    int n;

    while(scanf("%d", &n) && n){

        int intersections = 0;

        //  input

        for(int i = 1; i <= n; ++i)

            scanf("%lf %lf %lf %lf", &be[i].x, &be[i].y, &en[i].x, &en[i].y);

        // solve

        for(int i = 1; i <= n; ++i)

            for(int j = i+1; j <= n; ++j)

                intersections += IsIntersect(be[i], en[i], be[j], en[j]);

        // print

        printf("%d\n", intersections);

    }

    return 0;

}

 

你可能感兴趣的:(HDU)