UVALive 4426 Blast the Enemy! 计算几何求重心

本文介绍了一款新推出的计算机游戏的玩法策略,玩家需通过编程计算并射击机器人弱点——其二维形状的质心点。详细解释了质心点的定义与计算方法,包括如何将复杂形状分解为简单形状进行计算,并提供了输入输出示例。
D - Blast the Enemy!
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF

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 shape C is partitioned into two simple shapes A and B with areas SA and SB , then COM(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 area SA :

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 containing n , the number of vertices in robot's polygon (n$ \le$100) . The polygon vertices are specified in the next n 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 0
0 1
1 1
1 0
3 
0 1
1 0
2 2
8 
1 1
2 1
2 7
3 7
3 0
0 0
0 7
1 7
0

Sample Output

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

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 1001
const int inf=0x7fffffff;   //无限大
int main()
{
    int N;
    double x[maxn],y[maxn],a[maxn],ax[maxn],ay[maxn],xg=0,yg=0,a1=0,b1=0,c=0;
    int t=0;
    while(cin>>N){
    xg=0,yg=0,a1=0,b1=0,c=0;
    t++;
    int i,n;
    if(N==0)
        break;
    for(i=0;i<N;i++)
    {
    scanf("%lf %lf",&x[i],&y[i]);
    }
    for(i=0;i<N-1;i++)
    {
    a[i]=(y[i+1]+y[i])*(x[i]-x[i+1])/2.0;
    ax[i]=(x[i+1]*x[i+1]+x[i+1]*x[i]+x[i]*x[i])*(y[i+1]-y[i])/6.0;
    ay[i]=(y[i+1]*y[i+1]+y[i+1]*y[i]+y[i]*y[i])*(x[i]-x[i+1])/6.0;
    }
    a[N-1]=(y[0]+y[N-1])*(x[N-1]-x[0])/2.0;
    ax[N-1]=(x[0]*x[0]+x[0]*x[N-1]+x[N-1]*x[N-1])*(y[0]-y[N-1])/6.0;
    ay[N-1]=(y[0]*y[0]+y[0]*y[N-1]+y[N-1]*y[N-1])*(x[N-1]-x[0])/6.0;
    for(i=0;i<N;i++)
    {
    a1=a1+ax[i];
    b1=b1+a[i];
    c=c+ay[i];
    }
    xg=a1/b1;
    yg=c/b1;
    printf("Stage #%d: %.6lf %.6lf\n",t,xg,yg);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值