POJ 1279 Art Gallery (多边形组成的内核面积)

【题目链接】:click here~~

【题目大意】求多边形组成的内核面积

【思路】:模板题参考上一题

代码:

/*
* Problem: POJ No.1279
* Running time: 16MS
* 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=1505;

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 t;cin>>t;while(t--)
    {
        cin>>pre_point;
        for(int i=1; i<=pre_point; ++i){
            cin>>point[i].x>>point[i].y;
        }
        //多边形核的面积
        double area = 0.0;
        for(int i = 1; i <= last_point; ++i)
            area += p[i].x * p[i + 1].y - p[i + 1].x * p[i].y;
        area = fabs(area / 2.0);
        //此时last_point为最终切割得到的多边形的顶点数,p为存放顶点的数组
        printf("%.2f\n",area);
    }
    return 0;
}


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