计算多边形重心的方法:
https://blog.csdn.net/qq_43408978/article/details/96990832
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#define eps 0.01
#define pi 3.1415926
using namespace std;
struct point {
double x, y;
point () {};
point (double _x, double _y): x(_x), y(_y) {};
void sc() {
scanf("%lf%lf", &x, &y);
}
point operator-(point p) {
return point(x - p.x, y - p.y);
}
double operator*(point p) {
return x * p.y - y * p.x;
}
} pts[1000000];
int main() {
int t, n, i;
double sumArea, ssumx, ssumy, area, x, y;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (i = 0; i < n; i++) {
pts[i].sc();
}
sumArea = 0;
ssumx = 0;
ssumy = 0;
for (i = 1; i <= n - 2; i++) {
area = (pts[i] - pts[0]) * (pts[i + 1] - pts[0]) / 2;
sumArea += area;
ssumx += area * (pts[0].x + pts[i].x + pts[i + 1].x);
ssumy += area * (pts[0].y + pts[i].y + pts[i + 1].y);
}
x = ssumx / (3.0 * sumArea);
y = ssumy / (3.0 * sumArea);
printf("%.2f %.2f\n", x, y);
}
}