Shoelace formula for polygonal area: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(added Arturo)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(8 intermediate revisions by 5 users not shown)
Line 539:
{{out}}
<pre>30</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
In keeping with the principles of modularity and reusability, the problem has been broken down into subroutines that can process any polygon. In other words, the subroutines don't just solve the area of one polygon; they can find the area of any polygon.
 
<syntaxhighlight lang="Delphi">
{Create a 2D vector type}
 
type T2DVector = record
X, Y: double;
end;
 
{Test polygon}
 
var Polygon: array [0..4] of T2DVector =
((X:3; Y:4), (X:5; Y:11), (X:12; Y:8), (X:9; Y:5), (X:5; Y:6));
 
 
function GetPolygonArea(Polygon: array of T2DVector): double;
{Return the area of the polygon }
{K = [(x1y2 + x2y3 + x3y4 + ... + xny1) - (x2y1 + x3y2 + x4y3 + ... + x1yn)]/2}
var I,Inx: integer;
var P1,P2: T2DVector;
var Sum1,Sum2: double;
begin
Result:=0;
Sum1:=0; Sum2:=0;
for I:=0 to Length(Polygon)-1 do
begin
{Vector back to the beginning}
if I=(Length(Polygon)-1) then Inx:=0
else Inx:=I+1;
P1:=Polygon[I];
P2:=Polygon[Inx];
Sum1:=Sum1 + P1.X * P2.Y;
Sum2:=Sum2 + P2.X * P1.Y;
end;
Result:=abs((Sum1 - Sum2)/2);
end;
 
procedure ShowPolygon(Poly: array of T2DVector; Memo: TMemo);
var I: integer;
var S: string;
begin
S:='';
for I:=0 to High(Poly) do
S:=S+Format('(%2.1F, %2.1F) ',[Poly[I].X, Poly[I].Y]);
Memo.Lines.Add(S);
end;
 
 
procedure ShowPolygonArea(Memo: TMemo);
var Area: double;
begin
ShowPolygon(Polygon,Memo);
Area:=GetPolygonArea(Polygon);
Memo.Lines.Add('Area: '+FloatToStrF(Area,ffFixed,18,2));
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
(3.0, 4.0) (5.0, 11.0) (12.0, 8.0) (9.0, 5.0) (5.0, 6.0)
Area: 30.00
Elapsed Time: 3.356 ms.
</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 713 ⟶ 811:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Shoelace_formula_for_polygonal_area}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Shoelace formula 01.png]]
In '''[https://formulae.org/?example=Shoelace_formula_for_polygonal_area this]''' page you can see the program(s) related to this task and their results.
 
'''Test case'''
 
[[File:Fōrmulæ - Shoelace formula 02.png]]
 
[[File:Fōrmulæ - Shoelace formula 03.png]]
 
=={{header|Go}}==
Line 1,714 ⟶ 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 1,985 ⟶ 2,119:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var shoelace = Fn.new { |pts|
var area = 0
for (i in 0...pts.count-1) {
9,486

edits