POJ 1474 Video Surveillance(半平面交)

题意:半平面交求多边形内核(我明明及的我之前是会用kuangbin第一份版平面交的,现在怎么就不会用了呢,补第二份代码)

代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define eps 1e-10
using namespace std;
struct point{
    double x,y;
}A[110],p[110],q[110];
int N;
void init(){
    for(int i=1;i<=N;++i){
        p[i]=A[i];
    }
    p[N+1]=p[1];p[0]=p[N];
}
void Get_equation(point p1,point p2,double &a,double &b,double &c)
{
    a = p2.y - p1.y;
    b = p1.x - p2.x;
    c = p2.x*p1.y - p1.x*p2.y;
}
point Intersection(point p1,point p2,double a,double b,double c)
{
    double u = fabs(a*p1.x + b*p1.y + c);
    double v = fabs(a*p2.x + b*p2.y + c);
    point t;
    t.x = (p1.x*v + p2.x*u)/(u+v);
    t.y = (p1.y*v + p2.y*u)/(u+v);
    return t;
}
void cut(double a,double b,double c,point p[],int &cnt){
    int tempcnt=0;
    for(int i=1;i<=cnt;++i){
        if(a*p[i].x+b*p[i].y+c>=0)q[++tempcnt]=p[i];
        else {
            if(a*p[i-1].x+b*p[i-1].y+c>0){
                q[++tempcnt]=Intersection(p[i],p[i-1],a,b,c);
            }
            if(a*p[i+1].x+b*p[i+1].y+c>0){
                q[++tempcnt]=Intersection(p[i],p[i+1],a,b,c);
            }
        }
    }
    for(int i=1;i<=tempcnt;++i){
        p[i]=q[i];
    }
    p[tempcnt+1]=p[1];p[0]=p[tempcnt];
    cnt=tempcnt;
}


void slove(){
    A[N+1]=A[1];
    init();
    int ans=N;
    for(int i=1;i<=N;++i){
        double a,b,c;
        Get_equation(A[i],A[i+1],a,b,c);
        cut(a,b,c,p,ans);
    }
    if(ans<=0)
        printf("Surveillance is impossible.
");
    else
        printf("Surveillance is possible.
");
}
int main()
{
    int t=1,i;
    while(scanf("%d",&N),N){
        for(i=1;i<=N;++i){
            scanf("%lf%lf",&A[i].x,&A[i].y);
        }
        printf("Floor #%d
",t++);
        slove();
        printf("
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/lalalatianlalu/p/8999415.html