Shoelace formula for polygonal area: Difference between revisions

J draft
m (→‎{{header|REXX}}: added the REXX language.)
(J draft)
Line 42:
30.00
</pre>
 
=={{header|J}}==
 
Implementation:
 
<lang J>shoelace=:verb define
0.5*|+/((* 1&|.)/ - (* _1&|.)/)|:y
)</lang>
 
Task example:
 
<lang J> shoelace 3 4,5 11,12 8,9 5,:5 6
30</lang>
 
Exposition:
 
We start with our list of coordinate pairs
 
<lang J> 3 4,5 11,12 8,9 5,:5 6
3 4
5 11
12 8
9 5
5 6</lang>
 
But the first thing we do is transpose them so that x coordinates and y coordinates are the two items we are working with:
 
<lang j> |:3 4,5 11,12 8,9 5,:5 6
3 5 12 9 5
4 11 8 5 6</lang j>
 
We want to rotate the y list by one (in each direction) and multiply the x list items by the corresponding y list items. Something like this, for example:
 
<lang j> 3 5 12 9 5* 1|.4 11 8 5 6
33 40 60 54 20</lang>
 
Or, rephrased:
 
<lang j> (* 1&|.)/|:3 4,5 11,12 8,9 5,:5 6
33 40 60 54 20</lang>
 
We'll be subtracting what we get when we rotate in the other direction, which looks like this:
 
<lang j> ((* 1&|.)/ - (* _1&|.)/)|:3 4,5 11,12 8,9 5,:5 6
15 20 _72 _18 _5</lang>
 
Finally, we add up that list, take the absolute value (there are contexts where signed area is interesting - for example, some graphics application - but that was not a part of this task) and divide that by 2.
 
=={{header|Kotlin}}==
6,962

edits