您的位置:首页 > 其它

【计算几何】多边形

2015-10-13 21:00 597 查看
2.多边形

(polygon.pas/c/cpp)

【问题描述】

在平面直角坐标系中给出一个顶点横纵坐标均为整数的简单多边形[1],求在这个多边形内部有多少个横纵坐标均为整数的点。

【输入】

输入文件名为polygon.in,共 行,第一行包含一个正整数 。

下面共 行,每行包含两个整数 ,依次表示多边形顶点的坐标,顶点按照逆时针顺序给出。

【输出】

输出文件名为polygon.out,共一行,包含一个非负整数,表示多边形内部的整点个数。

【输入输出样例】

polygon.in

polygon.out

7

0 3

1 1

4 2

7 1

5 3

8 5

2 6

20

【样例说明】

[b]  

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>

using namespace std;

#define maxn 100001

int gcd(int n,int m){return m==0?n:gcd(m,n%m);}

inline int in()
{
int x=0,f=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')f=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*f;
}

struct ed{
int x,y;
}a[maxn+1];

int main()
{
freopen("polygon.in","r",stdin);
freopen("polygon.out","w",stdout);
int n;
long long biandian=0,S=0;
n=in();
for(int i=1;i<=n;i++)
a[i].x=in(),a[i].y=in();
a[n+1]=a[1];
for(int i=1;i<=n;i++)
{
biandian+=gcd(fabs(a[i+1].x-a[i].x),fabs(a[i+1].y-a[i].y));
S+=(long long)a[i].x*a[i+1].y-(long long)a[i].y*a[i+1].x;
}
printf("%lld",(long long)(S+2-biandian)>>(long long)1);
return 0;
}


View Code

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: