All problems
Contour Area (Shoelace)
Problem
Compute the area of a polygon with the shoelace formula.
Sum the cross products x_i * y_{i+1} - x_{i+1} * y_i around the closed contour, take the absolute value and divide by 2. Round to 4 decimal places.
Hint
Vertex order does not matter because the result is made absolute.
Test cases
case 1
in: [[[0,0],[1,0],[1,1],[0,1]]]
expected: 1
case 2
in: [[[0,0],[2,0],[0,2]]]
expected: 2
case 3
in: [[[0,0],[1,1],[2,2]]]
expected: 0
case 4
in: [[[0,0],[0,1],[1,1],[1,0]]]
expected: 1
Starter codePython
def contour_area(points):
# Your code here
pass4 test cases · run your code against them in the editor