HDU1115 几何+多边形重心
三角形重心:(x1+x2+x3)/3,(y1+y2+y3)/3 这是特殊情况,各点的质量相等
多边形 x=( sigma )( xi*mi )/sumArea
y=( sigma )( yi*mi )/sumArea
先把多边形变成多个三角形,分别求出mi(有点像面缩点),然后得到新的点的坐标。。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #include<math.h> 5 const int maxn = 1000005; 6 struct node{ 7 double x,y; 8 }a[ maxn ]; 9 struct node2{ 10 double x,y,s; 11 }b[ maxn ]; 12 double crossProd(node A, node B, node C) { 13 return (B.x-A.x)*(C.y-A.y) - (B.y-A.y)*(C.x-A.x); 14 } 15 int main(){ 16 int T; 17 scanf("%d",&T); 18 while( T-- ){ 19 int n; 20 scanf("%d",&n); 21 for( int i=0;i<n;i++ ){ 22 scanf("%lf%lf",&a[i].x,&a[i].y); 23 } 24 double sumArea=0; 25 double ans_x,ans_y; 26 int cnt=0; 27 ans_x=ans_y=0; 28 for( int i=2;i<n;i++ ){ 29 double tmp=crossProd(a[0], a[i-1], a[i]); 30 b[ cnt ].x=(a[i].x+a[i-1].x+a[0].x); 31 b[ cnt ].y=(a[i].y+a[i-1].y+a[0].y); 32 b[ cnt ].s=tmp; 33 cnt++; 34 //printf("tmp:%lf\n",tmp); 35 sumArea+=tmp; 36 } 37 for( int i=0;i<cnt;i++ ){ 38 ans_x+= b[ i ].x*b[ i ].s; 39 ans_y+= b[ i ].y*b[ i ].s; 40 } 41 ans_x/=(sumArea*3.0); 42 ans_y/=(sumArea*3.0); 43 printf("%.2lf %.2lf\n",ans_x,ans_y); 44 } 45 return 0; 46 }
keep moving...