您的位置:首页 > 其它

UVA10088多边形内整点个数计算(计算几何)

2014-03-05 01:11 453 查看
/*UVA10088
pick定理:
在坐标为整数的二维平面内,对于任意多边形,有s=a+b/2-1,其中b是落在边上的点数,a是内部点数,s是多边形的面积
两个整点连线上的整点的个数是gcd(dx,dy)
You may assume that none of the coordinates will be larger than 1,000,000 in absolute
values.
*/
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxn 1010
#define LL long long
using namespace std;
struct Point
{
double x,y;
Point(){}
Point(int xx,int yy){x=xx;y=yy;}
}P[maxn];
typedef Point Vector;
//多边形有向面积
Vector operator + (Vector A, Vector B)
{
return Vector(A.x+B.x, A.y+B.y);
}
//点-点=向量
Vector operator - (Vector A, Vector B)
{
return Vector(A.x-B.x, A.y-B.y);
}
double Cross(Vector A, Vector B)
{
return A.x*B.y - A.y*B.x;
}
double Area2(Point A, Point B, Point C)
{
return Cross(B-A, C-A);
}
double PolygonArea(Point *p, int n)
{
double area = 0.0;
for(int i = 1; i < n-1; i++)
area += Cross(p[i]-p[0], p[i+1]-p[0]);
return area/2.0;
}
LL gcd(LL a,LL b)
{
if((a%b)==0) return b;
else return gcd(b,a%b);
}
int n;
int main()
{
while(cin>>n && n>0)
{
LL b=0;
for(int i=0;i<n;i++)
cin>>P[i].x>>P[i].y;
for(int i=0;i<n;i++)
{
LL dx=fabs(P[i].x-P[(i+1)%n].x);
LL dy=fabs(P[i].y-P[(i+1)%n].y);
if(dx>dy)swap(dx,dy);
if(dx==0) b+=dy;else if (dy==0) b+=dx;else b+=gcd(dx,dy);//注意实际的dxdy是可能等于0的
}
double S=fabs(PolygonArea(P,n));
//        s=a+b/2-1
LL ans=(LL)S+1-(LL)b/2;
cout<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: