二维多边形面积计算,代码参考如下:
public static float GetPolyArea2D(Vector2[] points)
{
if (points == null) return 0;
int len = points.Length;
if (len < 3) return 0;
float areaTwice = 0;
for (int i = 0; i < len; i++)
{
areaTwice = areaTwice + (points[i].x * points[(i + 1) % len].y - points[(i + 1) % len].x * points[i].y);
}
return Mathf.Abs(areaTwice * 0.5f);
}