poj1474(半平面交)

Description

A friend of yours has taken the job of security officer at the Star-Buy Company, a famous depart- ment store. One of his tasks is to install a video surveillance system to guarantee the security of the customers (and the security of the merchandise of course) on all of the store’s countless floors. As the company has only a limited budget, there will be only one camera on every floor. But these cameras may turn around to look in every direction.

The first problem is to choose where to install the camera for every floor. The only requirement is that every part of the room must be visible from there. In the following figure the left floor can be completely surveyed from the position indicated by a dot, while for the right floor, there is no such position, the given position failing to see the lower left part of the floor.

Before trying to install the cameras, your friend first wants to know whether there is indeed a suitable position for them. He therefore asks you to write a program that, given a ground plan, de- termines whether there is a position from which the whole floor is visible. All floor ground plans form rectangular polygons, whose edges do not intersect each other and touch each other only at the corners.

Input

The input contains several floor descriptions. Every description starts with the number n of vertices that bound the floor (4 <= n <= 100). The next n lines contain two integers each, the x and y coordinates for the n vertices, given in clockwise order. All vertices will be distinct and at corners of the polygon. Thus the edges alternate between horizontal and vertical.

A zero value for n indicates the end of the input.

Output

For every test case first output a line with the number of the floor, as shown in the sample output. Then print a line stating “Surveillance is possible.” if there exists a position from which the entire floor can be observed, or print “Surveillance is impossible.” if there is no such position.

Print a blank line after each test case.

Sample Input
4
0 0
0 1
1 1
1 0
8
0 0
0 2
1 2
1 1
2 1
2 2
3 2
3 0
0

Sample Output
Floor #1
Surveillance is possible.
Floor #2
Surveillance is impossible.

这里写代码片
//n vertices, given in clockwise order 顺时针 

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>

using namespace std;

const int N=1010;
const double eps=1e-8;  //
struct node{
    double x,y;
    node (double xx=0,double yy=0)
    {
        x=xx;y=yy;
    }
};
node po[N],p[N],q[N];
int n,m;
double a,b,c;

node operator +(const node &a,const node &b)
{
    return node(a.x+b.x,a.y+b.y);
}
node operator -(const node &a,const node &b)
{
    return node(a.x-b.x,a.y-b.y);
}
node operator *(const node &a,const double &b)
{
    return node(a.x*b,a.y*b);
}
node operator /(const node &a,const double &b)
{
    return node(a.x/b,a.y/b);
}

int dcmp(double x)
{
    if (fabs(x)<eps) return 0;  ////fabs
    else if (x<0) return -1;
    else return 1;
}

void getline(node x,node y)
{
    a=y.y-x.y;
    b=x.x-y.x;
    c=x.y*y.x-x.x*y.y;
    return;
}

node insert(node x,node y)  //////fault
{
    double u=fabs(a*x.x+b*x.y+c);
    double v=fabs(a*y.x+b*y.y+c);
    node ans;
    ans.x=(u*y.x+v*x.x)/(u+v); //交叉相乘相加 除以和 
    ans.y=(u*y.y+v*x.y)/(u+v);  /////
    return ans;
}

void cut()
{
    int cnt=0;
    for (int i=1;i<=m;i++)  ///
    {
        if (a*p[i].x+b*p[i].y+c>=0) q[++cnt]=p[i];
        else{
            if (a*p[i-1].x+b*p[i-1].y+c>0)
            {
                //该点不在多边形内,但是和它相邻的点在多边形内
                //(在边上不行,这样是没有交点的),那么它和它相邻的点构成直线与
                //ax+by+c==0所构成的交点可能在新切割出的多边形内,
                q[++cnt]=insert(p[i-1],p[i]);
            }
            if (a*p[i+1].x+b*p[i+1].y+c>0)
            {
                q[++cnt]=insert(p[i+1],p[i]);
            }
        }
    }
    for (int i=1;i<=cnt;i++)
        p[i]=q[i];
    p[cnt+1]=p[1];
    p[0]=p[cnt];
    m=cnt;
    return; 
}

void solve()
{
    for (int i=1;i<=n;i++) p[i]=po[i];
    po[n+1]=po[1];
    p[n+1]=p[1];
    p[0]=p[n];
    m=n;
    for (int i=1;i<n;i++)
    {
        getline(po[i],po[i+1]);
        cut();
    }
    return;
}

int main()
{
    int T=0;
    scanf("%d",&n);
    while (n)
    {
        T++;
        printf("Floor #%d
",T);
        for (int i=1;i<=n;i++)
            scanf("%lf%lf",&po[i].x,&po[i].y);
        solve();
        if (m) printf("Surveillance is possible.
");  
        else printf("Surveillance is impossible.
");
        printf("
");
        scanf("%d",&n);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wutongtong3117/p/7673597.html