Skip to content
DeepForgeby svx
All problems
EasyCalculusca-142

Green's Theorem Area

Problem

Green's theorem gives the area enclosed by a simple closed curve as A = 0.5 * |closed integral of (x dy - y dx)|.

For a polygon given by its vertices in order, this becomes the shoelace formula 0.5 * |sum of (x_i * y_{i+1} - x_{i+1} * y_i)|. Return 0.0 for fewer than 3 vertices.

Hint

The edge from the last vertex back to the first closes the loop.

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]]]
expected: 0
case 4
in: [[[0,0],[4,0],[4,3]]]
expected: 6
case 5
in: [[[1,1],[3,1],[3,2],[1,2]]]
expected: 2
Starter codePython
def polygon_area(vertices):
    # vertices is a list of [x, y] pairs in order
    # Your code here
    pass

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