poj1279Art Gallery

Description

The art galleries of the new and very futuristic building of the Center for Balkan Cooperation have the form of polygons (not necessarily convex). When a big exhibition is organized, watching over all of the pictures is a big security concern. Your task is that for a given gallery to write a program which finds the surface of the area of the floor, from which each point on the walls of the gallery is visible. On the figure 1. a map of a gallery is given in some co-ordinate system. The area wanted is shaded on the figure 2.

Input

The number of tasks T that your program have to solve will be on the first row of the input file. Input data for each task start with an integer N, 5 <= N <= 1500. Each of the next N rows of the input will contain the co-ordinates of a vertex of the polygon ? two integers that fit in 16-bit integer type, separated by a single space. Following the row with the co-ordinates of the last vertex for the task comes the line with the number of vertices for the next test and so on.

Output

For each test you must write on one line the required surface - a number with exactly two digits after the decimal point (the number should be rounded to the second digit after the decimal point)

Sample Input
1
7
0 0
4 4
4 7
9 7
13 -1
8 -6
4 -4

Sample Output
80.00

分析:莫名其妙的AC,之前做半平面交,只要涉及面积计算就是无尽wa,
果不其然,这道题第一次提交也是wa,崩溃的看了看以前的代码,又交了一遍(什么都没改),然而就A了,exm。。。难道真的是看脸吗
这里写图片描述

这里写代码片
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>

using namespace std;

const int N=100001;
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;
    else if (x>0) return 1;
    else return -1;
}

double Cross(node x,node y)
{
    return x.x*y.y-x.y*y.x;
}

double Dot(node x,node y)
{
    return x.x*y.x+x.y*y.y;
}

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;
}

node insert(node x,node y)
{
    double u=fabs(a*x.x+b*x.y+c);
    double v=fabs(a*y.x+b*y.y+c);
    node ans;
    ans.x=(v*x.x+u*y.x)/(u+v);
    ans.y=(v*x.y+u*y.y)/(u+v);
    return ans;
}

void cut()
{
    int i,cnt=0;
    for (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) q[++cnt]=insert(p[i],p[i-1]);
            if (a*p[i+1].x+b*p[i+1].y+c>0) q[++cnt]=insert(p[i],p[i+1]);
        }
    }
    for (i=1;i<=cnt;i++) p[i]=q[i];
    p[0]=p[cnt]; p[cnt+1]=p[1];
    m=cnt;
    return;
}

double S()
{
    int i;
    double ans=0;
    for (i=0;i<m;i++)  //循环是从0m 
        ans+=Cross(p[i]-p[0],p[i+1]-p[0]);  //-p[0] 
    return fabs(ans)/2;  //带符号计算 
}

void doit()
{
     int i,j,k;
     for (i=1;i<=n;i++) p[i]=po[i];
     p[0]=p[n]; p[n+1]=p[1];
     0]=po[po[n];po[n+1]=po[1];
     m=n;
     for (i=1;i<=n;i++)
     {
        getline(po[i],po[i+1]);
        cut();
        if (!m) break;
     }
     if (m<=2) printf("0.00\n");
     else
     {
        double ans=S();
        printf("%0.2lf\n",ans);
     }
     return;
}

int main()
{
    int T;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d",&n);
        for (int i=1;i<=n;i++)
        //题目保证顺时针读入
            scanf("%lf%lf",&po[i].x,&po[i].y);
        doit();
    }
    return 0;
}

转载于:https://www.cnblogs.com/wutongtong3117/p/7673506.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值