求重心
1.精确求重心(点的顺序可顺时针可逆时针,但必须有序)
double Area(Point p0,Point p1,Point p2) {
double area = p0.x * p1.y + p1.x * p2.y + p2.x * p0.y;
area = area - p1.x * p0.y - p2.x * p1.y - p0.x * p2.y;
return area / 2;
}//求三点围成的三角形面积
Point Center(Point op[],int n) {
double sumx = 0,sumy = 0,sumarea = 0,area;
Point p0 = op[0],p1 = op[1],p2;
for(int i = 2; i < n; i ++) {
p2 = op[i];
area = Area(p0,p1,p2);
sumarea = sumarea + area;
sumx = sumx + (p0.x + p1.x + p2.x) * area;
sumy = sumy + (p0.y + p1.y + p2.y) * area;
p1 = p2;
}
return Point(sumx / sumarea / 3,sumy / sumarea / 3);
}
2.累加和求重心(点可以无序,但是精度不够)
Point Center(Point op[],int n) {
double sumx = 0,sumy = 0;
for(int i = 0; i < n; i ++) {
sumx = sumx + op[i].x;
sumy = sumy + op[i].y;
}
return Point(sumx / n,sumy / n);
}