Poj1385(求多边形的重心)利用叉积求面积

题目链接:http://poj.org/problem?id=1385

 

题意:求出给定多边形的重心坐标

 

#include<stdio.h> #define M 1000008 typedef struct{ double x,y; }Point; Point p[M],o; double Multi(Point p,Point q,Point o) { return (p.x-o.x)*(q.y-o.y)-(q.x-o.x)*(p.y-o.y); } Point Center(Point p[],int n) { Point ret; double dx,dy,are,tare; int i; dx=dy=0;are=0; p[n]=p[0]; for(i=0;i<n;i++){ tare=Multi(p[i],p[i+1],o); are+=tare; dx+=(p[i].x+p[i+1].x)*tare; dy+=(p[i].y+p[i+1].y)*tare; } ret.x=dx/(3*are);ret.y=dy/(3*are); return ret; } int main() { int n,t,i; scanf("%d",&t); while(t--){ scanf("%d",&n); o.x=o.y=0.0; for(i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y); p[0]=Center(p,n); printf("%.2f %.2f/n",p[0].x,p[0].y); } return 0; } 

你可能感兴趣的:(struct,ini)