(Hdu step 7.1.3) Lifting the Stone (to find the center of gravity of a convex polygon)

Source: Internet
Author: User

Topic:

Lifting the Stone
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 168 Accepted Submission (s): 98
problem Descriptionthere is many secret openings in The floor which is covered by a big heavy stone. When the stone was lifted up, a special mechanism detects this and activates poisoned arrows that was shot near the opening . The only possibility are to lift the stone very slowly and carefully. The ACM team must connect a rope to the stone and then lift it using a pulley. Moreover, the stone must is lifted all at once; No side can rise before another. So it was very important to find the centre of gravity and connect, the rope exactly to. The stone has a polygonal shape and its height are the same throughout the whole polygonal area. Your task is to find the centre of gravity for the given polygon. 
inputthe input consists of T test cases. The number of them (T) is given in the first line of the input file. Each test case begins with a line containing a single integer n (3 <= n <= 1000000) indicating the number of points That form the polygon. This was followed by N lines, each containing, integers Xi and Yi (| xi|, | yi| <= 20000). These numbers is the coordinates of the i-th point. When we connect the points in the given order, we get a polygon. Assume that the edges never touch all other (except the neighboring ones) and that they never cross. The area of the polygon is never zero, i.e. it cannot collapse to a single line. 
Output
Print exactly one line for each test case. The line should contain exactly and numbers separated by one space. These numbers is the coordinates of the centre of gravity. Round the coordinates to the nearest number with exactly, digits after the decimal point (0.005 rounds up to 0.01). Note that the centre of gravity may outside the polygon, if it shape is not convex. If There is such a case in the input data, print the centre anyway.
Sample Input
245 00 5-5 00-541 111 111 111 11
Sample Output
0.00 0.006.00 6.00
Sourcecentral Europe 1999
Recommendeddy


Topic Analysis:

Find the center of gravity of convex polygon, simple problem.

1, accumulate and seek center of gravity
The set plane has n discrete data points (xi, yi) (i = 1, 2,., n), which
The polygon center of gravity g (. x1,. Y1) is:

  

This is the simplest and most intuitive way to find polygons. Can directly utilize discrete numbers
The X-and y-coordinates of a stronghold can be a graphical center of gravity. But the flaw is that there is no discrete
Data points around the graphics to do any processing and analysis, precision is not enough.

2, algorithm one: In speaking of the algorithm, the first to understand the following several theorems.
Theorem 1 The vertex coordinates of the known triangle A1A2A3 ai (xi, yi) (i = 1, 2, 3). Its center of gravity coordinates are:

XG = (x1+x2+x3)/3; YG = (y1+y2+y3)/3;

Theorem 2 The vertex coordinates of the known triangle A1A2A3 ai (xi, yi) (i = 1, 2, 3). The area of the triangle is:

S = ((x2-x1) * (y3-y1)-(x3-x1) * (Y2-Y1))/2;

A1A2A3 boundary form Counterclockwise loop when Take +, clockwise time to take-.

In addition, in the process of solving, it is not necessary to consider whether the input order of points is clockwise or counterclockwise, and the division is offset.

Principle: Divide the polygon into N small area, each small area area is Ōi, the center of gravity is GI (. xi,. Yi), using the plane thin plate center of gravity formula to change the integral
into cumulative and:

    

                  

The general center of gravity formula for the polygon around discrete data points can be obtained by the principle and mathematical theorem presented above: any n-sided a1a2 with the AI (xi, yi) (i = 1, 2,., n) as the vertex. An, dividing it into N-2 triangles (1). The center of gravity for each triangle is GI (. xi,. Yi), with an area of Ōi. Then the center of gravity coordinates g (. x2,. y2) of the polygon are:

  

Figure 1 Polygon decomposition



The code is as follows:

#include <iostream> #include <cstdio>using namespace std;const int maxn = 1000001;struct PPoint {double x, y;} ;d ouble Area (ppoint p0, ppoint p1, ppoint p2) {Double area = 0;area = p0.x * p1.y + p1.x * p2.y + p2.x * p0.y-p1.x * p0. y-p2.x * p1.y-p0.x * P2.y;return AREA/2;  In addition, in the process of solving, it is not necessary to consider whether the input order of points is clockwise or counterclockwise, and the division is offset. }int Main () {int t;scanf ("%d", &t), while (t--) {int n;scanf ("%d", &n); PPoint p0,p1,p2;scanf ("%lf%lf", &p0.x,&p0.y) scanf ("%lf%lf", &p1.x,&p1.y);d ouble Sum_area = 0;double sum_x = 0;double sum_y = 0;int i;for (i = 2; i < n; ++i) {scanf ("%lf%lf", &p2.x,&p2.y);d ouble area = Area (p0,p1 , p2); Sum_area + = area;sum_x + = (p0.x + p1.x+ p2.x) *area;sum_y + = (p0.y + p1.y + p2.y) *area;p1 = p2;} printf ("%.2lf%.2lf\n", sum_x/(sum_area*3), sum_y/(sum_area*3));} return 0;}





(Hdu step 7.1.3) Lifting the Stone (to find the center of gravity of a convex polygon)

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.