Poj 1265 area (pick theorem)

Source: Internet
Author: User

Link: poj 1265

Starting from the origin, we will provide some Dx and Dy moving increments to form a polygon,

Calculate the number of points inside the polygon, the number of points on the edge, and the area.

Additional knowledge:

1. The number of vertices covered by a grid point is gcd (| DX |, | dy |), where, | DX |, | dy | horizontal and vertical increments of line segments.
2,Pick Theorem: Set the number of internal points of the polygon with the grid point as a vertex on the plane to A, the number of points on the edge to B, and the area to S,

Then S = a + B/2-1.
3. the area of any polygon is equal to half of the Cross Product of the vector composed of adjacent two points and the origin point in order.

Idea: Because Dx and Dy in each step are known, use the above knowledge to first obtain the number of points on the edge and the area of the polygon, then the internal point can be obtained.

Note: Do not take the absolute value every time an area is calculated. The cumulative sum and absolute value of the cross product are required.

#include<stdio.h>#include<stdlib.h>int chaji(int x1,int y1,int x2,int y2){    return x1*y2-x2*y1;}int gcd(int a,int b){    return b==0?a:gcd(b,a%b);}int main(){    int T,m,i,j,dx,dy,n,b,x,y;    float s;    scanf("%d",&T);    for(i=1;i<=T;i++){        scanf("%d",&m);        scanf("%d%d",&x,&y);        b=gcd(abs(x),abs(y));        s=0;        for(j=2;j<=m;j++){            scanf("%d%d",&dx,&dy);            b+=gcd(abs(dx),abs(dy));            s+=chaji(x,y,x+dx,y+dy);            x+=dx;            y+=dy;        }        if(s<0)            s=-s;        n=(s+2-b)/2;        printf("Scenario #%d:\n",i);        printf("%d %d %.1f\n\n",n,b,s/2);    }    return 0;}


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.