Lifting the stone (calculate the center of gravity of any polygon)

Source: Internet
Author: User
Lifting the stone Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 5432 accepted submission (s): 2273


Problem descriptionthere are eclipsecret openings in the floor which are covered by a big heavy stone. when the stone is lifted up, a special mechanic detects this and activates initialize ONED arrows that are shot near the opening. the only possibility is 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 be lifted all at once; no side can rise before another. so it is very important to find the center of gravity and connect the rope exactly to that point. the stone has a polygonal shape and its height is the same throughout the whole polygonal area. your task is to find the center of gravity for the given polygon.
 
Inputthe input consists of T test cases. the number of them (t) is given on 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 is followed by n lines, each containing two integers XI and Yi (| Xi |, | Yi | <= 20000 ). these numbers are the coordinates of the I-th point. when we connect the points in the given order, we get a polygon. you may assume that the edges never touch each other (could t the Neighboring ones) and that they never cross. the area of the polygon is never zero, I. e. it cannot collapse into a single line.
 
Outputprint exactly one line for each test case. the line shoshould contain exactly two numbers separated by one space. these numbers are the coordinates of the center of gravity. round the coordinates to the nearest number with exactly two digits after the decimal point (0.005 rounds up to 0.01 ). note that the center of gravity may be outside the polygon, if its shape is not convex. if there is such a case in the input data, print the center 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

Question: Calculate the center of gravity of a polygon;

Description:
Theorem 1 we know the vertex coordinate AI (XI, Yi) (I = 1, 2, 3) of the triangle △a1a2a3 ). Its center of gravity coordinates are:

XG = (X1 + X2 + X3)/3; YG = (Y1 + y2 + Y3)/3;


Theorem 2: divide a Polygon into N small triangle areas. Each small area has a area of σ I and a center of gravity of GI (. XI,. Yi ).
Sum:

    

AC code:

# Include <iostream> # include <cstdio> # include <cmath> using namespace STD; struct point {Double X, Y; point (double x = 0, Double Y = 0 ): x (x), y (y) {}; void read () {scanf ("% lf", & X, & Y );} point operator-(const point & A) const // overload minus sign; {return point (. x-X,. y-y) ;}}; double cross (point a, point B) // cross product of the vector; {return. x * B. y-. y * B. x;} double area (point a, point B, point C) // Triangle Area Formula 1/2 * Cross (point a, point B) {point A, B; A = B-A; B = C-A; return cross (a, B); // you do not need to divide it by 2 because it is offset in the final result ;} int main () {int t, n; scanf ("% d", & T); While (t --) {scanf ("% d", & N ); point P0, P1, P2; double sum_x, sum_y, sum_area; consumed read (); p1.read (); sum_x = sum_y = sum_area = 0.0; For (INT I = 2; I <n; I ++) {p2.read (); double TP = Area (P0, P1, P2); sum_x + = (bytes X + p1.x + p2.x) * TP; sum_y + = (p0.y + p1.y + p2.y) * TP; sum_area + = TP; P1 = P2;} printf ("%. 2lf %. 2lf \ n ", sum_x/sum_area/3.0, sum_y/sum_area/3.0);} return 0 ;}




 

Lifting the stone (calculate the center of gravity of any 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.