/******
题意:给你一个多边形,要你求出他的重心;
思路:把多边形分为n - 2个三角形,每个三角形重心为pi,面积为si,多边形重心G = sum{pi * si} / sum{si}
*******/
#include <iostream>
#include <cstdio>
using namespace std;
struct Point
{
int x, y;
Point operator-(const Point &t)const {return (Point){x - t.x, y - t.y};}
}p[1000009];
int cross(const Point &a, const Point &b){return a.x * b.y - a.y * b.x;}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int n, s, i;
double x, y, ax = 0.0, ay = 0.0, as = 0.0;
scanf("%d", &n);
for(i = 0; i < n; i++)
{
scanf("%d %d", &p[i].x, &p[i].y);
if(i < 2) continue;
s = cross(p[i] - p[0], p[i-1] - p[0]);
x = (double)(p[0].x + p[i-1].x + p[i].x) / 3.0;
y = (double)(p[0].y + p[i-1].y + p[i].y) / 3.0;
ax += (double)s * x;
ay += (double)s * y;
as += (double)s;
}
printf("%.2f %.2f\n", 1.0 * ax / as + 1e-8, 1.0 * ay / as + 1e-8);
}
return 0;
}
hdu1115 求多边形重心
最新推荐文章于 2019-08-10 22:58:06 发布