码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1279 Art Gallery 半平面交 多边形的核

时间:2017-09-05 23:03:35      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:include   cut   oid   can   模板   color   str   operator   ios   

题意:求多边形的核的面积

套模板即可

#include <iostream>
#include <cstdio>
#include <cmath>
#define eps 1e-18
using namespace std;

const int MAXN = 1555;
double a, b, c;
int n, cnt;

struct Point
{
    double x, y;
    double operator ^(const Point &b) const
    {
        return x*b.y - y*b.x;
    }
}point[MAXN], p[MAXN], tp[MAXN];

void Get_equation(Point p1, Point p2)
{
    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 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()
{
    int tmp = 0;
    for(int i=1; i<=cnt; i++)
    {
        //顺时针是>-eps和>eps,逆时针是<eps和<-eps
        if(a * p[i].x + b * p[i].y + c > -eps) tp[++tmp] = p[i];
        else
        {
            if(a * p[i-1].x + b * p[i-1].y + c > eps)
                tp[++tmp] = Intersection(p[i-1], p[i]);
            if(a * p[i+1].x + b * p[i+1].y + c > eps)
                tp[++tmp] = Intersection(p[i], p[i+1]);
        }
    }
    for(int i=1; i<=tmp; i++)
        p[i] = tp[i];
    p[0] = p[tmp];
    p[tmp+1] = p[1];
    cnt = tmp;
}

double solve()
{
    for(int i=1; i<=n; i++)
        p[i] = point[i];
    point[n+1] = point[1];
    p[0] = p[n];
    p[n+1] = p[1];
    cnt = n;
    for(int i=1; i<=n; i++)
    {
        Get_equation(point[i], point[i+1]);
        Cut();
    }
    double res = 0;
    for(int i = 1; i <= cnt; i++)
        res += p[i]^p[i+1];
    return fabs(res/2);
}

int main()
{
    int T;
    scanf("%d\n", &T);
    while(T--)
    {
        scanf("%d", &n);
        for(int i=1; i<=n; i++)
            scanf("%lf%lf", &point[i].x, &point[i].y);
        printf("%.2f\n", solve());
    }
    return 0;
}

 

POJ 1279 Art Gallery 半平面交 多边形的核

标签:include   cut   oid   can   模板   color   str   operator   ios   

原文地址:http://www.cnblogs.com/pach/p/7482240.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!