1. 程式人生 > >POJ-1385 Lifting the Stone 計算幾何 多邊形重心

POJ-1385 Lifting the Stone 計算幾何 多邊形重心

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; }