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 |
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 <big>'''X<sub>0</sub>, Y<sub>0</sub>'''</big>, and <big>'''X<sub>n+1</sub>''', <big>'''Y<sub>n+1</sub>'''</big> data points (and not treat them as exceptions). |
|||
When calculating the area for many polygons (or where the number of polygon sides is large), 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= is the same as the 1<sup>st</sup> REXX version.}} <br><br> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |