Shoelace formula for polygonal area: Difference between revisions

Line 1,418:
<pre>Polygonal area by shoelace formula:
[(3, 4), (5, 11), (12, 8), (9, 5), (5, 6)] -> 30.0</pre>
 
 
===Python: Alternate===
This adopts the ''indexing'' used in the numpy example above, but does not require the numpy library.
<lang python>>>> def area_by_shoelace2(x, y):
return abs(sum(x[i-1]*y[i]-x[i]*y[i-1] for i in range(len(x)))) / 2.
 
>>> points = [(3,4), (5,11), (12,8), (9,5), (5,6)]
>>> x, y = zip(*points)
>>> area_by_shoelace2(x, y)
30.0
>>> </lang>
 
=={{header|Racket}}==
Anonymous user