计算公式公式: http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

多边形的质心:

一个非自相交的n个顶点的多边形(x0,y0), (x1,y1), ..., (xn−1,yn−1) 的质心 (CxCy):

A是多边形的有向面积:

.

在这些公式中,顶点被假定为沿多边形周长编号。此外,顶点(xn,yn)与(x0,y0)是相同的,意味着 对最后一次循环的i + 1 会回到i = 0.
注意,如果点按顺时针方向进行编号,按上述方法计算,会出现一个无法预知的结果;但在这种情况下质心坐标也会是正确的。

In these formulas, the vertices are assumed to be numbered in order of their occurrence along the polygon's perimeter. Furthermore, the vertex ( xnyn ) is assumed to be the same as ( x0y0 ), meaning i + 1 on the last case must loop around to i = 0. Note that if the points are numbered in clockwise order the areaA, computed as above, will have a negative sign; but the centroid coordinates will be correct even in this case.

对于那些难以理解这些公式∑符号,下面给出C#的实现代码:

class Program
{
static void Main(string[] args)
{
List<Point> vertices = new List<Point>(); vertices.Add(new Point() { X = 1, Y = 1 });
vertices.Add(new Point() { X = 1, Y = 10 });
vertices.Add(new Point() { X = 2, Y = 10 });
vertices.Add(new Point() { X = 2, Y = 2 });
vertices.Add(new Point() { X = 10, Y = 2 });
vertices.Add(new Point() { X = 10, Y = 1 });
vertices.Add(new Point() { X = 1, Y = 1 }); Point centroid = Compute2DPolygonCentroid(vertices);
} static Point Compute2DPolygonCentroid(List<Point> vertices)
{
Point centroid = new Point() { X = 0.0, Y = 0.0 };
double signedArea = 0.0;
double x0 = 0.0; // Current vertex X
double y0 = 0.0; // Current vertex Y
double x1 = 0.0; // Next vertex X
double y1 = 0.0; // Next vertex Y
double a = 0.0; // Partial signed area // For all vertices except last
int i=0;
for (i = 0; i < vertices.Count - 1; ++i)
{
x0 = vertices[i].X;
y0 = vertices[i].Y;
x1 = vertices[i+1].X;
y1 = vertices[i+1].Y;
a = x0*y1 - x1*y0;
signedArea += a;
centroid.X += (x0 + x1)*a;
centroid.Y += (y0 + y1)*a;
} // Do last vertex
x0 = vertices[i].X;
y0 = vertices[i].Y;
x1 = vertices[0].X;
y1 = vertices[0].Y;
a = x0*y1 - x1*y0;
signedArea += a;
centroid.X += (x0 + x1)*a;
centroid.Y += (y0 + y1)*a; signedArea *= 0.5;
centroid.X /= (6*signedArea);
centroid.Y /= (6*signedArea); return centroid;
}
} public class Point
{
public double X { get; set; }
public double Y { get; set; }
}

  

C# 编程实现非自相交多边形质心的更多相关文章

  1. HDU 3685 Rotational Painting(多边形质心+凸包)(2010 Asia Hangzhou Regional Contest)

    Problem Description Josh Lyman is a gifted painter. One of his great works is a glass painting. He c ...

  2. [ECNU 1624] 求交集多边形面积

    求交集多边形面积 Time Limit:1000MS Memory Limit:30000KB Total Submit:98 Accepted:42 Description 在平面上有两给定的凸多边 ...

  3. 任意多边形切割/裁剪(附C#代码实现)

    本实现主要参考了发表于2003年<软件学报>的<一个有效的多边形裁剪算法>(刘勇奎,高云,黄有群)这篇论文,所使用的理论与算法大都基于本文,对论文中部分阐述进行了详细解释,并提 ...

  4. OpenGL研究3.0 多边形区域填充

    OpenGL研究3.0 多边形区域填充 DionysosLai(906391500@qq.com)2014-06-22 所谓多边形区域填充.就是将多边形内部区域,所有已相同色块填充.注意:这里讨论的多 ...

  5. 少儿编程|Scratch编程教程系列合集,总有一款适合你

    如果觉得资源不错,友情转发,贵在分享!!! 少儿编程Scratch: 少儿编程Scratch第一讲:Scratch完美的初体验少儿编程Scratch第二讲:奇妙的接球小游戏少儿编程Scratch第三讲 ...

  6. Image Processing and Analysis_8_Edge Detection:Edge and line oriented contour detection State of the art ——2011

    此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...

  7. PVS BSP

    作者:韦易笑链接:https://www.zhihu.com/question/38060533/answer/84432973来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  8. vue组件:canvas实现图片涂鸦功能

    方案背景 需求 需要对图片进行标注,导出图片. 需要标注N多图片最后同时保存. 需要根据多边形区域数据(区域.颜色.名称)标注. 对应方案 用canvas实现涂鸦.圆形.矩形的绘制,最终生成图片bas ...

  9. GPS围栏两个多边形相交问题的奇葩解法

    前言 GPS测量仪测量的产地面积,然后提交到系统中,系统需要校验这块产地和其他产地是否有重叠,重叠超过10%就要提出警告这块产地已经被XXX登记入库了.GPS测量仪测量出来的数据是连续的经纬度坐标数据 ...

随机推荐

  1. 关于在left join的on子句中限制左边表的取值时出现非期望的结果

    使用的SQL大概是这样的: select * from A left join B on A.id=B.id and A.id>10; --错误的使用 我们期望的结果集应该是 A中的id> ...

  2. Canvas: Out of system resources

    一个手写板的项目 在线程中操作Canvas画用户的笔记, 画不了几笔就卡住不画了, 然后保存到另外的image时 提示“Out of system Resource”错误, 百思不得姐 中间考虑是不是 ...

  3. 二十、Java基础--------IO流之其他对象

    在上面两篇文章中介绍了IO体系并详细介绍了字节流以及字符流,本文主要是讲述操作文件的File.转化流.标准输入与输出.Properties.打印流以及序列流. File File英文为"文件 ...

  4. Android Fragment替换View

    在BaseActivity 中添加替换方法 public void replace(int id, Fragment fragment) { getSupportFragmentManager() . ...

  5. CentOS 6.5 安装HDFS集群(Hadoop-2.7.3)

    安装真实集群,而不是但节点或者伪分布式,以3个节点为例,node1为NameNode和SecondNameNode,node2和node3为DataNode. 1.3台机器的配置必须要一模一样,只需要 ...

  6. kafka0.9.0及0.10.0配置属性

    问题导读1.borker包含哪些属性?2.Producer包含哪些属性?3.Consumer如何配置?borker(0.9.0及0.10.0)配置Kafka日志本身是由多个日志段组成(log segm ...

  7. 数据结构 B-树和B+树的应用:数据搜索和数据库索引

    B-树 1 .B-树定义 B-树是一种平衡的多路查找树,它在文件系统中很有用. 定义:一棵m 阶的B-树,或者为空树,或为满足下列特性的m 叉树:⑴树中每个结点至多有m 棵子树:⑵若根结点不是叶子结点 ...

  8. 突击战UVa11729Commando War

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=117&page= ...

  9. angular.extend()和 angular.copy()的区别

    1.angular.copy angular.copy(source, [destination]);  // source: copy的对象. 可以使任意类型, 包括null和undefined. ...

  10. Volley 实现原理图

    1.启动requestQueue 2. 添加请求 3. 启动cacheDispatcher 4.启动networkDispatcher 5. 数据分发