POJ-1385 Lifting the Stone 计算几何 多边形重心
程序员文章站
2022-04-01 15:50:40
...
POJ-1385 Lifting the Stone
题意: 计算多边形的重心。
分析: 通过计算其中三角形的加权重心得到整个多边形的重心。POJ(g++ wa, c++ ac)很迷。
代码:
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
using namespace std;
const int MAXN = 1e6 + 7;
struct Point
{
long long x, y;
Point () {}
Point (long long _x, long long _y)
{
x = _x;
y = _y;
}
Point operator - (const Point b) const
{
return Point (x - b.x, y - b.y);
}
long long operator ^ (const Point b) const
{
return x * b.y - y * b.x;
}
Point operator + (const Point b) const
{
return Point (x + b.x, y + b.y);
}
};
Point p[MAXN];
int n;
int main ()
{
int t;
scanf ("%d", &t);
while (t--)
{
scanf ("%d", &n);
for (int i = 0; i < n; i++)
{
scanf ("%lld%lld", &p[i].x, &p[i].y);
}
Point ans (0, 0);
long long s = 0;
for (int i = 0; i < n; i++)
{
long long ss = p[i]^p[(i + 1)%n];
s += ss;
Point tmp = p[i] + p[(i + 1)%n];
ans.x += tmp.x * ss;
ans.y += tmp.y * ss;
}
printf ("%.2lf %.2lf\n", 1.0*ans.x/(3l*s), 1.0*ans.y/(3l*s));
}
return 0;
}