码迷,mamicode.com
首页 > 其他好文 > 详细

多边形重心问题

时间:2015-02-27 13:36:54      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:

<div class="problem-display" style="font-size: 14px; color: rgb(70, 70, 70); font-family: Tahoma, Arial, sans-serif, simsun;"><h2 style="margin: 0px; padding: 0px; font-size: 18px; text-align: center; color: rgb(113, 32, 21); font-family: 微软雅黑, 黑体;">多边形重心问题</h2><div class="problem-ins" style="text-align: center;">时间限制:<span class="editable highlight" id="problem[time_limit]" style="color: rgb(113, 32, 21);">3000</span> ms  |  内存限制:<span class="editable highlight" id="problem[memory_limit]" style="color: rgb(113, 32, 21);">65535</span> KB</div><div class="problem-ins" style="text-align: center;">难度:<span class="editable highlight" style="color: rgb(113, 32, 21);">5</span></div></div><div class="clr" style="clear: both; color: rgb(70, 70, 70); font-family: Tahoma, Arial, sans-serif, simsun; font-size: 12.800000190734863px; line-height: 15.199999809265137px;"></div><dl class="problem-display" style="margin: 0px; padding: 0px; font-size: 14px; color: rgb(70, 70, 70); font-family: Tahoma, Arial, sans-serif, simsun;"><dt style="margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-size: 16px; font-weight: bold;">描述</dt><dd style="margin: 0px; padding: 0px;">在某个多边形上,取n个点,这n个点顺序给出,按照给出顺序将相邻的点用直线连接, (第一个和最后一个连接),所有线段不和其他线段相交,但是可以重合,可得到一个多边形或一条线段或一个多边形和一个线段的连接后的图形; 
如果是一条线段,我们定义面积为0,重心坐标为(0,0).现在求给出的点集组成的图形的面积和重心横纵坐标的和;</dd><div class="clr" style="clear: both;"></div><dl class="others" style="margin: 0px; padding: 0px;"><dt style="margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-size: 16px; font-weight: bold;">输入</dt><dd style="margin: 0px; padding: 0px;">第一行有一个整数0<n<11,表示有n组数据;
每组数据第一行有一个整数m<10000,表示有这个多边形有m个顶点;</dd><dt style="margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-size: 16px; font-weight: bold;">输出</dt><dd style="margin: 0px; padding: 0px;">输出每个多边形的面积、重心横纵坐标的和,小数点后保留三位;</dd><dt style="margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-size: 16px; font-weight: bold;">样例输入</dt><dd style="margin: 0px; padding: 0px;"><pre id="sample_input" style="margin-top: 0px; margin-bottom: 0px; padding: 5px 10px; font-family: Consolas, 'Courier New', 'DejaVu Sans Mono', 'Droid Sans Mono', monospace; background-color: rgb(239, 239, 239); border: 1px solid rgb(204, 204, 204); min-height: 20px; line-height: 1.5em;">3
3
0 1
0 2
0 3
3
1 1
0 0
0 1
4
1 1
0 0
0 0.5
0 1
样例输出
0.000 0.000
0.500 1.000
0.500 1.000
上传者
iphxer


<span style="font-size:32px;">思路:这个题,只是个公式问题,多边形面积和重心坐标问题。我昨天那个重心坐标公式错了。</span><span style="font-size:24px; font-family: Arial, Helvetica, sans-serif;">我就说一下重心坐标问题。把多边形分成若干个三角形,然后是每个三角形的</span><span style="font-size:24px;"><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">这个三角形的area</span><span style="font-family: Arial, Helvetica, sans-serif;">*(x的平均坐标)相加 除以总的area。</span></span>
<span style="color: rgb(51, 51, 51); font-family: arial, 宋体, sans-serif, 'Microsoft YaHei', tahoma; line-height: 23.99147605895996px; background-color: rgb(255, 102, 102);"><strong><span style="font-size:24px;">计算重心的时候,面积部分不应该带fabs。记着。。</span></strong></span>
<span style="font-size:32px;">http://zhidao.baidu.com/question/1575051969932482860#</span>
<span style="font-size:32px;">my code:</span>
<span style="font-size:18px;">#include<iostream>
#include<cmath>
#include<stdio.h>
using namespace std;
struct point
{
	double x;
	double y;
};
int main()
{
	int n,m,i;
	point a[10000];
	cin>>n;
    while(n--)
	{
	  double area=0;
	  double x1=0;
	  double y1=0;
      cin>>m;
	  for(i=0;i<m;i++)
		  cin>>a[i].x>>a[i].y;
	  for(i=0;i<=m-2;i++)
		  area+=a[i].x*a[i+1].y;
	  for(i=1;i<=m-1;i++)
		  area-=a[i].x*a[i-1].y;
	  area=area+a[m-1].x*a[0].y-a[0].x*a[m-1].y;
	  area=area/2;   
	  for(i=1;i<=m-2;i++)
		  x1+=(a[0].x+a[i].x+a[i+1].x)/3*(a[0].x*a[i].y+a[i].x*a[i+1].y+a[i+1].x*a[0].y-a[i].x*a[0].y-a[i+1].x*a[i].y-a[0].x*a[i+1].y)/2;
	  for(i=1;i<=m-2;i++)
		  y1+=(a[0].y+a[i].y+a[i+1].y)/3*(a[0].x*a[i].y+a[i].x*a[i+1].y+a[i+1].x*a[0].y-a[i].x*a[0].y-a[i+1].x*a[i].y-a[0].x*a[i+1].y)/2;
	   x1=x1/area;
	   y1=y1/area;
	   area=fabs(area);
	    if(area<0.0001)
          printf("0.000 0.000\n");
	  else
	  { printf("%.3lf ",area);
	  printf("%.3lf\n",x1+y1);}
	 }
	return 0;
}</span>

法二:pooker2008帮我改的。。哈哈

<span style="font-size:24px;">#include<iostream>
#include<cmath>
#include<stdio.h>
using namespace std;
struct point {
    double x;
    double y;
};
int main() {
    int n, m, i;
    point a[10000];
    cin >> n;
    while (n--) {
        double area = 0;
        double x1 = 0;
        double y1 = 0;
        cin >> m;
        for (i = 0; i < m; i++) {
            cin >> a[i].x >> a[i].y;
        }
        for (i = 1; i <= m - 2; i++) {
            double temp = ((a[i].x - a[0].x) * (a[i + 1].y - a[0].y) 
                         - (a[i].y - a[0].y) * (a[i + 1].x - a[0].x)) / 2.0;
            x1 += (a[0].x + a[i].x + a[i + 1].x) / 3.0 * temp;
            y1 += (a[0].y + a[i].y + a[i + 1].y) / 3.0 * temp;
            area += temp;
        }
        x1 = x1 / area;
        y1 = y1 / area;
        area = fabs(area);
        if (area < 1e-6)
            printf("0.000 0.000\n");
        else {
            printf("%.3f ", area);
            printf("%.3f\n", x1 + y1);
        }
    }
    return 0;
}</span>

多边形重心问题

标签:

原文地址:http://blog.csdn.net/zuguodexiaoguoabc/article/details/43966447

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!