查找多边形区域的质心

时间:2019-01-06 02:24:18

标签: c# geometry

我有以下几点要点:

{X = 3.142888 Y = 101.633827}
{X = 3.142325 Y = 101.633827}
{X = 3.142023 Y = 101.633759}
{X = 3.141899 Y = 101.633652}
{X = 3.141045 Y = 101.633659}
{X = 3.141433 Y = 101.634193}
{X = 3.141291 Y = 101.6343}
{X = 3.141381 Y = 101.634415}
{X = 3.141194 Y = 101.634895}
{X = 3.141618 Y = 101.6349}
{X = 3.142697 Y = 101.634239}

我尝试使用http://csharphelper.com/blog/2014/07/find-the-centroid-of-a-polygon-in-c/

的方法
public PointF FindCentroid()
{
    // Add the first point at the end of the array.
    int num_points = Points.Length;
    PointF[] pts = new PointF[num_points + 1];
    Points.CopyTo(pts, 0);
    pts[num_points] = Points[0];

    // Find the centroid.
    float X = 0;
    float Y = 0;
    float second_factor;
    for (int i = 0; i < num_points; i++)
    {
        second_factor =
            pts[i].X * pts[i + 1].Y -
            pts[i + 1].X * pts[i].Y;
        X += (pts[i].X + pts[i + 1].X) * second_factor;
        Y += (pts[i].Y + pts[i + 1].Y) * second_factor;
    }

    // Divide by 6 times the polygon's area.
    float polygon_area = PolygonArea();
    X /= (6 * polygon_area);
    Y /= (6 * polygon_area);

    // If the values are negative, the polygon is
    // oriented counterclockwise so reverse the signs.
    if (X < 0)
    {
        X = -X;
        Y = -Y;
    }

    return new PointF(X, Y);
}

但是结果很遥远:

{X = 3.11197066 Y = 100.614342}

在这里我想念什么吗?

谢谢

0 个答案:

没有答案