Poj1265 -- area (pick theorem + Polygon Area)

Source: Internet
Author: User

Area

Description
Being well known for its highly innovative products, Merck wocould definitely be a good target for industrial espionage. to protect its brand-new research and development facility the company has installed the latest system of surveillance robots patrolling the area. these robots move along the walls of the facility and report suspicous observations to the Central Security Office. the only flaw in the system a competitor using agent cocould find is the fact that the robots radio their movements unencrypted. not being able to find out more, the agent wants to use that information to calculate the exact size of the area occupied by the new facility. it is public knowledge that all the corners of the building are situated on a rectangular grid and that only straight wballs are used. figure 1 shows the course of a robot around und an example area.
Figure 1: Example area.
You are hired to write a program that calculates the area occupied by the new facility from the movements of a robot along its Wils. you can assume that this area is a polygon with corners on a rectangular grid. however, your boss insists that you use a formula he is so proud to have found somewhere. the formula relates the number I of grid points inside the polygon, the number E of grid points on the edges, and the total area A of the polygon. unfortunately, you have lost the sheet on which he had written down that simple formula for you, so your first task is to find the formula yourself.
Input
The first line contains the number of scenarios.
For each scenario, you are given the number M, 3 <= m <100, of movements of the robot in the first line. The following M lines contain pairs limit x Dy? Of integers, separated by a single blank, satisfying.-100 <= DX, Dy <= 100 and (dx, Dy )! = (0, 0 ). such a pair means that the robot moves on to a grid point DX units to the right and Dy units upwards on the grid (with respect to the current position ). you can assume that the curve along which the robot moves is closed and that it does not intersect or even touch itself doesn t for the start and end points. the robot moves anti-clockwise around the building, so the area to be calculated lies to the left of the curve. it is known in advance that the whole polygon wocould fit into a square on the grid with a side length of 100 units.
Output
The output for every scenario begins with a line containing extends cenario # I :? Where I is the number of the scenario starting at 1. then print a single line containing I, e, and a, the area a rounded to one digit after the decimal point. separate the three numbers by two single blanks. terminate the output for the scenario with a blank line.
Sample Input
2
4
1 0
0 1
-1 0
0-1
7
5 0
1 3
-2 2
-1 0
0-3
-3 1
0-3
Sample output
Scenario #1:
0 4 1.0
Scenario #2:
12 16 19.0

Question:

For a simple polygon on the plane, calculate the points on the edge, the points in the polygon, and the area of the polygon.

Solution:

Pick theorem and cross product calculate the area of a polygon.

Pick theorem:Formula for Calculating the area of a polygon vertex in a dot matrix: S = a + B/2-1. A indicates the points inside the polygon, and B indicates the points on the polygon boundary, s indicates the area of the polygon.

Polygon Area:

1) The area of △abc is half of the Cross multiplication between the vector AB and the vector AC.

2) For a polygon, select a vertex P1 and connect it to other vertices. You can divide the Polygon into several triangles.

3) The polygon area is ABS (sum {crossmul (a, B, P1) | A, B is two adjacent vertices}) (evaluate and then obtain ABS first, otherwise there will be an error in the concave polygon)

Calculate the number of vertices on the Edge:

For PA (x1, Y1), Pb (X2, Y2) connected to the selected segment, the number of points passing through the gcd (ABS (x1-x2), ABS (y1-y2) + 1.

Code:

 1 #include<stdio.h> 2 #include<cmath> 3 #include<iostream> 4 #include<algorithm> 5 #define MAXN 10000 6 using namespace std; 7 int gcd(int a,int b) 8 { 9     return b==0?a:gcd(b,a%b);10 }11 int TeaTable(int x1,int y1,int x2,int y2)12 {13     return x1*y2-x2*y1;14 }15 int main()16 {17     int T,times=0;18     cin>>T;19     while (T--)20     {21         times++;22         int N;23         cin>>N;24         int area=0;25         int x=0,y=0,dx=0,dy=0;26         int cnt=0;27         for (int i=1;i<=N;i++)28         {29             int tx,ty;30             cin>>tx>>ty;31             dx=x+tx,dy=y+ty;32             cnt+=gcd(abs(tx),abs(ty));33             area+=TeaTable(x,y,dx,dy);34             x=dx,y=dy;35         }36         area=area>0?area:-area;37         double ans3=(double)area/2.0;38         int ans2=cnt;39         int ans1=(area+2-ans2)/2;40         printf("Scenario #%d:\n",times);41         printf("%d %d %.1lf\n\n",ans1,ans2,ans3);42     }43 44     return 0;45 }

 

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.