zoukankan      html  css  js  c++  java
  • *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
     
    题意:
    计算多边形的重心。
    代码:

      

                

     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
    ",sumx/sumarea,sumy/sumarea);  
    50     }
    51     return 0;
    52 }
  • 相关阅读:
    js setTimeout的第三个参数
    vue 实现跑马灯 transform
    vue 使用闭包实现防抖
    js 获取输入日期的几个月前的日期
    js 作用域和作用域链
    退役划水(10)
    退役划水(9)
    解决 SpringBoot Elasticsearch 7.x 聚合查询遇到的问题
    ElasticSearch7.4.2:RestHighLevelClient应用
    RestHighLevelClient操作ES的API
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6080203.html
Copyright © 2011-2022 走看看