POJ 1265 Area 计算多边形格点数

http://poj.org/problem?id=1265

Being well known for its highly innovative products, Merck would definitely be a good target for industrial espionage. To protect its brand-new research and development facility the company has installed the latest system of surveillance robots patrolling the area. These robots move along the walls of the facility and report suspicious observations to the central security office. The only flaw in the system a competitor抯 agent could find is the fact that the robots radio their movements unencrypted. Not being able to find out more, the agent wants to use that information to calculate the exact size of the area occupied by the new facility. It is public knowledge that all the corners of the building are situated on a rectangular grid and that only straight walls are used. Figure 1 shows the course of a robot around an example area.

在这里插入图片描述
Figure 1: Example area.

You are hired to write a program that calculates the area occupied by the new facility from the movements of a robot along its walls. You can assume that this area is a polygon with corners on a rectangular grid. However, your boss insists that you use a formula he is so proud to have found somewhere. The formula relates the number I of grid points inside the polygon, the number E of grid points on the edges, and the total area A of the polygon. Unfortunately, you have lost the sheet on which he had written down that simple formula for you, so your first task is to find the formula yourself.

Input
The first line contains the number of scenarios.
For each scenario, you are given the number m, 3 <= m < 100, of movements of the robot in the first line. The following m lines contain pairs 揹x dy�of integers, separated by a single blank, satisfying .-100 <= dx, dy <= 100 and (dx, dy) != (0, 0). Such a pair means that the robot moves on to a grid point dx units to the right and dy units upwards on the grid (with respect to the current position). You can assume that the curve along which the robot moves is closed and that it does not intersect or even touch itself except for the start and end points. The robot moves anti-clockwise around the building, so the area to be calculated lies to the left of the curve. It is known in advance that the whole polygon would fit into a square on the grid with a side length of 100 units.
Output
The output for every scenario begins with a line containing 揝cenario #i:� where i is the number of the scenario starting at 1. Then print a single line containing I, E, and A, the area A rounded to one digit after the decimal point. Separate the three numbers by two single blanks. Terminate the output for the scenario with a blank line.
Sample Input

2
4
1 0
0 1
-1 0
0 -1
7
5 0
1 3
-2 2
-1 0
0 -3
-3 1
0 -3

Sample Output

Scenario #1:
0 4 1.0

Scenario #2:
12 16 19.0

题目大意:初始点为 ( 0 , 0 ) (0,0) (0,0),给出每次移动的横坐标距离和纵坐标距离,求围成的多边形的格点数。(多边形内的格点数 多边形边界的格点数 多边形的面积)
思路: P i c k Pick Pick公式:给定顶点坐标均是整点的简单多边形,有:面积=内部格点数目+边上格点数目/2-1。边界上的格点数目:把每条边当做左开右闭的区间以避免重复,一条左开右闭的线段 ( x 1 , y 1 ) → ( x 2 , y 2 ) (x_{1},y_{1})\to(x_{2},y_{2}) (x1,y1)(x2,y2)上的格点数为: g c d ( x 2 − x 1 , y 2 − y 1 ) gcd(x_{2}-x_{1},y_{2}-y_{1}) gcd(x2x1,y2y1)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;

const double pi=acos(-1.0);//弧度pi
const double eps=1e-10;//精度

struct point
{
    double x,y;
    point(double a=0,double b=0)
    {
        x=a,y=b;
    }
    friend point operator * (point a,double b)
    {
        return point(a.x*b,a.y*b);
    }
    friend point operator * (double a,point b)
    {
        return point(b.x*a,b.y*a);
    }
    point operator - (const point &b)const
    {
        return point(x-b.x,y-b.y);
    }
    point operator + (const point &b)const
    {
        return point(x+b.x,y+b.y);
    }
    point operator / (const double b)const
    {
        return point(x/b,y/b);
    }
    bool operator < (const point &b)const//按坐标排序
    {
        if(fabs(x-b.x)<eps)
            return y<b.y-eps;
        return x<b.x-eps;
    }
    void transxy(double sinb,double cosb)//逆时针旋转b弧度
    {                                      //若顺时针 在传入的sinb前加个-即可
        double tx=x,ty=y;
        x=tx*cosb-ty*sinb;
        y=tx*sinb+ty*cosb;
    }
    void transxy(double b)//逆时针旋转b弧度
    {                     //若顺时针传入-b即可
        double tx=x,ty=y;
        x=tx*cos(b)-ty*sin(b);
        y=tx*sin(b)+ty*cos(b);
    }
    double norm()
    {
        return sqrt(x*x+y*y);
    }
};

inline double dot(point a,point b)//点积
{
    return a.x*b.x+a.y*b.y;
}
inline double cross(point a,point b)//叉积
{
    return a.x*b.y-a.y*b.x;
}

inline double dist(point a,point b)//两点间距离
{
    return (a-b).norm();
}

inline int sgn(double x)
{
    if(fabs(x)<eps)
        return 0;
    if(x>0)
        return 1;
    return -1;
}

int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}

bool point_on_seg(point p,point s,point t)//判断点p 是否在线段st上 包括端点
{
    return sgn(cross(p-s,t-s))==0&&sgn(dot(p-s,p-t))<=0;
}

typedef point Vector;

const int maxn=105;

struct polygon
{
    int n;//多边形顶点数
    point a[maxn];//0 到 n-1 顺时针顺序
    polygon() {}
    double perimeter()//计算多边形周长
    {
        double sum=0;
        a[n]=a[0];
        for(int i=0;i<n;i++)
            sum+=(a[i+1]-a[i]).norm();
        return sum;
    }
    double area()//计算多边形有向面积
    {
        double sum=0;
        a[n]=a[0];
        for(int i=0;i<n;i++)
            sum+=cross(a[i+1],a[i]);
        return sum/2;
    }
    double final_area()//多边形面积 即一定为正数
    {
        return fabs(area());
    }
    int point_in(point t)//t在多边形外返回0 t在多边形内返回1 t在多边形边界上返回2
    {
        int num=0,d1,d2,k;
        a[n]=a[0];
        for(int i=0;i<n;i++)
        {
            if(point_on_seg(t,a[i],a[i+1]))
                return 2;
            k=sgn(cross(a[i+1]-a[i],t-a[i]));
            d1=sgn(a[i].y-t.y);
            d2=sgn(a[i+1].y-t.y);
            if(k>0&&d1<=0&&d2>0)
                ++num;
            if(k<0&&d2<=0&&d1>0)
                --num;
        }
        return num!=0;
    }
    point mass_center()//求多边形的重心坐标
    {
        point ans=point(0,0);
        if(sgn(area())==0)
            return ans;//多边形面积为0时重心没有定义 特判
        a[n]=a[0];
        for(int i=0;i<n;i++)
            ans=ans+(a[i]+a[i+1])*cross(a[i+1],a[i]);
        return ans/area()/6;
    }
    int border_int_point_num()
    {
        int num=0;
        a[n]=a[0];
        for(int i=0;i<n;i++)
            num+=gcd(abs(int(a[i+1].x-a[i].x)),abs(int(a[i+1].y-a[i].y)));
        return num;
    }
    int inside_int_point_num()
    {
        return int(final_area())+1-border_int_point_num()/2;
    }
};

int n;
point b[maxn];

int main()
{
    int t;
    scanf("%d",&t);
    int times=0;
    while(t--)
    {
        scanf("%d",&n);
        if(times)
            printf("\n");
        polygon p;
        p.n=n;
        double x=0,y=0;
        double xx,yy;
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf",&xx,&yy);
            x+=xx,y+=yy;
            p.a[i].x=x,p.a[i].y=y;
        }
        printf("Scenario #%d:\n",++times);
        printf("%d %d %.1f\n",p.inside_int_point_num(),p.border_int_point_num(),p.final_area());
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值