UVa1342

欧拉定理,平面图的顶点数、边数、面数分别为V,E和F,则V+F-E=2。

在保证 线段规范正交 的情况下,求得线段交点,交点可能重复,应该再去除重复的点,求出V。

sort()函数是要使用<运算符的

unique()函数是要使用==运算符的

根据所求的V,利用Onsegment()函数,求E。

Onsegment()函数作用是:判断一个点是否在一条线段上,不包含端点!!

所以,e的初始值应该为n,而不是0

bool OnSegment(const Point& p, const Point& a1, const Point& a2) {
  return dcmp(Cross(a1-p, a2-p)) == 0 && dcmp(Dot(a1-p, a2-p)) < 0;
}

主要代码:

        for ( i = 0; i < n; ++i ) {
            scanf ( "%lf%lf", &P[i].x, &P[i].y );
            V[i] = P[i];
        }
        --n;
        int c = n, e = n;
        for ( i = 0; i < n; ++i )
            for ( j = i+1; j < n; ++j )
                if ( SegmentProperIntersection(P[i], P[i+1], P[j], P[j+1]) )
                        V[c++] = GetLineIntersection(P[i], P[i+1]-P[i], P[j], P[j+1]-P[j]);
        sort ( V, V+c );//<
        c = unique(V, V+c) - V;//==
        //for ( i = 0; i < c; ++i ) printf ( "i=%d x=%lf y=%lf\n", i, V[i].x, V[i].y );                     
        for ( i = 0; i < c; ++i )
            for ( j = 0; j < n; ++j )
                if ( OnSegment(V[i], P[j], P[j+1]) ) ++e;
        //printf ( "e=%d\n", e );
        printf ( "Case %d: There are %d pieces.\n", cases++, e+2-c );
原文地址:https://www.cnblogs.com/Accoral/p/3138558.html