多边形重心模板

HDU 1115

Lifting the Stone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 5719    Accepted Submission(s): 2391

Problem Description

There are many secret openings in the floor which are covered by a big heavy stone. When the stone is lifted up, a special mechanism detects this and activates poisoned arrows that are shot near the opening. The only possibility is to lift the stone very slowly and carefully. The ACM team must connect a rope to the stone and then lift it using a pulley. Moreover, the stone must be lifted all at once; no side can rise before another. So it is very important to find the centre of gravity and connect the rope exactly to that point. The stone has a polygonal shape and its height is the same throughout the whole polygonal area. Your task is to find the centre of gravity for the given polygon.

Input

The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer N (3 <= N <= 1000000) indicating the number of points that form the polygon. This is followed by N lines, each containing two integers Xi and Yi (|Xi|, |Yi| <= 20000). These numbers are the coordinates of the i-th point. When we connect the points in the given order, we get a polygon. You may assume that the edges never touch each other (except the neighboring ones) and that they never cross. The area of the polygon is never zero, i.e. it cannot collapse into a single line.

Output

Print exactly one line for each test case. The line should contain exactly two numbers separated by one space. These numbers are the coordinates of the centre of gravity. Round the coordinates to the nearest number with exactly two digits after the decimal point (0.005 rounds up to 0.01). Note that the centre of gravity may be outside the polygon, if its shape is not convex. If there is such a case in the input data, print the centre anyway.

Sample Input

2
4
5 0
0 5
-5 0
0 -5
4
1 1
11 1
11 11
1 11

Sample Output

0.00 0.00
6.00 6.00

Source

Central Europe 1999

解法:将多边形切分成n-2个三角形,然后分别算出每个三角形的重心和面积,中心通过三个顶点坐标取得,面积通过叉积算出。

则N边形的重心x坐标为 reultx = (Σ(Si * Pi.x))/S .......Σ是n-2个多边形的面积,Si表示是第i块三角形的面积,Pi

表示第i块三角形的重心的x坐标。同理可得重心y坐标。

由于在计算重心的时候每次都要除以3,则放到最后再除。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
#define N 1000010

struct Point
{
    double x,y;
    Point (){}
    Point (double x,double y):x(x),y(y){}
    Point operator - ( Point p){
        return Point(x-p.x,y-p.y);
    }
    double operator ^ ( Point p){
        return x*p.y-y*p.x;
    }
    double operator * ( Point p){
        return x*p.x+y*p.y;
    }
};

int n;
Point p[N];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf",&p[i].x,&p[i].y);
        }
        double area=0;
        double sumx=0;
        double sumy=0;
        for(int i=2;i<n;i++)
        {
            double areaTmp=((p[i]-p[0])^(p[i-1]-p[0]))/2;
            area+=areaTmp;
            sumx+=areaTmp*(p[0].x+p[i-1].x+p[i].x);
            sumy+=areaTmp*(p[0].y+p[i-1].y+p[i].y);
        }
        double resultx=sumx/area/3;
        double resulty=sumy/area/3;
        //if(resultx==0) resultx=0;
        //if(resulty==0) resulty=0;  防止输出-0.00
        printf("%.2f %.2f\n",resultx,resulty);
    }
    return 0;
}

NYOJ 3

多边形重心问题

时间限制:3000 ms  |  内存限制:65535 KB

难度:5

描述
在某个多边形上,取n个点,这n个点顺序给出,按照给出顺序将相邻的点用直线连接, (第一个和最后一个连接),所有线段不和其他线段相交,但是可以重合,可得到一个多边形或一条线段或一个多边形和一个线段的连接后的图形;  如果是一条线段,我们定义面积为0,重心坐标为(0,0).现在求给出的点集组成的图形的面积和重心横纵坐标的和;
输入
第一行有一个整数0<n<11,表示有n组数据; 每组数据第一行有一个整数m<10000,表示有这个多边形有m个顶点;
输出
输出每个多边形的面积、重心横纵坐标的和,小数点后保留三位;
样例输入
3
3
0 1
0 2
0 3
3
1 1
0 0
0 1
4
1 1
0 0
0 0.5
0 1
样例输出
0.000 0.000
0.500 1.000
0.500 1.000
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
#define N 10010

struct Point
{
    double x,y;
    Point (){}
    Point (double x,double y):x(x),y(y){}
    Point operator - ( Point &p){
        return Point(x-p.x,y-p.y);
    }
    double operator ^ ( Point &p){
        return x*p.y-y*p.x;
    }
    double operator * ( Point &p){
        return x*p.x+y*p.y;
    }
};

int n;
Point p[N];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf",&p[i].x,&p[i].y);
        }
        double area=0;
        double sumx=0;
        double sumy=0;
        for(int i=2;i<n;i++)
        {
            double areaTmp=((p[i]-p[0])^(p[i-1]-p[0]))/2;
            area+=areaTmp;
            sumx+=areaTmp*(p[0].x+p[i-1].x+p[i].x);
            sumy+=areaTmp*(p[0].y+p[i-1].y+p[i].y);
        }
        double resultx=sumx/area/3;
        double resulty=sumy/area/3;
        if(area==0) resultx=resulty=0;
        printf("%.3f %.3f\n",area,resultx+resulty);
    }
    return 0;
}
时间: 2024-10-11 17:36:15

多边形重心模板的相关文章

hdu1115 Lifting the Stone(几何,求多边形重心模板题)

题意:就是给你一个多边行的点的坐标,求此多边形的重心. 一道求多边形重心的模板题! #include<cstdio> #include<cmath> #include<cstring> using namespace std; struct point { double x,y; }PP[1000047]; point bcenter(point pnt[],int n){ point p,s; double tp,area = 0, tpx=0, tpy=0; p.x

谁能告诉我为什么sum_area输出总是0(多边形重心问题)

多边形重心问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 在某个多边形上,取n个点,这n个点顺序给出,按照给出顺序将相邻的点用直线连接, (第一个和最后一个连接),所有线段不和其他线段相交,但是可以重合,可得到一个多边形或一条线段或一个多边形和一个线段的连接后的图形: 如果是一条线段,我们定义面积为0,重心坐标为(0,0).现在求给出的点集组成的图形的面积和重心横纵坐标的和: 输入 第一行有一个整数0<n<11,表示有n组数据:每组数据第一行有一个整数m&l

hdu1115(多边形重心算法)

题目意思: 给出一个n边形的n个顶点,求出这个n边形的重心坐标. http://acm.hdu.edu.cn/showproblem.php?pid=1115 题目分析: /** *出处:http://blog.csdn.net/ysc504/article/details/8812339 *①质量集中在顶点上 *  n个顶点坐标为(xi,yi),质量为mi,则重心 * X = ∑( xi×mi ) / ∑mi * Y = ∑( yi×mi ) / ∑mi * 特殊地,若每个点的质量相同,则 *

NYOJ3(多边形重心)

题目意思: 在某个多边形上,取n个点,这n个点顺序给出,按照给出顺序将相邻的点用直线连接, (第一个和最后一个连接),所有线段不和其他线段相交,但是可以重合,可得到一个多边形或一条线段或一个多边形和一个线段的连接后的图形: 如果是一条线段,我们定义面积为0,重心坐标为(0,0).现在求给出的点集组成的图形的面积和重心横纵坐标的和. http://acm.nyist.net/JudgeOnline/problem.php?pid=3 输入 第一行有一个整数0<n<11,表示有n组数据: 每组数据

多边形重心问题

多边形重心问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 在某个多边形上,取n个点,这n个点顺序给出,按照给出顺序将相邻的点用直线连接, (第一个和最后一个连接),所有线段不和其他线段相交,但是可以重合,可得到一个多边形或一条线段或一个多边形和一个线段的连接后的图形: 如果是一条线段,我们定义面积为0,重心坐标为(0,0).现在求给出的点集组成的图形的面积和重心横纵坐标的和: 输入 第一行有一个整数0<n<11,表示有n组数据:每组数据第一行有一个整数m&l

Hdu 3685 Rotational Painting(多边形重心+凸包)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3685 思路:先求出多边形重心,放置的边一定为凸包边.判断重心是否落在边之间(求点到直线与点到线段的距离,判断). 4 0 0 4 0 8 4 4 4 注意这种情况,重心不能在凸包边端点的垂线上. #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using name

hdu 3685 多边形重心+凸包

Rotational Painting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2498    Accepted Submission(s): 702 Problem Description Josh Lyman is a gifted painter. One of his great works is a glass pain

hdu3685 Rotational Painting 求多边形重心和凸包

http://acm.hdu.edu.cn/showproblem.php?pid=3685 Rotational Painting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2614    Accepted Submission(s): 737 Problem Description Josh Lyman is a gifted

hdu 1115(计算多边形重心)

题意:已知一多边形没有边相交,质量分布均匀.顺序给出多边形的顶点坐标,求其重心. 分析: 求多边形重心的题目大致有这么几种: 1,质量集中在顶点上.n个顶点坐标为(xi,yi),质量为mi,则重心 X = ∑( xi×mi ) / ∑mi Y = ∑( yi×mi ) / ∑mi 特殊地,若每个点的质量相同,则 X = ∑xi / n Y = ∑yi / n 2,质量分布均匀.这个题就是这一类型,算法和上面的不同. 特殊地,质量均匀的三角形重心: X = ( x0 + x1 + x2 ) / 3