标签:des style blog http color os io java strong
2 4 5 0 0 5 -5 0 0 -5 4 1 1 11 1 11 11 1 11
0.00 0.00 6.00 6.00
xg = (x1+x2+x3) / 3 ; yg = (y1+y2+y3) / 3 ;
定理2: 将多边形划分成n个小三角形区域, 每个小区域面积为σi ,重心为Gi ( . xi , . yi ) ,利用求平面薄板重心公式把积分变
成累加和:


#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
struct Point
{
double x,y;
Point(double x = 0, double y = 0) : x(x),y(y) {};
void read()
{
scanf("%lf %lf",&x,&y);
}
Point operator - (const Point &a) const //重载减号;
{
return Point(a.x - x, a.y - y);
}
};
double cross(Point A, Point B) //向量的叉积;
{
return A.x * B.y - A.y * B.x;
}
double area(Point a, Point b, Point c) //三角型面积公式1/2 * cross(Point A, Point B)
{
Point A,B;
A = b - a;
B = c - a;
return cross(A,B);//不需要再除以2,因为在最后的结果中抵消了;
}
int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
Point p0,p1,p2;
double sum_x,sum_y,sum_area;
p0.read();
p1.read();
sum_x = sum_y = sum_area = 0.0;
for(int i = 2; i < n; i++)
{
p2.read();
double tp = area(p0,p1,p2);
sum_x += (p0.x + p1.x + p2.x) * tp;
sum_y += (p0.y + p1.y + p2.y) * tp;
sum_area += tp;
p1 = p2;
}
printf("%.2lf %.2lf\n",sum_x / sum_area / 3.0, sum_y / sum_area / 3.0);
}
return 0;
}
标签:des style blog http color os io java strong
原文地址:http://blog.csdn.net/zsgg_acm/article/details/38948083