求多边形的质心hdu1115
来源:互联网 发布:我的淘宝是什么意思 编辑:程序博客网 时间:2024/05/21 10:34
给出点。然后求多边形的质心。
要求多边形的质心,就得先要知道三角形的质心要怎么求。
三角形的质心(A+B+C)/3.
可以用向量来证明。
然后我们还得知道质点系的质心公式:在质量均匀分布在质点上。如果质量均匀分布在顶点上,那么这个公式是正确的。
但是我们要求的是平面。所以质量均匀分布在平面上,可以将平面分成N-2个三角形(多边形三角剖分)。每个三角形的质心就可以求出来。
这样平面的质量就等价与这几个质心的质量也等价与三角形的面积 。
又由于分割出去的三角形大小差异。非均匀分布。所以我们需要加权平均值(各数值乘以相应的权数,然后加总求和得到总体值,再除以总的单位数)。
有向面积就是权。
然后结果就是
( (对应三角形质心_X)*对应的子三角面积/总面积 , (对应三角形质心_Y)*对应的子三角面积/总面积 )
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <utility>#include <math.h>#include <string.h>#include <algorithm>using namespace std;typedef pair<double,double> P;const int maxn=1e6+5;const double eps=1e-2;P p[maxn];const P operator-(P& a,P& b){ return make_pair(b.first-a.first,b.second-a.second);}double cross(P a,P b){ //printf("%f,%f %f,%f\n",a.first,a.second,b.first,b.second); return a.first*b.second-a.second*b.first;}P gravity(P c[],int n){ double ansarea=0; P ans; ans.first=0; ans.second=0; for(int i=1;i<n-1;i++) { double area=cross(c[i]-c[0],c[i+1]-c[0])/2; //printf("area=%f\n",area); ans.first+=(c[i].first+c[i+1].first+c[0].first)/3*area;///三角形质心*有向面积 ans.second+=(c[i].second+c[i+1].second+c[0].second)/3*area; //printf("fi=%f se=%f\n",ans.first,ans.second); ansarea+=area; } ans.first=ans.first/ansarea; ans.second=ans.second/ansarea; return ans;}int main(){ //freopen("in.txt","r",stdin); int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%lf%lf",&p[i].first,&p[i].second); //printf("%f %f\n",p[i].first,p[i].second); } P ans=gravity(p,n); if(fabs(ans.first)<=eps) ans.first=0; if(fabs(ans.second)<=eps) ans.second=0; printf("%.2f %.2f\n",ans.first,ans.second); } return 0;}
0 0
- 求多边形的质心hdu1115
- hdu1115 求多边形重心
- HDU1115 Lifting the Stone (求多边形的重心)
- hdu1115(求多边形重心模板)
- Lifting the Stone(hdu1115)多边形的重心
- hdu1115 Lifting the Stone (求多边形重心)
- javascript实现计算多边形的质心
- 求cluster的质心坐标
- HDU1115 --多边形重心求解
- hdu1115(多边形重心算法)
- HDU1115 多边形面积公式
- 求质心
- 求图像质心的C语言实现
- 用高斯定理求多面体的质心
- 用高斯定理求多面体的质心
- hdu1115 Lifting the Stone(几何,求多边形重心模板题)
- hdu1115 Lifting the Stone(几何,求多边形重心模板题)
- HDU1115 Gravity(求重心)
- 《一起学》系列11:Redis入门
- IDT与中断
- !HDU 4173 到点的距离不超过2.5,找最多能被满足的点的个数-简单几何
- ural1960Palindromes and Super Abilities(回文树)
- Android Camera 使用小结
- 求多边形的质心hdu1115
- 本文大部分参考自 http://blog.csdn.net/mayingcai1987/article/details/6200909 ,对原文中的讲解FLAG_NEW_TASK地方加了一些自己的观点
- 封装 字符串
- 欢迎使用CSDN-markdown编辑器
- hd1372 Knight Moves
- LMA与VMA总结
- Hadoop家族包含的子项目及其主要功能
- objective C程序 C与OC的对比
- FutureTask类源代码