Shoelace formula for polygonal area: Difference between revisions

Content added Content deleted
m (→‎{{header|Lua}}: + Lua version)
m (→‎{{header|Lua}}: + for loop version)
Line 122: Line 122:


=={{header|Lua}}==
=={{header|Lua}}==
<lang lua>function shoeArea(ps)
local function det2(i,j)
return ps[i][1]*ps[j][2]-ps[j][1]*ps[i][2]
end
local sum = #ps>1 and det2(#ps,1) or 0
for i=1,#ps-1 do sum = sum + det2(i,i+1)end
return math.abs(0.5 * sum)
end</lang>
Using an accumulator helper inner function
Using an accumulator helper inner function
<lang lua>function shoeArea(ps)
<lang lua>function shoeArea(ps)
Line 136: Line 144:
local p = {{3,4}, {5,11}, {12,8}, {9,5}, {5,6}}
local p = {{3,4}, {5,11}, {12,8}, {9,5}, {5,6}}
print(shoeArea(p))-- 30 </lang>
print(shoeArea(p))-- 30 </lang>
both version handle special cases of zero or 1 point.


=={{header|Perl 6}}==
=={{header|Perl 6}}==