Poj 1265 Area

Source: Internet
Author: User

This question uses several knowledge. One is the formula for finding the polygon area. Then, calculate the number of vertices on the boundary of the polygon based on the vertices on the whole point. The last one is pick.
Theorem. Calculate the number of points in a polygon based on the previous two information and the pick theorem.
The method for finding the polygon area is still the principle that the cross product represents the directed area. We regard the origin point as another point to divide the original Polygon into N triangles, and then divide their directed surfaces.
Add up.
The number of vertices on the boundary is determined based on the conclusion that Gcd (dx, dy) represents the number of Integer Points on the current edge. The proof of this conclusion is actually relatively simple, assuming dx = a, dy = B.
The initial vertex is x0 and y0. Assume d = Gcd (a, B ). The Vertex on the edge can be expressed as (x0 + k * (a/d), y0 + k * (B/d )). To make the vertex an integer,
K must be an integer and 0 <= k <= d, so a maximum of d vertices exist.
The pick theorem is used to calculate the number of points inside a polygon. Area = internal point + boundary point/2-1.

The Code is as follows:
# Include <stdio. h>
# Include <string. h>
# Include <algorithm>
# Include <math. h>
Using namespace std;
# Define MAX (100 + 10)

Struct Point
{
Double x, y;
};
Point pts [MAX];

Int nN;
Const int IN = 1;
Const int EAGE = 2;
Const int OUT = 3;
Const double fPre = 1e-8;

Double Det (double fX1, double fY1, double fX2, double fY2)
{
Return fX1 * fY2-fX2 * fY1;
}

Double Cross (Point a, Point B, Point c)
{
Return Det (B. x-a. x, B. y-a. y, c. x-a. x, c. y-a. y );
}

Double GetArea ()
{
Double fArea = 0.0;
Point ori = {0.0, 0.0 };

For (int I = 0; I <nN; ++ I)
{
FArea + = Cross (ori, pts [I], pts [(I + 1) % nN]);
}
Return fabs (fArea) * 0.5;
}

Int gcd (int nX, int nY)
{
If (nX <0)
{
NX =-nX;
}
If (nY <0)
{
NY =-nY;
}
If (nX <nY)
{
Swap (nX, nY );
}
While (nY)
{
Int nT = nY;
NY = nX % nY;
NX = nT;
}
Return nX;
}

Int main ()
{
Int nT;
Int nI, nE;
Double fArea;

Scanf ("% d", & nT );
Int dx, dy;

For (int I = 1; I <= nT; ++ I)
{
Scanf ("% d", & nN );
NI = nE = 0;
Pts [0]. x = pts [0]. y = 0;
For (int j = 1; j <= nN; ++ j)
{
Scanf ("% d", & dx, & dy );
Pts [j]. x = pts [j-1]. x + dx;
Pts [j]. y = pts [j-1]. y + dy;
NE + = gcd (dx, dy );
} Www.2cto.com
FArea = GetArea ();
NI = (fArea + 1)-nE/2;
Printf ("Scenario # % d: \ n % d %. 1f \ n", I, nI, nE, fArea );
}

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.