C# 二维与三维计算多边形面积的方法

x33g5p2x  于2022-06-08 转载在 其他  
字(0.6k)|赞(0)|评价(0)|浏览(366)

1、二维情况下,计算多边形面积

internal static float GetArea(List<Vector2> points)
{
    float area2 = 0;
    for (int numPoint = 0; numPoint < points.Count - 1; numPoint++)
    {
        MyPoint point = points[numPoint];
        MyPoint nextPoint = points[numPoint + 1];
        area2 += point.x * nextPoint.y - point.y * nextPoint.x;
    }
    return area2 / 2f;
}

2、三维情况下,计算多边形面积

internal static float GetArea3D(List<Vector3> points, Vector3 normal)
 {
    Vector3 vector = Vector3.zero;
    for (int numPoint = 0; numPoint < points.Count - 1; numPoint++)
    {
        var point = points[numPoint];
        var nextPoint = points[numPoint + 1];
        vector += Vector3.Cross(point, nextPoint);
    }
    return (1f / 2f) * Math.Abs(Vector3.Dot(vector, normal));
 }

相关文章