zoukankan      html  css  js  c++  java
  • 多边形求重心 HDU1115

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

    引用博客:https://blog.csdn.net/ysc504/article/details/8812339

     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 //②质量分布均匀
     9 //  特殊地,质量均匀的三角形重心:
    10 //  X = ( x0 + x1 + x2 ) / 3
    11 //  Y = ( y0 + y1 + y2 ) / 3 
    12 //③三角形面积公式:S =  ( (x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1) ) / 2 ;
    13 //因此做题步骤:1、将多边形分割成n-2个三角形,根据③公式求每个三角形面积。
    14 //                2、根据②求每个三角形重心。
    15 //                3、根据①求得多边形重心。
    16 #include <stdio.h>
    17 struct Point
    18 {
    19     double x, y;
    20 };
    21 double area(Point p1, Point p2, Point p3)
    22 {
    23     return ((p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y)) / 2;
    24 }
    25 int main()
    26 {
    27     int t, n, i;
    28     Point p1, p2, p3;
    29     double gx, gy, sumarea, temp;
    30     scanf("%d", &t);
    31     while (t--)
    32     {
    33         scanf("%d", &n);
    34         gx = gy = sumarea = 0;
    35         scanf("%lf%lf%lf%lf", &p1.x, &p1.y, &p2.x, &p2.y);
    36         for(i = 2; i < n; i++)
    37         {
    38             scanf("%lf%lf", &p3.x, &p3.y);
    39             temp = area(p1, p2, p3);
    40             gx += (p1.x + p2.x + p3.x) * temp;
    41             gy += (p1.y + p2.y + p3.y) * temp;
    42             sumarea += temp;
    43             p2 = p3;
    44         }
    45         gx = gx / sumarea / 3;
    46         gy = gy / sumarea / 3;
    47         printf("%.2lf %.2lf
    ", gx, gy);
    48     }
    49     return 0;
    50 }
    View Code
  • 相关阅读:
    Add Two Numbers
    Same Tree
    Single Number
    题目1190:大整数排序
    题目1182:统计单词
    题目1181:遍历链表
    题目1180:对称矩阵
    题目1179:阶乘
    题目1206:字符串连接
    HTML事件
  • 原文地址:https://www.cnblogs.com/yuanweidao/p/11182288.html
Copyright © 2011-2022 走看看