Test instructions: To find the center of gravity of an irregular simple polygon.
Solution: The centroid of the polygon is the weighted average of the area of the center of gravity of all triangles.
The article about finding the center of the polygon: finding the center of gravity
You can do it with a cross product.
Code:
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#defineMod 1000000007#defineEPS 1e-8using namespacestd;structpoint{Doublex, y; Point (Doublex=0,Doubley=0): X (x), Y (y) {}voidInput () {scanf ("%LF%LF",&x,&y); }};typedef point Vector;intDCMP (Doublex) {if(x <-eps)return-1; if(X > EPS)return 1; return 0;} Vectoroperator+ (vector A, vector B) {returnVector (a.x + b.x, A.Y +b.y); }vectoroperator-(vector A, vector B) {returnVector (a.x-b.x, A.Y-b.y); }vectoroperator* (Vector A,DoubleP) {returnVector (A.x*p, a.y*p); }vectoroperator/(Vector A,DoubleP) {returnVector (a.x/p, a.y/p); }DoubleCross (vector A, vector B) {returna.x*b.y-a.y*b.x;}DoubleCalcconvexarea (point* p,intN) {//Convex bag Area DoubleArea =0.0; for(intI=1; i<n-1; i++) Area+ = Cross (p[i]-p[0],p[i+1]-p[0]); returnarea*0.5;} Point p[106],ch[106];intMain () {intN,i,j,cs =1; while(SCANF ("%d", &n)!=eof &&N) { for(i=0; i<n;i++) P[i].input (); DoubleS =Calcconvexarea (p,n); DoubleX =0.0, Y =0.0; for(i=2; i<n;i++) { DoubleArea =0.5*cross (p[i-1]-p[0],p[i]-p[0]); X+ = area* (p[0].x+p[i-1].x+p[i].x)/3.0; Y+ = area* (p[0].y+p[i-1].Y+P[I].Y)/3.0; } printf ("Stage #%d:%.6f%.6f\n", cs++,x/s,y/S); } return 0;}
View Code
Uvalive 4426 Blast the enemy! --Seeking polygon center of gravity