I'm struggling to understand following code snippet:
@metric
def area(prev, nxt):
"""Area of a polygon with points in clockwise order."""
return area_piece(prev, nxt) / 2
def centroid(points):
a = area(points)
return [center_x_helper(points)/a, center_y_helper(points)/a]
@metric
def center_x_helper(prev, nxt):
return (prev[0] + nxt[0]) * area_piece(prev, nxt) / 6
@metric
def center_y_helper(prev, nxt):
return (prev[1] + nxt[1]) * area_piece(prev, nxt) / 6
def area_piece(prev, nxt):
return nxt[0] * prev[1] - prev[0] * nxt[1]
The full code is located here.
I assume that points is some kind of 2D array/list, where each item contains a pair of X- and Y-coordinates. Somehow though, points is passed to area(prev, nxt) to perform the calculations on each pair of coordinates.
When I try one of following
centroid([[150, 200], [0, 300], [0, 400], [3, 1]])
centroid([150, 200, 0, 300, 0, 400, 3, 1])
I get this error:
TypeError: area() missing 1 required positional argument: 'nxt'
Am I missing something here or is this code simply not runnable this way?
@metricis defined by the functiondef metric(function)and is executed each time before the "annotated" (decorated?) function itself is executed? and the annotated function then receives the return-value ofmetric(function)as parameter(s)?