Calculation of geometric poj pick formula cross-product Polygon Area
Returns the contour of a polygon (given in the form of an edge vector. how many points are in this graph? 2. number of points in the graph. the area of the image.
Idea: first, define the pick formula:
The formula does not mean that we can determine the area of the polygon. First, we do not need to use the pick formula to find the area. Second, we cannot find the whole point in the middle of the polygon. However, the area can be obtained by using the cross product, and the entire point on the side of the polygon can be obtained by using GCD. After a slight deformation, we can solve the number of integral points in the middle of the polygon.
Code (C ++ AC, G ++ wa ):
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;struct Point{int x,y;Point(int _ = 0,int __ = 0):x(_),y(__) {}Point operator +(const Point &a)const {return Point(x + a.x,y + a.y);}};int cases;int points;inline int Cross(const Point &a,const Point &b){return a.x * b.y - a.y * b.x;}int GCD(int x,int y){return y ? GCD(y,x % y):x;}int main(){for(cin >> cases; cases; --cases) {scanf("%d",&points);int on_side = 0,area = 0;Point now,dir,_next;for(int i = 1; i <= points; ++i) {scanf("%d%d",&dir.x,&dir.y);on_side += GCD(abs(dir.x),abs(dir.y));_next = (now + dir);area += Cross(now,_next);now = _next;}area = abs(area);static int T = 0;printf("Scenario #%d:\n",++T);printf("%d %d %d.%d\n\n",(area + 2 - on_side) / 2,on_side,area >> 1,((area&1) ? (5):(0)));}return 0;}
Poj 1265 area pick Formula