POJ 1474 Video Surveillance (多边形内核判断)

【题目链接】click here~~

【题目大意】:多边形内核的判断

【思路】:模板题

代码:

/*
* Problem: POJ No.1474
* Running time: 125MS
* Complier: G++
* Author: herongwei
* Create Time: 12:27 2015/10/2 星期五
*/

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

const double pi=acos(-1.0);
const double e=exp(1.0);
const double eps=1e-8;
const int maxn=105;

struct Point{
  double x,y;
}point[maxn];

Point temp[maxn];
Point p[maxn];
int pre_point ,last_point;
double a,b,c;

void getline(Point x,Point y)//获取直线ax+by+c==0
{
    a=y.y-x.y;
    b=x.x-y.x;
    c=y.x*x.y-x.x*y.y;
}

Point intersect(Point x,Point y)//获取直线ax+by+c==0  和点x和y所连直线的交点
{
    double u=fabs(a*x.x+b*x.y+c);
    double v=fabs(a*y.x+b*y.y+c);
    Point ans;
    ans.x=(x.x*v+y.x*u)/(u+v);
    ans.y=(x.y*v+y.y*u)/(u+v);
    return ans;
}

void cut()//用直线ax+by+c==0切割多边形
{
    int cut_num=0;
    for(int i=1; i<=last_point; ++i)
    {
        if(a*p[i].x+b*p[i].y+c>=0){
            temp[++cut_num]=p[i];
        }
        else
        {
            if(a*p[i-1].x+b*p[i-1].y+c>0)
            {
                temp[++cut_num]=intersect(p[i-1],p[i]);
            }
            if(a*p[i+1].x+b*p[i+1].y+c>0)
            {
                temp[++cut_num]=intersect(p[i+1],p[i]);
            }
        }
    }
    for(int i=1; i<=cut_num; ++i)
    {
        p[i]=temp[i];
    }
    p[cut_num+1]=temp[1];
    p[0]=temp[cut_num];
    last_point=cut_num;
}

void solve()
{
    for(int i=1; i<=pre_point; ++i){
        p[i]=point[i];
    }
    point[pre_point+1]=point[1];
    p[pre_point+1]=p[1];
    p[0]=p[pre_point];
    last_point=pre_point;
    for(int i=1; i<=pre_point; ++i)
    {
        getline(point[i],point[i+1]);//根据point[i]和point[i+1]确定直线ax+by+c==0
        cut();//用直线ax+by+c==0切割多边形
    }
}
int main()
{
     int tot=1;
     while(cin>>pre_point&&pre_point){
         for(int i=1; i<=pre_point; ++i){
             cin>>point[i].x>>point[i].y;
         }
         solve();
         printf("Floor #%d\n",tot++);
         if(last_point==0) puts("Surveillance is impossible."),puts("");
         else puts("Surveillance is possible."),puts("");
     }
     return 0;
}
/*
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
Floor #1
Surveillance is possible.

Floor #2
Surveillance is impossible.
*/



你可能感兴趣的:(内核,poj,多边形,计算几何,半平面)