HDU 1115 Lifting the Stone(求多边形重心)
大意:给你个n,有n个点,然后给你n个点的坐标,求这n个点形成的多边形的重心的坐标。
1 struct point 2 { 3 double x, y; 4 } P[1000010]; 5 6 struct line 7 { 8 point a, b; 9 } ; 10 11 double xmult(point p1, point p2, point p) 12 { 13 return (p1.x-p.x)*(p2.y-p.y) - (p2.x-p.x)*(p1.y-p.y); 14 } 15 16 point intersection(line u,line v){ 17 point ret=u.a; 18 double t=((u.a.x-v.a.x)*(v.a.y-v.b.y)-(u.a.y-v.a.y)*(v.a.x-v.b.x)) 19 /((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x)); 20 ret.x+=(u.b.x-u.a.x)*t; 21 ret.y+=(u.b.y-u.a.y)*t; 22 return ret; 23 } 24 25 point barycenter(point a,point b,point c){ 26 line u,v; 27 u.a.x=(a.x+b.x)/2; 28 u.a.y=(a.y+b.y)/2; 29 u.b=c; 30 v.a.x=(a.x+c.x)/2; 31 v.a.y=(a.y+c.y)/2; 32 v.b=b; 33 return intersection(u,v); 34 } 35 36 ///多边形重心 37 point Barycenter(int n,point* p){ 38 point ret,t; 39 double t1=0,t2; 40 int i; 41 ret.x=ret.y=0; 42 for (i=1;i<n-1;i++) 43 if (fabs(t2=xmult(p[0],p[i],p[i+1]))>eps){ 44 t=barycenter(p[0],p[i],p[i+1]); 45 ret.x+=t.x*t2; 46 ret.y+=t.y*t2; 47 t1+=t2; 48 } 49 if (fabs(t1)>eps) 50 ret.x/=t1,ret.y/=t1; 51 return ret; 52 } 53 54 int T; 55 int n; 56 57 void Solve() 58 { 59 scanf("%d", &T); 60 while(T--) 61 { 62 scanf("%d", &n); 63 for(int i = 0; i < n; ++i) 64 { 65 scanf("%lf%lf", &P[i].x, &P[i].y); 66 } 67 point t = Barycenter(n, &P[0]); 68 printf("%.2lf %.2lf\n", t.x+eps, t.y+eps); 69 } 70 }