C# 编程实现非自相交多边形质心
计算公式公式: http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon
多边形的质心:
一个非自相交的n个顶点的多边形(x0,y0), (x1,y1), ..., (xn−1,yn−1) 的质心 (Cx, Cy):
A是多边形的有向面积:
.
注意,如果点按顺时针方向进行编号,按上述方法计算,会出现一个无法预知的结果;但在这种情况下质心坐标也会是正确的。
In these formulas, the vertices are assumed to be numbered in order of their occurrence along the polygon's perimeter. Furthermore, the vertex ( xn, yn ) is assumed to be the same as ( x0, y0 ), 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# 编程实现非自相交多边形质心的更多相关文章
- 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 ...
- [ECNU 1624] 求交集多边形面积
求交集多边形面积 Time Limit:1000MS Memory Limit:30000KB Total Submit:98 Accepted:42 Description 在平面上有两给定的凸多边 ...
- 任意多边形切割/裁剪(附C#代码实现)
本实现主要参考了发表于2003年<软件学报>的<一个有效的多边形裁剪算法>(刘勇奎,高云,黄有群)这篇论文,所使用的理论与算法大都基于本文,对论文中部分阐述进行了详细解释,并提 ...
- OpenGL研究3.0 多边形区域填充
OpenGL研究3.0 多边形区域填充 DionysosLai(906391500@qq.com)2014-06-22 所谓多边形区域填充.就是将多边形内部区域,所有已相同色块填充.注意:这里讨论的多 ...
- 少儿编程|Scratch编程教程系列合集,总有一款适合你
如果觉得资源不错,友情转发,贵在分享!!! 少儿编程Scratch: 少儿编程Scratch第一讲:Scratch完美的初体验少儿编程Scratch第二讲:奇妙的接球小游戏少儿编程Scratch第三讲 ...
- Image Processing and Analysis_8_Edge Detection:Edge and line oriented contour detection State of the art ——2011
此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...
- PVS BSP
作者:韦易笑链接:https://www.zhihu.com/question/38060533/answer/84432973来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- vue组件:canvas实现图片涂鸦功能
方案背景 需求 需要对图片进行标注,导出图片. 需要标注N多图片最后同时保存. 需要根据多边形区域数据(区域.颜色.名称)标注. 对应方案 用canvas实现涂鸦.圆形.矩形的绘制,最终生成图片bas ...
- GPS围栏两个多边形相交问题的奇葩解法
前言 GPS测量仪测量的产地面积,然后提交到系统中,系统需要校验这块产地和其他产地是否有重叠,重叠超过10%就要提出警告这块产地已经被XXX登记入库了.GPS测量仪测量出来的数据是连续的经纬度坐标数据 ...
随机推荐
- js 简体中文拼音对应表
https://github.com/silaLi/pinyin js 拼音对象,包涵大部分文字
- eclipse 安装activity插件
公司做流程需要用到流程插件,之前用了bpm4 activity是基于bpm4延伸的,这里先介绍下activity 插件是如何安装的 官网资料 *Name:*Activiti BPMN 2.0 desi ...
- 【转】centos关机与重启命令详解
连接:http://blog.csdn.net/jiangzhengdong/article/details/8036594 Linux centos关机与重启命令详解与实战 Linux centos ...
- leetcode 上的Counting Bits 总结
最近准备刷 leetcode 做到了一个关于位运算的题记下方法 int cunt = 0; while(temp) { temp = temp&(temp - 1); //把二进制最左边那 ...
- Delphi XE5-XE8 以上 如何发布文件到工程中
首发在 ① FireMonkey[DELPHI XE5] 165232328 欢迎使用 FMX 开发手机程序的高手来访. (* *********************************** ...
- CheckBox 半选中状态
<input type='checkbox' />可以半选中,这个特性,很多浏览器都支持,包括Firefox,Chrome和IE 用 input.indeterminate 这个属性来获取 ...
- Hdu 4681 2013 Multi-University Training Contest 8 String
带跨越式的LCS,同样是在朴素的LCS上加入一种跨越一段的转移,这样我们要预处理出跨越一段给定串的转移函数. 这个题同样可以正反两边LCS做 呆马: #include <iostream> ...
- percona server 二进制安装下编译tpcc-mysql的坑
出于习惯,percona server的部署都是通过二进制包自动化安装,结果遇到一个硕大无比的坑,编译TPCC-MySQL时出现警告 10:49:36 root@DB-Master:~/tpcc-my ...
- %----format 格式化字符串---- 生成器---- 迭代器
%方式格式化字符串 顺序传参数 o转换8进制x转换十六进制 tp1 = "i am %s" % "alex"tp2 = "i am %s age %d ...
- mysql explain执行计划详解
1).id列数字越大越先执行,如果说数字一样大,那么就从上往下依次执行,id列为null的就表是这是一个结果集,不需要使用它来进行查询. 2).select_type列常见的有: A:simp ...