Http://acm.nyist.net/JudgeOnline/problem.php? PID = 3

Source: Internet
Author: User

Calculate the polygon center of gravity in a geometric way,

It is known that a polygon has no edges and the quality distribution is even. Returns the coordinates of vertices of a polygon in sequence, and returns the center of gravity.

1. The quality is concentrated on the vertex. The coordinates of N vertices are (XI, Yi), and the weight is Mi, then the center of gravity
X = sigma (XI x mi)/SIGMA Mi
Y = Σ (yi x mi)/Σ Mi
In particular, if the quality of each point is the same
X = Σ XI/n
Y = Σ Yi/n

2. uniform quality distribution. This is the type of question, and the algorithm is different from the above.
In particular, the triangle center of gravity with uniform quality:
X = (x0 + X1 + x2)/3
Y = (y0 + Y1 + y2)/3

These two methods are used. First, the polygon is divided into multiple triangles, and the area and center of gravity of each triangle are obtained. Then, the center of gravity of each triangle is the vertex, A new polygon is constructed, and the polygon has the same center of gravity as the original polygon, but the quality of the polygon is concentrated on the vertex. Therefore, the first method can be used to obtain the polygon, because the quality is proportional to the area, it can be converted to the triangle area. As for how to calculate the triangle area, you can use cross product. Note that when the triangle is outside the polygon, It is negative, the method for judging whether the Triangle Area is positive or negative can also be determined by the right hand spiral. If the Triangle Area is clockwise, the negative area is positive counterclockwise.

AC code:

# Include <cstdio> # include <cmath> # include <string> # define exp 1e-7using namespace STD; typedef struct STR {Double X; Double Y; STR (): X (0.0), y (0.0) {}} point; point S [10005]; int main () {int t; scanf ("% d", & T ); while (t --) {int N; scanf ("% d", & N); For (INT I = 0; I <n; ++ I) scanf ("% lf", & S [I]. x, & S [I]. y); double sum = 0.0; point cur; For (INT I = 1; I <= N; ++ I) {double temp = (s [I % N]. x * s [I-1]. y-s [I-1]. x * s [I % N]. y)/2; // sum + = temp; cur. X + = temp * (s [I % N]. X + s [I-1]. x)/3.0; cur. Y + = temp * (s [I % N]. Y + s [I-1]. y)/3.0;} If (FABS (Sum-0.0) <exp) printf ("0.000 0.000 \ n"); else printf ("%. 3lf %. 3lf \ n ", sum, (cur. X + cur. y)/SUM);} 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.