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): 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
     

    多边形重心模板

    解法:将多边形切分成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
    ",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
    ",area,resultx+resulty);
        }
        return 0;
    }
    趁着还有梦想、将AC进行到底~~~by 452181625
  • 相关阅读:
    matcaffe的blob维度顺序
    ftp同步代码
    SpringCloud的微服务网关:zuul(实践)
    SpringCloud的微服务网关:zuul(理论)
    SpringCloud的服务消费者 (二):(rest+feign/ribbon)声明式访问注册的微服务
    SpringCloud的服务消费者 (一):(rest+ribbon)访问注册的微服务
    SpringCloud的服务注册中心(二)注册中心服务端和两个微服务应用客户端
    Jetty入门(1-3)Eclipse集成gradle-Gretty插件或maven-jetty插件运行应用
    HTTP协议扫盲(三)HTTP协议的请求头列表和分类描述
    Jetty入门(1-2)eclipse集成jetty插件并发布运行应用
  • 原文地址:https://www.cnblogs.com/hate13/p/4152302.html
Copyright © 2011-2022 走看看