LA 4426 Blast the Enemy!

来源:互联网 发布:电力系统分析软件bpa 编辑:程序博客网 时间:2024/05/24 04:37

A new computer game has just arrived and as an active and always-in-the-scene player, you should finish it before the next university term starts. At each stage of this game, you have to shoot an enemy robot on its weakness point. The weakness point of a robot is always the ``center of mass" of its 2D shape in the screen. Fortunately, all robot shapes are simple polygons with uniform density and you can write programs to calculate exactly the center of mass for each polygon.

Let's have a more formal definition for center of mass (COM). The center of mass for a square, (also circle, and other symmetric shapes) is its center point. And, if a simple shapeC is partitioned into two simple shapes A and B with areas SA and SB , thenCOM(C) (as a vector) can be calculated by

COM( C) = $\displaystyle {\frac{{S_{A} \times COM(A) + S_{B} \times COM(B)}}{{S_{A} + S_{B}}}}$.

As a more formal definition, for a simple shape A with areaSA :

COM( A) = $\displaystyle {\frac{{\int \int_{A} \vec{a}.ds}}{{S_{A}}}}$

Input

The input contains a number of robot definitions. Each robot definition starts with a line containingn , the number of vertices in robot's polygon (n$ \le$100) . The polygon vertices are specified in the nextn lines (in either clockwise or counter-clock-wise order). Each of these lines contains two space-separated integers showing the coordinates of the corresponding vertex. The absolute value of the coordinates does not exceed 100. The case of n = 0 shows the end of input and should not be processed.

Output

The i -th line of the output should be of the form ``Stage #i:x y " (omit the quotes), where (x, y ) is the center of mass for the i -th robot in the input. The coordinates must be rounded to exactly 6 digits after the decimal point.

Sample Input

4 0 00 11 11 03 0 11 02 28 1 12 12 73 73 00 00 71 70

Sample Output

Stage #1: 0.500000 0.500000 Stage #2: 1.000000 1.000000 Stage #3: 1.500000 3.300000
解析

求多边形重心。

题目已经暗示的很清楚了:把多边形分成n-3个三角形。分别求每个三角形的重心(向量)乘以它的面积。最后把乘了权重的向量加起来,除以多边形总面积。

第一次写计算几何,代码风格可能不是太好。。。

那面积函数求出来的其实是面积的2倍,不过所有的面积都扩大了2倍,除法之后对向量没影响了。

#include<cstdio>#include<cstdlib>#include<cmath>using namespace std;#define LOCALint N;struct Point{double x,y;//Point(double x=0,double y=0):x(x),y(y);}p[110];typedef Point Vector;Vector operator + (Vector a,Vector b){Vector c; c.x=a.x+b.x; c.y=a.y+b.y; return c;}Vector operator - (Vector a,Vector b){Vector c; c.x=a.x-b.x; c.y=a.y-b.y; return c;}Vector operator * (Vector a,double b){Vector c; c.x=a.x*b; c.y=a.y*b; return c;}Vector operator / (Vector a,double b){Vector c; c.x=a.x/b; c.y=a.y/b; return c;}double Cross(Vector a,Vector b) {return a.x*b.y-a.y*b.x;}double Area(Vector a,Vector b,Vector c) {return Cross(b-a,c-a);}//事实上是面积的2倍double S[110];void work(){for(int i=1;i<=N;i++){scanf("%lf%lf",&p[i].x,&p[i].y);}S[0]=0;Vector mass; mass.x=0; mass.y=0;for(int i=2;i<N;i++){S[i]=Area(p[1],p[i],p[i+1]); S[0]+=S[i];mass=mass+((p[1]+p[i]+p[i+1])/3.0)*S[i];}//printf("%.6lf %.6lf\n",mass.x,mass.y);mass=mass/S[0];printf("%.6lf %.6lf\n",mass.x,mass.y);}int main(){#ifdef LOCALfreopen("D.in","r",stdin);#endifint T=1;while(scanf("%d",&N)==1){if(N==0) break;printf("Stage #%d: ",T++);work();}#ifdef LOCALwhile(1);#endifreturn 0;}


0 0
原创粉丝点击