2 . Polygon
(Polygon.pas/c/cpp)
"Problem description"
In a planar Cartesian coordinate system, a simple polygon [1] with a vertex transverse ordinate is given as an integer, and the number of points in the polygon where the horizontal ordinate is an integer is obtained.
Input
The input file name is polygon.in, a total line, and the first line contains a positive integer.
The following lines, each containing two integers, represent the coordinates of the polygon vertices in turn, and the vertices are given in counter-clockwise order.
Output
The output file is named Polygon.out, a total line containing a non-negative integer representing the number of the hour inside the polygon.
"Input and Output sample"
Polygon.in |
Polygon.out |
7 0 3 1 1 4 2 7 1 5 3 8 5 2 6 |
20 |
"Sample description"
The dot in yellow is the whole point within the polygon, a total of 20.
Exam questions.
Calculate geometry The first problem is to send it.
First, the pic theorem: S=a+b/2-1 (A is the number of points in the lattice graph, B is the hour on the edge)
So just ask for the whole point on the edge, graphics area.
The whole point on the edge: (GCD of the absolute value of the difference between the coordinates of each edge)-1 is all points on the two end points of the edge.
Then ask for the area.
Cross-product Baidu bar. (The math formula can't be explained by hanging up.) )
So approximately the source point is a node, and then even n times, each time the input is 1 points and 2 points, 2 points and 3 points. N points and 1 points.
Then there are two vectors at a time.
The sum of their vector forks is the area and
As for proving yourself a push.
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <cmath>5 6 using namespacestd;7 8 #defineMAXN 1000019 Ten intgcdintNintm) {returnm==0? N:GCD (m,n%m);} One AInlineint inch() - { - intx=0, f=1;CharCh=GetChar (); the while((ch<'0'|| Ch>'9') &&ch!='-') ch=GetChar (); - if(ch=='-') f=-1, ch=GetChar (); - while(ch>='0'&&ch<='9') x=x*Ten+ch-'0', ch=GetChar (); - returnx*F; + } - + structed{ A intx, y; at}a[maxn+1]; - - intMain () - { -Freopen ("polygon.in","R", stdin); -Freopen ("Polygon.out","W", stdout); in intN; - Long Longbiandian=0, s=0; ton=inch(); + for(intI=1; i<=n;i++) -a[i].x=inch(), a[i].y=inch(); thea[n+1]=a[1]; * for(intI=1; i<=n;i++) $ {Panax NotoginsengBIANDIAN+=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; the } +printf"%lld",(Long Long* (p+2-biandian) >> (Long Long)1); A return 0; the}
View Code
Calculate geometry polygon