HDU - 1115 Lifting the Stone (计算几何+重心)

http://acm.hdu.edu.cn/showproblem.php?pid=1115

题意:

就是一个裸的求重心

思路:

把一个n边形换化成n-2个三角形,分别求这n-2个三角形的重心坐标,并把这个重心赋予权值,就变成了一个重量全在定点上的图形求重心。
三角心求重心X = (x1+x2+x3)/3,Y = (y1+y2+y3)/3
一个n个顶点重量全在顶点上的多边形重心为

x = i = 1 n ( x i a r e a i ) i = 1 n a r e a i
y = i = 1 n ( y i a r e a i ) i = 1 n a r e a i

area为三角形的面积,即每个顶点的权值

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <string.h>
#include <queue>
#include <stack>
#include <deque>
#include <stdlib.h>
#include <bitset>

using namespace std;

#define ll long long
#define ull unsigned long long
const int maxn = 1e6 + 5;
const int M = 1e9 + 7;

struct Point {
    double x, y;
    Point friend operator - (Point a, Point b) {
        return {a.x - b.x, a.y - b.y};
    }
}a, b, c;

double X(Point a, Point b) {
    return a.x * b.y - a.y * b.x;
}

double each_sta(Point a, Point b, Point c) {
    return X(b - a, c - a)/2.;
}

double sumx, sumy, sumarea;

int main(int argc, const char * argv[]) {
    int T;
    scanf("%d", &T);
    while(T--) {
        int N;
        scanf("%d", &N);
        scanf("%lf %lf", &a.x, &a.y);
        scanf("%lf %lf", &b.x, &b.y);
        sumx = sumy = sumarea = 0;
        for (int i = 2; i < N; i ++) {
            scanf("%lf %lf", &c.x, &c.y);
            double s = each_sta(a, b, c);
            sumarea += s;
            sumx += (a.x + b.x + c.x) * s;
            sumy += (a.y + b.y + c.y) * s;
            b = c;
        }
        printf("%.2lf %.2lf\n", sumx/(3 * sumarea), sumy/(3 * sumarea));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/henu_jizhideqingwa/article/details/81565164