Skip to content
DeepForgeby svx
All problems
EasyComputer Visioncv-092

Polygon Centroid

Problem

Compute the centroid of a polygon with the shoelace formula.

Return [round(cx, 4), round(cy, 4)]; for a degenerate zero-area polygon return the mean of the vertices, and return [] when there are no points.

Hint

The area is half the summed cross products; the centroid divides the weighted sums by 3 * area2.

Test cases

case 1
in: [[[0,0],[4,0],[0,4]]]
expected: [1.3333,1.3333]
case 2
in: [[[0,0],[2,0],[2,2],[0,2]]]
expected: [1,1]
case 3
in: [[[0,0],[1,1],[2,2]]]
expected: [1,1]
case 4
in: [[]]
expected: []
Starter codePython
def polygon_centroid(points):
    # Your code here
    pass

4 test cases · run your code against them in the editor