Topic Meaning:
An n-shaped N-vertex is given to find the coordinate of the center of gravity of the N-side shape.
http://acm.hdu.edu.cn/showproblem.php?pid=1115
Topic Analysis:
/**
* Source: http://blog.csdn.net/ysc504/article/details/8812339
*① quality centered on vertices
* N Vertex coordinates (xi,yi), MI in mass, center of gravity
* X =∑ (XIXMI)/∑mi
* Y =∑ (YIXMI)/∑mi
* Specifically, if the quality of each point is the same, then
* X =∑xi/n
* Y =∑yi/n
*② Uniform mass Distribution
* Specially, the quality of the triangle center of mass evenly:
* X = (x0 + x1 + x2)/3
* Y = (y0 + y1 + y2)/3
*③ Triangular area formula: S = ((x2-x1) * (y3-y1)-(x3-x1) * (Y2-Y1))/2;
* To do the steps: 1, the polygon is divided into n-2 triangles, according to the ③ formula for each triangle area.
* 2, according to ② for each triangle center of gravity.
* 3, according to ① to obtain the polygon center of gravity.
**/
Now according to this algorithm to give two kinds of code, one is to n points, with one of the points as the standard, divided into n-2 triangle, and then to seek the center of gravity.
The other is based on the origin of the n+1 into a triangle, and then to seek the center of gravity.
AC Code:
The first type of code:
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace Std;
struct point{
Double x, y;
};
Double area (point p1,point p2,point p3) {//cross-multiply to find triangular areas
Return ((p2.x-p1.x) * (P3.Y-P1.Y)-(p3.x-p1.x) * (P2.Y-P1.Y))/2;
}
int main ()
{
int n,t;
scanf ("%d", &t);
while (t--) {
scanf ("%d", &n);
Point P1,p2,p3;
Double Gx,gy,sumarea;
gx=gy=sumarea=0;
scanf ("%lf%lf%lf%lf", &p1.x,&p1.y,&p2.x,&p2.y);
for (int i=2;i<n;i++) {//divided into n-2 triangles
scanf ("%lf%lf", &p3.x,&p3.y);
Double Area=area (P1,P2,P3);//area of a single triangle
gx+= (p1.x+p2.x+p3.x) *area;//center of gravity multiplied by its weight (area), because each one is divided by 3, the hospital is placed in the last
gy+= (P1.Y+P2.Y+P3.Y) *area;
sumarea+=area;//Calculating Ownership values
p2=p3;//replace P2, calculate next triangle
}
The polygon center of gravity of gx=gx/sumarea/3;//
GY=GY/SUMAREA/3;
printf ("%.2lf%.2lf\n", gx,gy);
}
return 0;
}
The second type of code:
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace Std;
struct point{
Double x, y;
}P[10005];
Double area (point p1,point p2,point p3) {//cross-multiply to find triangular areas
Return ((p2.x-p1.x) * (P3.Y-P1.Y)-(p3.x-p1.x) * (P2.Y-P1.Y))/2.0;
}
int main ()
{
int n,t;
Point P0;
p0.x=p0.y=0.0;
scanf ("%d", &t);
while (t--) {
scanf ("%d", &n);
Double Gx,gy,sumarea,area;
gx=gy=sumarea=0;
for (int i = 0; i < n; ++i)
scanf ("%lf%lf", &p[i].x, &P[I].Y);
for (int i=1;i<=n;i++) {
Area=area (p0,p[i%n],p[i-1]);//The area of the single triangle with the origin
gx+= (p[i%n].x+p[i-1].x) *area;//center of gravity multiplied by its weight (area), because each one is divided by 3, the hospital is placed in the last
gy+= (P[I%N].Y+P[I-1].Y) *area;
sumarea+=area;//Calculating Ownership values
}
gx=gx/(sumarea*3);//polygon center of gravity to be asked
gy=gy/(sumarea*3);
printf ("%.2lf%.2lf\n", gx,gy);
}
return 0;
}
hdu1115 (Polygon centroid algorithm)