Shoelace formula for polygonal area: Difference between revisions

→‎{{header|Haskell}}: Tidied in terms of bimap
(Shoelace formula for polygonal area in BASIC256)
(→‎{{header|Haskell}}: Tidied in terms of bimap)
Line 717:
 
=={{header|Haskell}}==
<lang Haskell>mainimport :: IOData.Bifunctor (bimap)
main = print (shoelace [(3, 4), (5, 11), (12, 8), (9, 5), (5, 6)])
 
----------- SHOELACE FORMULA FOR POLYGONAL AREA ----------
-- The area of a polygon formed by the list of (x, y) coordinates.
 
-- The area of a polygon formed by the list of (x, y) coordinates.
-- the list of (x, y) coordinates.
 
shoelace :: [(Double, Double)] -> Double
shoelace =
let calcSums ((xix, yiy), (nxia, nyib)) (l,= r) =bimap (l + xix * nyi, rb +) nxi(a * yiy +)
in (/ 2) .
. abs
abs . uncurry (-) . foldr calcSums (0, 0) . (<*>) zip (tail . cycle)</lang>
. uncurry (-)
. foldr calcSums (0, 0)
abs . uncurry (-) . foldr calcSums (0, 0) . (<*>) zip (tail . cycle)</lang>
 
--------------------------- TEST -------------------------
main :: IO ()
main =
print $
main = print ( shoelace [(3, 4), (5, 11), (12, 8), (9, 5), (5, 6)])</lang>
{{out}}
<pre>30.0</pre>
9,655

edits