HDU 1115 Lifting the Stone 重心问题
http://acm.hdu.edu.cn/showproblem.php?pid=1115
题意:
给你多边形(N)的N个顶点 , 求这N个顶点构造出了N边形的重心。
坑爹:
不能直接以三角形的推算出 x = (x1 + x2 + ... + xn)/n , y = (y1 + y2 + ... + yn) / n 。
解法:
切分成N-2个三角形,然后分别算出每个三角形的重心和面积,面积通过叉积算出,N边形的重心坐标是
x = ( Σ (Ai * pi.x))/ S .......Σ的i是从1到N-2 S是多边形面积 pi.x是第i块三角形的重心,同理可得y
View Code
1 #include<iostream> 2 using namespace std; 3 4 const int maxn = 1000000 + 10; 5 6 struct Point 7 { 8 int x; 9 int y; 10 }; 11 Point point[maxn]; 12 13 double multi(Point p0,Point p1,Point p2) 14 { 15 return (p1.x - p0.x)*(p2.y - p0.y) - (p1.y - p0.y)*(p2.x - p0.x); 16 } 17 18 int main() 19 { 20 int T; 21 cin>>T; 22 while(T--) 23 { 24 int N; 25 cin>>N; 26 int i; 27 for(i=0; i<N; i++) 28 { 29 cin>>point[i].x>>point[i].y; 30 } 31 double Nsum = 0; 32 double sum = 0; 33 double resultX = 0; 34 double resultY = 0; 35 36 for(i=1; i<N-1; i++) 37 { 38 Nsum = Nsum + multi(point[0],point[i],point[i+1])/2; 39 sum = multi(point[0],point[i],point[i+1])/2; 40 resultX += (point[0].x + point[i].x + point[i+1].x)*sum/3; 41 resultY += (point[0].y + point[i].y + point[i+1].y)*sum/3; 42 } 43 44 resultX /= Nsum; 45 resultY /= Nsum; 46 47 printf("%.2lf %.2lf\n",resultX,resultY); 48 } 49 50 return 0; 51 }

浙公网安备 33010602011771号