Area-poj 1265 (pick theorem to find lattice points + polygon area)

Source: Internet
Author: User

The main idea: start with the origin and then add an x, Y value each time, find out how many points at the end of the polygon edge, how many internal points, polygon area is how much.

Analysis:

1, with the lattice point as the vertex of the line segment, the number of points covered is gcd (Dx,dy), wherein, dxdy respectively for the segment of the horizontal and vertical points accounted for. If DX or dy is 0, the number of points covered is dy or dx.
2. Pick Formula: The area of a simple polygon with a lattice point on a plane = the number of points on the edge/2+ the number of points inside +1.
3. The area of any polygon is equal to the sum of the cross product of the vector of two neighboring points and the origin in order.

The code is as follows:

-------------------------------------------------------------------------------------------------------------

#include <iostream>#include<string.h>#include<stdio.h>#include<algorithm>#include<math.h>using namespacestd;Const intMAXN = 1e4+7;Const DoubleEPS = 1e-Ten;intGCD (intMintN) {    if(!m | |!N)returnm+N; returnGCD (N, m%n);}intMain () {intT, t=1; scanf ("%d", &T);  while(t--)    {        intN, x, Y, nx=0, ny=0, cnt=0, area=0; scanf ("%d", &N);  for(intI=0; i<n; i++) {scanf ("%d%d", &x, &y); CNT+=GCD (ABS (x), ABS (y)); X+ = NX, y + =NY; Area+ = (x*ny-y*NX); NX= x, NY =y; }        if(Area <0) area =-Area ; printf ("Scenario #%d:\n", t++); printf ("%d%d%.1f\n\n", (AREA-CNT)/2+1, CNT, area/2.0); }    return 0;}

Area-poj 1265 (pick theorem to find lattice points + polygon area)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.