Shoelace formula for polygonal area: Difference between revisions

m
m (→‎{{header|Haskell}}: Some tidying and reduction.)
Line 1,066:
<lang python>'''Polygonal area by shoelace formula'''
 
from itertools import (cycle, islice)
from functools import (reduce)
from operator import sub
 
 
Line 1,078 ⟶ 1,079:
l, r = a
(x, y), (dx, dy) = tpl
return (l + x * dy, r + y * dx)
 
return abs(nl, nr) = sub(*reduce(
go,
zip(xys, tail(cycle(xys))),
return xs[1:] xys,
islice(cycle(xys), 1)
return xs),
(0, 0)
))) / 2
return abs(nl - nr) / 2
 
 
Line 1,096 ⟶ 1,099:
print(__doc__ + ':')
print(repr(ps) + ' -> ' + str(shoelaceArea(ps)))
 
 
# GENERIC -------------------------------------------------
 
# tail :: [a] -> [a]
# tail :: Gen [a] -> [a]
def tail(xs):
'''The elements following the head of a
(non-empty) list or generator stream.
'''
if isinstance(xs, list):
return xs[1:]
else:
list(islice(xs, 1)) # First item dropped.
return xs
 
 
9,655

edits