Shoelace formula for polygonal area: Difference between revisions
Shoelace formula for polygonal area (view source)
Revision as of 15:37, 5 February 2024
, 3 months ago→{{header|Wren}}: Changed to Wren S/H
No edit summary |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(7 intermediate revisions by 4 users not shown) | |||
Line 608:
</pre>
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
proc shoelace . p[][] res .
sum = 0
for i = 1 to len p[][] - 1
sum += p[i][1] * p[i + 1][2]
sum -= p[i + 1][1] * p[i][2]
.
sum += p[i][1] * p[1][2]
sum -= p[1][1] * p[i][2]
res = abs sum / 2
.
data[][] = [ [ 3 4 ] [ 5 11 ] [ 12 8 ] [ 9 5 ] [ 5 6 ] ]
shoelace data[][] res
print res
</syntaxhighlight>
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">
def shoelace(points) do
points
|> Enum.reduce({0, List.last(points)}, fn {x1, y1}, {sum, {x0, y0}} ->
{sum + (y0 * x1 - x0 * y1), {x1, y1}}
end)
|> elem(0)
|> div(2)
end
</syntaxhighlight>
=={{header|F_Sharp|F#}}==
Line 782 ⟶ 811:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Shoelace_formula_for_polygonal_area}}
'''Solution'''
[[File:Fōrmulæ - Shoelace formula 01.png]]
'''Test case'''
[[File:Fōrmulæ - Shoelace formula 02.png]]
[[File:Fōrmulæ - Shoelace formula 03.png]]
=={{header|Go}}==
Line 1,783 ⟶ 1,818:
<pre>
The area of the polygon = 30
</pre>
=={{header|RPL}}==
{| class="wikitable"
! RPL code
! Comment
|-
|
≪
DUP 1 GET +
0 2 3 PICK SIZE '''FOR''' j
OVER j GET LAST 1 - GET
OVER RE OVER IM * SWAP RE ROT IM * - +
'''NEXT'''
ABS 2 / SWAP DROP
≫ <span style="color:blue">''''SHOEL''''</span> STO
|
<span style="color:blue">'''SHOEL'''</span> ''( { (vertices) } → area ) ''
append 1st vertice at the end
sum = 0 ; loop
get 2 vertices
sum += determinant
end loop
finalize calculation, clean stack
return area
|}
{(3,4) (5,11) (12,8) (9,5) (5,6)} <span style="color:blue">'''SHOEL'''</span>
{{out}}
<pre>
1: 30
</pre>
Line 2,054 ⟶ 2,119:
=={{header|Wren}}==
<syntaxhighlight lang="
var area = 0
for (i in 0...pts.count-1) {
|