The centre of polygon tzc
2013-05-09 13:50
218 查看
The center of gravity (also known as "center of mass" or "centroid" can be calculated with the following formula:
The centroid of a non-self-intersecting closed polygon defined by n vertices (x0,y0), (x1,y1), ..., (xn−1,yn−1) is the point (Cx, Cy), where
and where A is the polygon's signed area,
becenter实现上述计算
X = SUM[(Xi + Xi+1) * (Xi * Yi+1 - Xi+1 * Yi)] / 6 / A Y = SUM[(Yi + Yi+1) * (Xi * Yi+1 - Xi+1 * Yi)] / 6 / A
The centroid of a non-self-intersecting closed polygon defined by n vertices (x0,y0), (x1,y1), ..., (xn−1,yn−1) is the point (Cx, Cy), where
and where A is the polygon's signed area,
becenter实现上述计算
#include<cstdio> #include<cmath> struct point { double x,y; }pp[1000008]; point becenter(point pnt[],int m) { point p,s; double tp,area=0,tpx=0,tpy=0; int i; p.x=pnt[0].x,p.y=pnt[0].y; for(i=1;i<=m;i++) { s.x=pnt[i%m].x; s.y=pnt[i%m].y; tp=(p.x*s.y-s.x*p.y); area+=tp/2; 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; } int main() { int i,j; double l,k,d,n; int ncase,m; scanf("%d",&ncase); while(ncase--) { scanf("%d",&m); for(i=0;i<m;i++) scanf("%lf%lf",&pp[i].x,&pp[i].y); point c=becenter(pp,m); printf("%.2lf %.2lf\n",c.x,c.y); } return 0; }
相关文章推荐
- The centre of polygon (多边形重心)
- Determining if a point lies on the interior of a polygon
- Determining if a point lies on the interior of a polygon
- hunnu11562:The Triangle Division of the Convex Polygon(第n个卡特兰数取模)
- HNU 13101 The Triangle Division of the Convex Polygon 组合数的因式分解求法
- HOJ 13101 The Triangle Division of the Convex Polygon(数论求卡特兰数(模不为素数))
- The Number of Triangles Formed by Intersecting Diagonals of a Regular Polygon
- The Number of Triangles Formed by Intersecting Diagonals of a Regular Polygon
- HNU 13101 The Triangle Division of the Convex Polygon 卡特兰数第n项%m(m可为非素数
- a fast algorithm to compute the area of a polygon
- HUNAN 11562 The Triangle Division of the Convex Polygon(大卡特兰数)
- The Triangle Division of the Convex Polygon
- Algorithm to find the area of a polygon
- Monitor the Use of Removable Storage Devices
- Maven-010-maven 编译报错:Failure to ... in ... was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced.
- #372 Delete Node in the Middle of Singly Linked List
- Taking Advantage of the Winlogon Notification Package
- The Standard Librarian: Containers of Incomplete Types
- the first time use the header of zlib.h
- Android安装 Location of the Android SDK has not been setup in the preferences 问题