C# 编程实现非自相交多边形质心

时间:2024-01-01 12:32:27

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

多边形的质心:

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

C#  编程实现非自相交多边形质心
C#  编程实现非自相交多边形质心

A是多边形的有向面积:

C#  编程实现非自相交多边形质心.

在这些公式中,顶点被假定为沿多边形周长编号。此外,顶点(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; }
}