Can someone tell me why the output of sum_area is always 0 (the polygon center of gravity) and the sum_area center of gravity?
Polygon Center of Gravity Problem time limit: 3000 MS | memory limit: 65535 KB difficulty: 5
-
Description
-
On a polygon, n points are obtained. The n points are given in order and adjacent points are connected in a straight line (the first and last connections ), all line segments do not overlap with other line segments, but can overlap. You can obtain the connected image of a polygon, a line segment, or a polygon and a line segment;
If it is a line segment, we define the area as 0 and the center of gravity coordinate as (). Now we want to calculate the sum of the area and the center of gravity of the image composed of the given point set;
-
Input
-
The first row has an integer of 0 <n <11, indicating that n groups of data exist;
The first row of each group of data has an integer m <10000, indicating that the polygon has m vertices;
-
Output
-
The sum of area and center of gravity of each polygon is output, and three digits are retained after the decimal point;
-
Sample Input
-
330 10 20 331 10 00 141 10 00 0.50 1
-
Sample output
-
0.000 0.0000.500 1.0000.500 1.000
# Include <iostream>
# Include <cmath>
# Include <vector>
Using namespace std;
Struct Poit
{
Double x;
Double y;
};
Double Area_3 (Poit C, Poit A, Poit B );
Int main ()
{
Int m;
Cin> m;
For (int I = 0; I <m; I ++)
{
Int n;
Cin> n;
Vector <Poit>;
Poit G = {0, 0}; // G is the center of gravity
Double sum_area = 0; // total area
Double Gxy; // the x-axis and Y-axis of the center of gravity
For (int j = 0; j <n; j ++)
{
Int x0, y0;
Cin> x0> y0;
Poit p = {x0, y0 };
A. push_back (p );
}
For (int k = 0; k <n; k ++)
{
G. x = G. x + a [k]. x;
G. y = G. y + a [k]. y;
}
G. x = G. x/n;
G. y = G. y/n;
Gxy = G. x + G. y;
A. push_back (a [0]); // The final sum and zero weight;
For (int h = 0; h <n; h ++) // calculates the total area.
{
Sum_area = sum_area + Area_3 (a [h], a [h + 1], G );
}
Cout <sum_area <"" <Gxy <endl;
}
Return 0;
}
Double Area_3 (Poit C, Poit A, Poit B) // to ensure that angle A is acute and B is the center of gravity;
{
Double a, B, c;
Double area;
A = sqrt (pow (B. x-C.x), 2) + pow (B. y-C.y), 2 ));
B = sqrt (pow (A. x-C.x), 2) + pow (A. y-C.y), 2 ));
C = sqrt (pow (A. x-B.x), 2) + pow (A. y-B.y), 2 ));
Double cos_A = (pow (B, 2) + pow (c, 2)-pow (a, 2)/(2 * B * c );
Double sin_A = abs (sqrt (1-pow (cos_A, 2 )));
Area = 0.5 * B * c * sin_A;
Return area;
}