Anonymous user
Shoelace formula for polygonal area: Difference between revisions
Shoelace formula for polygonal area (view source)
Revision as of 04:21, 28 August 2017
, 6 years ago→{{header|Lua}}: + Lua version
(→{{header|REXX}}: added REXX version 2, used a better way to express multiple quoted literal strings in one statement.) |
m (→{{header|Lua}}: + Lua version) |
||
Line 120:
its area is 30.0
</pre>
=={{header|Lua}}==
Using an accumulator helper inner function
<lang lua>function shoeArea(ps)
local function ssum(acc, p1, p2, ...)
if not p2 or not p1 then
return math.abs(0.5 * acc)
else
return ssum(acc + p1[1]*p2[2]-p1[2]*p2[1], p2, ...)
end
end
return ssum(0, ps[#ps], table.unpack(ps))
end
local p = {{3,4}, {5,11}, {12,8}, {9,5}, {5,6}}
print(shoeArea(p))-- 30 </lang>
=={{header|Perl 6}}==
|