hdu 1086 You can Solve a Geometry Problem too(线段相交的交点个数)

来源:互联网 发布:java工厂模式的作用 编辑:程序博客网 时间:2024/06/05 10:31

和上一题差不多,只是变成了计数。

可是我改了上题的那个flag部分和数组大小去交却是wa。

然后我把读入改成了直接读到对应线段的对应点的对应坐标,以及判断线段相交的函数改成

传两条线段的四个端点就AC了。。。不知道为嘛。。。。


#include<cstdio>#include<iostream>#include<cstring>#define maxn 110using namespace std;struct Point{    double x,y;}point[maxn<<1];struct node{    Point a,b;}seg[maxn];typedef long long ll;double direction(Point a,Point b,Point c){    double c1,c2,c3,c4;    c1 = c.x-a.x;    c2 = c.y-a.y;    c3 = b.x-a.x;    c4 = b.y-a.y;    return (c1*c4-c2*c3);}bool onsegment(Point a,Point b,Point c){    if(c.x<=max(a.x,b.x) && c.x>=min(a.x,b.x) && c.y<=max(a.y,b.y)&& c.y>=min(a.y,b.y))        return true;    else return false;}bool segintersect(Point a,Point b,Point c,Point d){    double d1,d2,d3,d4;    d1 = direction(a,b,c);    d2 = direction(a,b,d);    d3 = direction(c,d,a);    d4 = direction(c,d,b);    if(d1*d2<0 && d3*d4<0)        return true;    else if(d1==0 && onsegment(a,b,c))        return true;    else if(d2==0 && onsegment(a,b,d))        return true;    else if(d3==0 && onsegment(c,d,a))        return true;    else if(d4==0 && onsegment(c,d,b))        return true;    else return false;}int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        if(n==0) break;        for(int i=0;i<n;i++)            scanf("%lf%lf%lf%lf",&seg[i].a.x,&seg[i].a.y,&seg[i].b.x,&seg[i].b.y);        int ans = 0;        for(int i=0;i<n;i++)        {            for(int j=i+1;j<n;j++)            {                if(segintersect(seg[i].a,seg[i].b,seg[j].a,seg[j].b))                    ans++;            }        }        printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击