Polygon center of gravity, center of gravity

Source: Internet
Author: User
Tags cmath

Polygon center of gravity, center of gravity
Polygon Center of Gravity Problem time limit: 3000 MS | memory limit: 65535 KB difficulty: 5

Description
On a polygon, n points are obtained. The n points are given in order and adjacent points are connected in a straight line (the first and last connections ), all line segments do not overlap with other line segments, but can overlap. You can obtain the connected image of a polygon, a line segment, or a polygon and a line segment;
If it is a line segment, we define the area as 0 and the center of gravity coordinate as (). Now we want to calculate the sum of the area and the center of gravity of the image composed of the given point set;
Input
The first row has an integer of 0 <n <11, indicating that n groups of data exist;
The first row of each group of data has an integer m <10000, indicating that the polygon has m vertices;
Output
The sum of area and center of gravity of each polygon is output, and three digits are retained after the decimal point;
Sample Input
330 10 20 331 10 00 141 10 00 0.50 1
Sample output
0.000 0.0000.500 1.0000.500 1.000
Idea: Divide multiple variants into triangles for calculation.
Reference: Triangle Area: Click to open the link
Polygon Center of Gravity: Click to open the link
Result bits retained: Click to open the link
Code 1 (original) is as follows:
# Include <iostream> # include <cmath> # include <iomanip> using namespace std; int n, m; double * price; void cal () {// obtain the x coordinate double area = 0.0, x = 0.0, y = 0.0; for (int I = 0; I <(m-2); I ++) {// split into triangles to obtain an area of double temp = 0.0; double x1 = price [0]; // fixed point double y1 = price [1]; double x2 = price [2 + I * 2]; // The bright side that increases with I. double y2 = price [3 + I * 2]; double x3 = price [4 + I * 2]; double y3 = price [5 + I * 2]; // double temp = 0.5 * abs (x1 * y2 + x2 * y3 + x3 * y1-x1 * y3-x2 * y1-x3 * y2); temp = (x2-x1) * (y3-y1) -(x3-x1) * (y2-y1)/2; area + = temp; x + = (x1 + x2 + x3) * temp/3.0; y + = (y1 + y2 + y3) * temp/3.0;} if (fabs (area-0) <0.0000001) cout <"0.000 0.000" <endl; else cout <setiosflags (ios: fixed) <setprecision (3) <fabs (area) <"" <setiosflags (ios: fixed) <setprecision (3) <(x/area + y/area) <endl;} int main () {cin> n; // enter the number of groups to be computed while (n --) {cin> m; price = new double [m * 2]; for (int I = 0; I <m * 2; I ++) {cin> price [I]; // put all into a one-dimensional array} cal (); delete [] price ;} return 0 ;}
Code 2 (reference: Click to open the link)
#include<iostream>#include<cmath>#include<cstdio>using namespace std;struct Point{ double x,y;};int main(){    int n,t;    scanf("%d",&t);    while(t--){        scanf("%d",&n);        Point p1,p2,p3;        double gx,gy,sumarea;        gx=gy=sumarea=0;        scanf("%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y);        for(int i=2;i<n;i++){            scanf("%lf%lf",&p3.x,&p3.y);            double area=((p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y))/2;            gx+=(p1.x+p2.x+p3.x)*area;            gy+=(p1.y+p2.y+p3.y)*area;            sumarea+=area;            p2=p3;        }        gx=gx/sumarea/3;        gy=gy/sumarea/3;        if(fabs(sumarea - 0) < 0.0000001)puts("0.000 0.000");        else printf("%.3lf %.3lf\n",fabs(sumarea),gy+gx);    }    return 0;}
Code 3 (reference: Click to open the link)
# Include <iostream> # include <cstring> # include <cstdio> # include <cmath> # include <algorithm> using namespace std; # define INF 0.0000001 const int N = 10010; struct point {double x, y; point (): x (0), y (0) {}} p [N]; int main () {int ncase; scanf ("% d", & ncase); while (ncase --) {int n; double result = 0; point ans; scanf ("% d", & n ); for (int I = 0; I <n; ++ I) scanf ("% lf", & p [I]. x, & p [I]. y); for (int I = 1; I <= n; ++ I) {double temp = (p [I % n]. x * p [I-1]. y-p [I % n]. y * p [I-1]. x)/2.0; result + = temp; ans. x + = temp * (p [I % n]. x + p [I-1]. x)/3.0; ans. y + = temp * (p [I % n]. y + p [I-1]. y)/3.0;} if (fabs (result-0) <INF) puts ("0.000 0.000"); elseprintf ("%. 3lf %. 3lf \ n ", fabs (result), (ans. x + ans. y)/result); // result returns 0;}/* 172 34 53 47 84 7834 568 100 */

(Full text)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.