HDU-1086-You can Solve a Geometry Problem too

来源:互联网 发布:java中webservice实例 编辑:程序博客网 时间:2024/05/16 08:28
#include<iostream>#include<string>#include<queue>#include<map>#include<set>#include<vector>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;/*    这个道算两条线段是否有交点,求交点个数;(不考虑交点重复的情况)    思路: 告诉你两条线段AB,CD;判断线段AB,CD是否相交只要判断直线AB与线段CD相交           以及直线CD与线段AB相交,那么便可以证明两线段相交;           判断直线与线段是否相交可以将线段的两个端点代入直线方程,若结果异号,说明直线与线段相交;*/typedef struct Point{    double x,y;}Point;//  判断直线AB是否与线段CD相交;bool Intersect(Point A,Point B,Point C,Point D){    //  直线方程F(x,y)为:(y-y1)*(x1-x2)-(x-x2)*(y1-y2)=0;    double FC=(C.y-A.y)*(A.x-B.x)-(C.x-A.x)*(A.y-B.y);    double FD=(D.y-A.y)*(A.x-B.x)-(D.x-A.x)*(A.y-B.y);    if(FC*FD<=0) return true;    else return false;}int main(){    int n;    while(~scanf("%d",&n)){        if(!n) break;        int sum=0;        Point a[105],b[105];        for(int i=0;i<n;i++) scanf("%lf%lf%lf%lf",&a[i].x,&a[i].y,&b[i].x,&b[i].y);        //  二重循环,遍历每条线段;        for(int i=0;i<n-1;i++){            for(int j=i+1;j<n;j++){                //  如果直线AB与线段CD相交且直线CD与线段AB相交,那么计数加一;                if(Intersect(a[i],b[i],a[j],b[j])&&Intersect(a[j],b[j],a[i],b[i])) sum++;            }        }        printf("%d\n",sum);    }    return 0;}

0 0