1
\$\begingroup\$

I have a irregular 2D polygon, it is a PackedVector2Array, how do I get its area?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You can use Gauss' shoelace formula:

func calculate_area(mesh_vertices: PackedVector2Array) -> float:
    var result := 0.0
    var num_vertices := mesh_vertices.size()

    for q in range(num_vertices):
        var p = (q - 1 + num_vertices) % num_vertices
        result += mesh_vertices[q].cross(mesh_vertices[p])
    
    return abs(result) * 0.5
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .