The centre of polygon tzc

The center of gravity (also known as "center of mass" or "centroid" can be calculated with the following formula:

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
X coordinate of the center
Y coordinate of the center
and where A is the polygon's signed area,
Area formula

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenLayers is a popular open-source JavaScript library for displaying maps on the web. It provides various features, including the ability to create and manipulate polygons. To create a polygon using OpenLayers, you can use the `ol.geom.Polygon` class. Here's an example of how you can create a simple polygon: ```javascript // Create an array of coordinates for the polygon vertices var coordinates = [ [0, 0], [10, 0], [10, 10], [0, 10], [0, 0] ]; // Create a polygon geometry using the coordinates array var polygon = new ol.geom.Polygon([coordinates]); // Create a feature with the polygon geometry var feature = new ol.Feature(polygon); // Create a vector layer and add the feature to it var vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector({ features: [feature] }) }); // Create a map and add the vector layer to it var map = new ol.Map({ target: 'map', // Specify the target HTML element layers: [vectorLayer], view: new ol.View({ center: ol.proj.fromLonLat([0, 0]), // Set the initial center of the map zoom: 2 // Set the initial zoom level }) }); ``` In this example, we create a polygon geometry using an array of coordinates representing the vertices of the polygon. We then create a feature with the polygon geometry and add it to a vector layer. Finally, we create a map and add the vector layer to display the polygon. Remember to include the OpenLayers library in your HTML file by adding the appropriate script tag: ```html <script src="https://cdn.jsdelivr.net/npm/ol@6.5.0/dist/ol.js"></script> ``` You can customize the polygon styling and add more complex functionalities based on your specific requirements.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值