Shoelace formula for polygonal area: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
No edit summary
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 237:
The polygon area is 30.000000 square units.
</pre>
 
=={{header|C sharp|C#}}==
{{trans|Java}}
<lang csharp>using System;
using System.Collections.Generic;
 
namespace ShoelaceFormula {
using Point = Tuple<double, double>;
 
class Program {
static double ShoelaceArea(List<Point> v) {
int n = v.Count;
double a = 0.0;
for (int i = 0; i < n - 1; i++) {
a += v[i].Item1 * v[i + 1].Item2 - v[i + 1].Item1 * v[i].Item2;
}
return Math.Abs(a + v[n - 1].Item1 * v[0].Item2 - v[0].Item1 * v[n - 1].Item2) / 2.0;
}
 
static void Main(string[] args) {
List<Point> v = new List<Point>() {
new Point(3,4),
new Point(5,11),
new Point(12,8),
new Point(9,5),
new Point(5,6),
};
double area = ShoelaceArea(v);
Console.WriteLine("Given a polygon with vertices [{0}],", string.Join(", ", v));
Console.WriteLine("its area is {0}.", area);
}
}
}</lang>
{{out}}
<pre>Given a polygon with vertices [(3, 4), (5, 11), (12, 8), (9, 5), (5, 6)],
its area is 30.</pre>
 
=={{header|C++}}==
Line 273 ⟶ 309:
{{out}}
<pre>30</pre>
 
=={{header|C#|C sharp}}==
{{trans|Java}}
<lang csharp>using System;
using System.Collections.Generic;
 
namespace ShoelaceFormula {
using Point = Tuple<double, double>;
 
class Program {
static double ShoelaceArea(List<Point> v) {
int n = v.Count;
double a = 0.0;
for (int i = 0; i < n - 1; i++) {
a += v[i].Item1 * v[i + 1].Item2 - v[i + 1].Item1 * v[i].Item2;
}
return Math.Abs(a + v[n - 1].Item1 * v[0].Item2 - v[0].Item1 * v[n - 1].Item2) / 2.0;
}
 
static void Main(string[] args) {
List<Point> v = new List<Point>() {
new Point(3,4),
new Point(5,11),
new Point(12,8),
new Point(9,5),
new Point(5,6),
};
double area = ShoelaceArea(v);
Console.WriteLine("Given a polygon with vertices [{0}],", string.Join(", ", v));
Console.WriteLine("its area is {0}.", area);
}
}
}</lang>
{{out}}
<pre>Given a polygon with vertices [(3, 4), (5, 11), (12, 8), (9, 5), (5, 6)],
its area is 30.</pre>
 
=={{header|D}}==
Line 377:
30
</pre>
 
=={{header|Fōrmulæ}}==
 
In [http://wiki.formulae.org/Shoelace_formula_for_polygonal_area this] page you can see the solution of this task.
 
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). 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 transportation effects more than visualization and edition.
 
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|Fortran}}==
Line 522 ⟶ 514:
{{out}}
<pre>The area of the polygon = 30</pre>
 
=={{header|Fōrmulæ}}==
 
In [http://wiki.formulae.org/Shoelace_formula_for_polygonal_area this] page you can see the solution of this task.
 
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). 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 transportation effects more than visualization and edition.
 
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|Go}}==
Line 969:
30
30</pre>
 
=={{header|Perl 6}}==
===Index and mod offset===
{{works with|Rakudo|2017.07}}
 
<lang perl6>sub area-by-shoelace(@p) {
(^@p).map({@p[$_;0] * @p[($_+1)%@p;1] - @p[$_;1] * @p[($_+1)%@p;0]}).sum.abs / 2
}
 
say area-by-shoelace( [ (3,4), (5,11), (12,8), (9,5), (5,6) ] );</lang>
{{out}}
<pre>30</pre>
 
===Slice and rotation===
{{works with|Rakudo|2017.07}}
<lang perl6>sub area-by-shoelace ( @p ) {
my @x := @p».[0];
my @y := @p».[1];
 
my $s := ( @x Z* @y.rotate( 1) ).sum
- ( @x Z* @y.rotate(-1) ).sum;
 
return $s.abs / 2;
}
 
say area-by-shoelace( [ (3,4), (5,11), (12,8), (9,5), (5,6) ] );
</lang>
{{out}}
<pre>30</pre>
 
=={{header|Phix}}==
Line 1,134 ⟶ 1,105:
(area (P 3 4) (P 5 11) (P 12 8) (P 9 5) (P 5 6)))</lang>
 
{{out}}
<pre>30</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
===Index and mod offset===
{{works with|Rakudo|2017.07}}
 
<lang perl6>sub area-by-shoelace(@p) {
(^@p).map({@p[$_;0] * @p[($_+1)%@p;1] - @p[$_;1] * @p[($_+1)%@p;0]}).sum.abs / 2
}
 
say area-by-shoelace( [ (3,4), (5,11), (12,8), (9,5), (5,6) ] );</lang>
{{out}}
<pre>30</pre>
 
===Slice and rotation===
{{works with|Rakudo|2017.07}}
<lang perl6>sub area-by-shoelace ( @p ) {
my @x := @p».[0];
my @y := @p».[1];
 
my $s := ( @x Z* @y.rotate( 1) ).sum
- ( @x Z* @y.rotate(-1) ).sum;
 
return $s.abs / 2;
}
 
say area-by-shoelace( [ (3,4), (5,11), (12,8), (9,5), (5,6) ] );
</lang>
{{out}}
<pre>30</pre>
Line 1,382 ⟶ 1,383:
30
</pre>
 
 
=={{header|VBA}}==
10,327

edits