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

Source: Internet
Author: User

Lifting the Stone (calculate the center of gravity of any polygon)
Lifting the StoneTime 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
 
  
# Include
  
   
# Include
   
    
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 ;}
   
  
 




 

Related Article

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.