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; }
相关文章推荐
- UVALive 7004(计算几何 判断点与多边形的关系)
- 【计算几何】 叉乘求多边形面积<凹凸均可>
- poj1584 A Round Peg in a Ground Hole 判断多边形凹凸,点到线的距离【基础计算几何】
- UVA 10522 - Height to Area(计算几何)
- UVALive 6835 Space Golf-计算几何
- UVA-11178 - Morley's Theorem(计算几何)
- hdu1115(计算多边形几何重心)
- uva579 计算几何入门
- UVA 11178 - Morley's Theorem(计算几何)
- ZOJ1248/POJ1474/HDU1469 Video Surveillance(判断多边形是否存在核,计算几何)
- uva 190(计算几何)
- UVA 12304 2D Geometry 110 in 1!(计算几何)
- 计算几何--简单多边形与圆面积交
- uva 11796 Dog Distance(计算几何, 基础)
- UVa 10256 (计算几何基础摸板)
- UVALive 6092 Catching Shade in Flatland --枚举+几何计算
- poj 3348 Cows 凸包 求多边形面积 计算几何 难度:0 Source:CCC207
- uva 10406 - Cutting tabletops(计算几何)
- UVA 11178-Morley's Theorem(计算几何_莫雷定理)
- UVa 438 The Circumference of the Circle (计算几何)