标签:des style blog http color io os java ar
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5440 Accepted Submission(s): 2278
该题目中的点顺序是按逆时针顺序给的。
求多边形重心分两种情况:
①质量集中在顶点上。n个顶点坐标为(xi,yi),质量为mi,则重心
X = ∑( xi×mi ) / ∑mi Y = ∑( yi×mi ) / ∑mi
特殊地,若每个点的质量相同,则 X = ∑xi / n Y = ∑yi / n
②质量分布均匀。这个题就是这一类型,算法和上面的不同。
特殊地,质量均匀的三角形重心:
X = ( x0 + x1 + x2 ) / 3 Y = ( y0 + y1 + y2 ) / 3
题解参考自:http://blog.csdn.net/lttree/article/details/24720007
所以,这道题解法:
1.以一个点为顶点,做多个三角形,然后求每个三角形的重心与质量。
2.然后用每个三角形的重心为顶点再构成一个多边形。
3.这个多边形就属于上述的第一种情况了,质量在顶点上的多边形面积。
4.套公式就OK拉~
PS:注意几点:
由于三角形质量与面积成正比(WHY?因为质量分布均匀~。~),所以质量可用面积来代替。
再者用叉积求三角形面积要算正负情况(正负号要保留),
此举是为了防止多边形为凹多边形的情况,三角形面积会在多边形外。
OK~翠花,上模板~,此模板也会更新在我整理的 计算几何模板 中哟~~~
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> #define x first #define y second using namespace std; typedef long long LL; typedef pair<double,double> Point; const int N = 10010; int n; vector<Point>P; Point cal() { Point p ,s ; double tp , area = 0 , tpx = 0 ,tpy = 0; p.x = P[0].x ,p.y = P[0].y; for( int i = 1 ; i <= n ;++i ) { if(i == n ) s.x = P[0].x , s.y = P[0].y; else s.x = P[i].x , s.y = P[i].y; tp = ( p.x * s.y - p.y *s.x ); area += tp / 2.0 ; tpx += ( p.x + s.x )* tp ; tpy += ( p.y + s.y )* tp ; p.x =s.x ; p.y = s.y; } s.x = tpx / (6*area); s.y = tpy / (6*area); return s; } void run() { double xx,yy; P.clear(); scanf("%d",&n); for(int i = 0 ; i< n ;++i){ scanf("%lf%lf",&xx,&yy); P.push_back(Point(xx,yy)); } Point res = cal(); printf("%.2lf %.2lf\n",res.x,res.y); } int main() { #ifdef LOCAL // cout<<"1"<<endl; freopen("in.txt","r",stdin); #endif // LOCAL int cas =1 ,_; cin>>_; while(_--) { run(); } return 0; }
标签:des style blog http color io os java ar
原文地址:http://www.cnblogs.com/YRETSIM/p/3980380.html