*HDU 1115 计算几何

Lifting the Stone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7674    Accepted Submission(s): 3252

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

题意:

计算多边形的重心。

代码:

  

 1 //1.  质量集中在顶点上
 2 //    n个顶点坐标为(xi,yi),质量为mi,则重心
 3 //  X = ∑( xi×mi ) / ∑mi
 4 //  Y = ∑( yi×mi ) / ∑mi
 5 //  特殊地,若每个点的质量相同,则
 6 //  X = ∑xi / n
 7 //  Y = ∑yi / n
 8 //2.  质量分布均匀
 9 //  特殊地,质量均匀的三角形重心:
10 //  X = ( x0 + x1 + x2 ) / 3
11 //  Y = ( y0 + y1 + y2 ) / 3
12 //3.  三角形面积公式:S =  ( (x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1) ) / 2 ; 向量p2p1与向量p3p1叉积/2。
13 //因此做题步骤:1、将多边形分割成n-2个三角形,根据3公式求每个三角形面积。  //用向量面积 凹多边形时面积会在多边形外面。
14 //              2、根据2求每个三角形重心。
15 //              3、根据1求得多边形重心。      //当总面积是0的情况时注意后面除总面积。
16 #include<iostream>
17 #include<cstdio>
18 #include<cstring>
19 #include<cmath>
20 using namespace std;
21 struct nod
22 {
23     double x,y;
24 };
25 double getarea(nod p0,nod p1,nod p2)
26 {
27     return ((p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x))/2;
28 }
29 int main()
30 {
31     int t,n;
32     nod p0,p1,p2;
33     scanf("%d",&t);
34     while(t--)
35     {
36         scanf("%d",&n);
37         scanf("%lf%lf",&p0.x,&p0.y);
38         scanf("%lf%lf",&p1.x,&p1.y);
39         double sumarea=0,sumx=0,sumy=0;
40         for(int i=3;i<=n;i++)
41         {
42             scanf("%lf%lf",&p2.x,&p2.y);
43             double Area=getarea(p0,p1,p2);
44             sumarea+=Area;
45             sumx+=(p0.x+p1.x+p2.x)*Area/3;
46             sumy+=(p0.y+p1.y+p2.y)*Area/3;
47             p1=p2;
48         }
49         printf("%.2lf %.2lf\n",sumx/sumarea,sumy/sumarea);
50     }
51     return 0;
52 }
时间: 2025-01-07 11:01:59

*HDU 1115 计算几何的相关文章

hdu 1115 Lifting the Stone (数学几何)

Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5203    Accepted Submission(s): 2155 Problem Description There are many secret openings in the floor which are covered by a big

hdu 1616 计算几何 凸包

题意是一个世界有许多个国家,每个国家有N个建筑,包括一个发电站和N-1个用电建筑,所有建筑围成的凸包就是这个国家的面积.一枚导弹如果在一个国家之内爆炸则可以使这个国家停电. step 1:求出每个国家的凸包(我用水平排序就是各种坑,改叉乘排序才过,主要是后面求面积的时候需要这个叉乘排序的信息). step 2:判断每枚导弹是否在这个国家的范围之内. step 3:求出所有停电的国家的面积. 就是计算几何的综合模拟水题,坑点就是要小心(QAQ||写计算几何的题目都是要小心). 传送门:http:/

HDU 1115 Lifting the Stone

Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5440    Accepted Submission(s): 2278 Problem Description There are many secret openings in the floor which are covered by a big

hdu 3320 计算几何(三维图形几何变换)

openGL Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 170    Accepted Submission(s): 77 Problem Description Jiaoshou selected a course about “openGL” this semester. He was quite interested in m

HDU 5784 (计算几何)

Problem How Many Triangles (HDU 5784) 题目大意 给定平面上的n个点(n<2000),询问可以组成多少个锐角三角形. 解题分析 直接统计锐角三角形较困难,考虑问题的反面,统计直角三角形.钝角三角形.平角三角形(暂时这么叫吧QAQ). 首先枚举三角形的一个端点A,对其他点进行象限为第一关键字,极角为第二关键字排序. 然后使用三个指针,进行O(n)的扫描. 具体做法为用 i 指针指向三角形的第二个端点B.我们可以假想通过平移和旋转,把A点置于平面直角坐标系的原点,

2017-03-18 HDU 5733 计算几何 codeforces 599E 状压dp(待补)

HDU 5733 题意:给出四面体的四个顶点,求出其内切球的球心坐标和半径,如果不存在内切球,输出"O O O O". tags:一堆公式..可以做模板了 我们可以将平面上的四点得到由同一个点出发的三个矢量.这样就可以计算这三个矢量的混合积M,则M/6即为四面体体积V. 题目无解的情况当且仅当四点共面,即混合积为0. 求得四面体体积后,可以根据公式r = 3V/(S1+S2+S3+S4)得到内切球半径, S1~S4为四面体四个面的面积. 当前的问题转化为如何求四面体四个面的面积. 由于

hdu 1558(计算几何+并查集)

Segment set Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4479    Accepted Submission(s): 1672 Problem Description A segment and all segments which are connected with it compose a segment set.

*HDU 1086 计算几何

You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10259    Accepted Submission(s): 5074 Problem Description Many geometry(几何)problems were designed in the ACM/

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