C# 编程实现非自相交多边形质心
2024-10-11 13:47:38
计算公式公式: http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon
多边形的质心:
一个非自相交的n个顶点的多边形(x0,y0), (x1,y1), ..., (xn−1,yn−1) 的质心 (Cx, Cy):
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 ( 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; }
}
最新文章
- 我们为什么使用Node
- 浏览器禁用Cookie,基于Cookie的会话跟踪机制失效的解决的方法
- jQuery mobile 核心功能
- HDU 4627 There are many unsolvable problem in the world.It could be about one or about zero.But this time it is about bigger number.
- VS2010与SVN
- win10 uwp 九幽图床
- Python基础__字典、集合、运算符
- nginx 配置说明
- ETCD集群安装实验
- C++Primer第五版——习题答案详解(二)
- 建立SQL链接服务器
- python - 类的内置 attr 方法
- Golang简单日志类
- Ansible Playbook handlers 语句
- SQLServer2008开启远程连接
- Excel作为数据源TesTNG做数据驱动完整代码
- Java 8 Date-Time API概览
- Thuwc2018 游记
- thinkphp对数据库的增删改查(查询构造器)
- js加强小结
热门文章
- Linux下的SVN服务器搭建
- C语言函数的声明以及函数原型
- 解决VS2015启动时Package manager console崩溃的问题 - Windows PowerShell updated your execution policy successfully, but the setting is overridden by a policy defined at a more specific scope
- lua 学习 (一 )Mac搭建lua环境和ide
- Ubuntu 反复进入登录框问题
- Innodb 表空间传输迁移数据
- LabVIEW如何调用C#Winform
- ubuntu16.04下配置JDK 1.8+安装Java EE,并实现最大子数组算法
- Shell脚本检测Tomcat是否正在运行
- 【前端】Ember.js学习笔记