Title Link: http://poj.org/problem?id=1385
Title: Give you a polygon point, to seek the center of gravity.
First, the center of gravity of the triangle: ((X1+X2+X3)/3, (Y1+Y2+Y3)/3)
Then the center of mass of the polygon is divided into a number of triangles, the triangle area is the weight, the center of gravity of each triangle weighted average.
Attention:
Pair<double,double> will be mle.
Fabs will lose precision? (This I do not know), so in the use of vector cross-product to find the triangular area is the best way to directly let the area is positive. Otherwise the fabs will be WA ...
Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <cmath>5 using namespacestd;6typedefLong LongLL;7 #defineEPS 1e-88 #defineX First9 #defineY SecondTen Onetypedefstruct_point{//with Pair<double,double> will mle!! A DoubleFirst ; - Doublesecond; - }point; the - typedef point VECTOR; - Double operator*(VECTOR x,vector y) { - DoubleRes; +res = x.x*y.y-x.y*y.x; - returnRes; + } AVECTORoperator-(point X,point y) { at VECTOR Res; -Res. X = x.x-y.x; -Res. Y = x.y-y.y; - returnRes; - } - in Const intMax_n =1000001; - intT,n; to Point pt[max_n]; + -Point Getcenter [Point points[],intN) { the Point Res; * Doublearea,areamulx,areamuly; $Area =0.0; Areamulx =0.0; Areamuly =0.0;Panax Notoginseng for(intI=1; i<n-1; i++){ -VECTOR V1 = points[i]-points[0]; theVECTOR v2 = points[i+1]-points[i];//if Fabs will lose precision after + DoubleTarea = V1 * V2/2.0; AArea + =Tarea; theAreamulx + = (points[0]. X+points[i]. x+points[i+1]. X) *Tarea; +Areamuly + = (points[0]. Y+points[i]. y+points[i+1]. Y) *Tarea; - } $Res. X = Areamulx/(3.0*Area ); $Res. Y = Areamuly/(3.0*Area ); - returnRes; - } the - intMain () {Wuyiscanf"%d",&T); the while(t-- ){ -scanf"%d",&N); Wu for(intI=0; i<n;i++){ -scanf"%LF%LF", &pt[i]. x,&Pt[i]. Y); About } $Point ans =Getcenter (pt,n); -printf"%.2LF%.2lf\n", ans. X+eps,ans. y+EPS); - } - return 0; A}
[POJ 1385] Lifting the Stone (computational geometry)