Shoelace formula for polygonal area: Difference between revisions
Content added Content deleted
mNo edit summary |
m (header) |
||
Line 7: | Line 7: | ||
=={{Python}}== |
=={{header|Python}}== |
||
<lang python>>>> def area_by_shoelace(x, y): |
<lang python>>>> def area_by_shoelace(x, y): |
||
"Assumes x,y points go around the polygon in one direction" |
"Assumes x,y points go around the polygon in one direction" |
Revision as of 17:53, 9 August 2017
Shoelace formula for polygonal area is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Write a function/method/routine to use the the Shoelace formula to calculate the area of the polygon described by the ordered points:
(3,4), (5,11), (12,8), (9,5), and (5,6)
Show the answer here, on this page.
Python
<lang python>>>> def area_by_shoelace(x, y):
"Assumes x,y points go around the polygon in one direction" return abs( sum(i * j for i, j in zip(x, y[1:] + y[:1])) -sum(i * j for i, j in zip(x[1:] + x[:1], y ))) / 2
>>> points = [(3,4), (5,11), (12,8), (9,5), (5,6)] >>> x, y = zip(*points) >>> area_by_shoelace(x, y) 30.0 >>> </lang>