Shoelace formula for polygonal area: Difference between revisions

(Realize in F#)
Line 150:
<lang fsharp>
// Shoelace formula for area of polygon. Nigel Galloway: April 11th., 2018
let fN(n::g) = let n,g = abs(List.pairwise(n::g@[n])|>List.fold(fun (n,g) ((nα,gα),(nβ,gβ))->((n+(nα*gβ),-(g+gα*nβ))) (0,.0)/2.0
printfn "%f" (fN [(3.0,4.0); (5.0,11.0); (12.0,8.0); (9.0,5.0); (5.0,6.0)])</lang>
(float(abs(n-g)))/2.0
printfn "%f" (fN [(3,4); (5,11); (12,8); (9,5); (5,6)])
</lang>
{{out}}
<pre>
30.000000
</pre>
 
=={{header|Fortran}}==
Except for the use of "END FUNCTION ''name'' instead of just END, and the convenient function SUM with array span expressions (so SUM(P) rather than a DO-loop to sum the elements of array P), both standardised with F90, this would be acceptable to F66, which introduced complex number arithmetic. Otherwise, separate X and Y arrays would be needed, but complex numbers seemed convenient seeing as (x,y) pairs are involved. But because the MODULE facility of F90 has not been used, routines invoking functions must declare the type of the function names, especially if the default types are unsuitable, as here. In function AREA, the x and y parts are dealt with together, but in AREASL they might be better as separate arrays, thus avoiding the DIMAG and DBLE functions to extract the x and y parts. Incidentally, the x and y parts can be interchanged and the calculation still works. Comparing the two resulting areas might give some indication of their accuracy.
2,172

edits