Shoelace formula for polygonal area: Difference between revisions

Content added Content deleted
(Added Sidef)
(→‎{{header|REXX}}: added REXX version 2, used a better way to express multiple quoted literal strings in one statement.)
Line 181: Line 181:


=={{header|REXX}}==
=={{header|REXX}}==
===version 1===
<lang rexx>/*REXX program uses a Shoelace formula to calculate the area of an N-sided polygon. */
<lang rexx>/*REXX program uses a Shoelace formula to calculate the area of an N-sided polygon. */
parse arg pts /*obtain optional arguments from the CL*/
parse arg pts /*obtain optional arguments from the CL*/
Line 186: Line 187:
pts=space(pts, 0); @=pts /*elide extra blanks; save pts.*/
pts=space(pts, 0); @=pts /*elide extra blanks; save pts.*/
do n=1 until @='' /*perform destructive parse on @*/
do n=1 until @='' /*perform destructive parse on @*/
parse var @ '(' x.n ',' y.n ")" ',' @ /*obtain X and Y coördinates*/
parse var @ '(' x.n "," y.n ')' "," @ /*obtain X and Y coördinates*/
end /*n*/
end /*n*/
A=0 /*initialize the area to zero.*/
A=0 /*initialize the area to zero.*/
Line 199: Line 200:
polygon area of 5 points: (3,4),(5,11),(12,8),(9,5),(5,6) is ───► 30
polygon area of 5 points: (3,4),(5,11),(12,8),(9,5),(5,6) is ───► 30
</pre>
</pre>

===version 2===
This REXX version uses a different method to define the &nbsp; <big>'''X<sub>0</sub>, &nbsp; Y<sub>0</sub>'''</big>, &nbsp; and &nbsp; <big>'''X<sub>n+1</sub>''', &nbsp; <big>'''Y<sub>n+1</sub>'''</big> &nbsp; data points &nbsp; (and not treat them as exceptions).

When calculating the area for many polygons &nbsp; (or where the number of polygon sides is large), &nbsp; this method would be faster.
<lang rexx>/*REXX program uses a Shoelace formula to calculate the area of an N-sided polygon. */
parse arg pts /*obtain optional arguments from the CL*/
if pts='' then pts= '(3,4),(5,11),(12,8),(9,5),(5,6)' /*Not specified? Use default. */
pts=space(pts, 0); @=pts /*elide extra blanks; save pts.*/
do n=1 until @='' /*perform destructive parse on @*/
parse var @ '(' x.n "," y.n ')' "," @ /*obtain X and Y coördinates*/
end /*n*/
np=n+1 /*a variable to hold N+1 value*/
parse value x.1 y.1 x.n y.n with x.np y.np x.0 y.0 /*define X.0 & X.n+1 points.*/
A=0 /*initialize the area to zero.*/
do j=1 for n; jp=j+1; jm=j-1 /*adjust for J for overflow. */
A=A + x.j * (y.jp - y.jm) /*compute a part of the area. */
end /*j*/
A=abs(A/2) /*obtain half of the │ A │ sum*/
say 'polygon area of ' n " points: " pts ' is ───► ' A /*stick a fork in it, we're done*/</lang>
{{out|output|text=&nbsp; is the same as the 1<sup>st</sup> REXX version.}} <br><br>


=={{header|Scala}}==
=={{header|Scala}}==