Anonymous user
Shoelace formula for polygonal area: Difference between revisions
Shoelace formula for polygonal area (view source)
Revision as of 08:13, 18 September 2021
, 2 years ago→Python: Alternate
m (→Python: Explicit: Whoops) |
|||
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}}==
|