How I Mathematician Wonder What You Are!

题目大意:判断多多边形是否存在内核。

代码如下:

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;

const int MAXN = 107;
const int oo = 1e9+7;
const double EPS = 1e-10;

int Sign(double t)
{
    if(t > EPS)
        return 1;
    if(fabs(t) < EPS)
        return 0;
    return -1;
}

struct Point
{
    double  x, y;
    Point(double x=0, double y=0):x(x),y(y){}
    Point operator - (const Point &t)const{
        return Point(x-t.x, y-t.y);
    }
    double operator ^(const Point &t)const{
        return x*t.y - y*t.x;
    }

}p[MAXN], in[MAXN];
struct Segment
{///ax + by = c
    Point S, E;
    double a, b, c;
    Segment(Point S=0, Point E=0):S(S), E(E){
        a = S.y - E.y;
        b = E.x - S.x;
        c = E.x*S.y - S.x*E.y;
    }
    Point crossNode(const Segment &t)const{
        Point res;

        res.x = (c*t.b-t.c*b) / (a*t.b-t.a*b);
        res.y = (c*t.a-t.c*a) / (b*t.a-t.b*a);

        return res;
    }
    int Mul(const Point &t)
    {///用叉积判断方向
        return Sign((E-S)^(t-S));
    }
};
int CutPoly(Segment L, int N)
{
    Point tmp[MAXN];
    int cnt = 0;

    for(int i=1; i<=N; i++)
    {
        if(L.Mul(in[i]) <= 0)
            tmp[++cnt] = in[i];
        else
        {
            if(L.Mul(in[i-1]) < 0)///求出交点
                tmp[++cnt] = L.crossNode(Segment(in[i-1],in[i]));
            if(L.Mul(in[i+1]) < 0)
                tmp[++cnt] = L.crossNode(Segment(in[i],in[i+1]));
        }
    }

    for(int i=1; i<=cnt; i++)
        in[i] = tmp[i];
    in[0] = in[cnt], in[cnt+1] = in[1];

    return cnt;
}

int main()
{
    int N;

    while(scanf("%d", &N) != EOF && N)
    {
        int M;

        for(int i=N; i>=1; i--)
        {
            scanf("%lf%lf", &p[i].x, &p[i].y);
            in[i] = p[i];
        }
        in[0] = p[0] = p[N];
        in[N+1] = p[N+1] = p[1];
        M = N;

        for(int i=1; i<=N; i++)
            M = CutPoly(Segment(p[i],p[i+1]), M);

        if(M > 0)
            printf("1
");
        else
            printf("0
");
    }

    return 0;
}
原文地址:https://www.cnblogs.com/liuxin13/p/4920639.html