hdu1115 (Polygon center of the gravity algorithm)

Source: Internet
Author: User
Tags cmath

Meaning of the title:

Given an n just n vertex. This is achieved by the N-point coordinate polygon.

Http://acm.hdu.edu.cn/showproblem.php?

pid=1115


Topic Analysis:

/**

* Source: http://blog.csdn.net/ysc504/article/details/8812339

*① quality centered on vertices
* N Vertex coordinates are (xi,yi). The mass is MI, then the center of gravity
* X =∑ (XIXMI)/∑mi
* Y =∑ (YIXMI)/∑mi
* in particular. If the quality of each point is the same. The
* X =∑xi/n
* Y =∑yi/n
*② Uniform mass Distribution
* in particular. Uniform Triangular center of mass:
* X = (x0 + x1 + x2)/3
* Y = (y0 + y1 + y2)/3
*③ Triangular area formula: S = ((x2-x1) * (y3-y1)-(x3-x1) * (Y2-Y1))/2;
* Do the steps: 1, the polygon is cut into a n-2 triangle. The area of each triangle is calculated according to the ③ formula.
* 2, according to ② to seek each triangle center of gravity.
* 3, according to ① to obtain the polygon center of gravity.
**/

Now, based on this algorithm, two kinds of code are given, one is n points. Take one of the points as the standard. into a n-2 triangle. And then the center of gravity.

The other is to divide the n+1 triangle into the original point, then the center of gravity.


AC Code:

The first type of code:

#include <iostream>
#include <cmath>
#include <cstdio>
using namespace Std;
struct point{
Double x, y;
};
Double area (point p1,point p2,point p3) {//cross-multiply to find triangular areas
Return ((p2.x-p1.x) * (P3.Y-P1.Y)-(p3.x-p1.x) * (P2.Y-P1.Y))/2;
}
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++) {//divided into n-2 triangles
scanf ("%lf%lf", &p3.x,&p3.y);
Double Area=area (P1,P2,P3);//area of a single triangle
gx+= (p1.x+p2.x+p3.x) *area;//center of gravity multiplied by its weight (area), since each was divided by 3, the hospital was placed in the last
gy+= (P1.Y+P2.Y+P3.Y) *area;
sumarea+=area;//Calculating all weights
p2=p3;//replace P2, calculate next triangle
}
The polygon center of gravity of gx=gx/sumarea/3;//
GY=GY/SUMAREA/3;
printf ("%.2lf%.2lf\n", gx,gy);
}
return 0;
}

Another type of code:

#include <iostream>
#include <cmath>
#include <cstdio>
using namespace Std;
struct point{
Double x, y;
}P[10005];
Double area (point p1,point p2,point p3) {//cross-multiply to find triangular areas
Return ((p2.x-p1.x) * (P3.Y-P1.Y)-(p3.x-p1.x) * (P2.Y-P1.Y))/2.0;
}
int main ()
{
int n,t;
Point P0;
p0.x=p0.y=0.0;
scanf ("%d", &t);
while (t--) {
scanf ("%d", &n);
Double Gx,gy,sumarea,area;
gx=gy=sumarea=0;
for (int i = 0; i < n; ++i)
scanf ("%lf%lf", &p[i].x, &AMP;P[I].Y);
for (int i=1;i<=n;i++) {
Area=area (p0,p[i%n],p[i-1]);//The area of the single triangle with the origin
gx+= (p[i%n].x+p[i-1].x) *area;//center of gravity multiplied by its weight (area), since each was divided by 3, the hospital was placed in the last
gy+= (P[I%N].Y+P[I-1].Y) *area;
sumarea+=area;//Calculating all weights
}
gx=gx/(sumarea*3);//polygon center of gravity to be asked
gy=gy/(sumarea*3);
printf ("%.2lf%.2lf\n", gx,gy);
}
return 0;
}


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

hdu1115 (Polygon center of the gravity algorithm)

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.