Shoelace formula for polygonal area: Difference between revisions

m
→‎{{header|REXX}}: changed whitespace and comments, simplified code, elided use of temporary variables.
m (→‎{{header|REXX}}: changed whitespace and comments, simplified code, elided use of temporary variables.)
Line 1,046:
===endpoints as exceptions===
<lang rexx>/*REXX program uses a Shoelace formula to calculate the area of an N-sided polygon. */
parse arg pts; $polygon = 'polygon area of ' /*obtainget optional argumentsargs 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 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#*/
A=0 0 /*initialize the area to zero.*/
do j=1 for n#; jp=j+1; if jp>n# then jp=1 /*adjust for J for overflow. */
jm=j-1; if jm==0 then jm=n# /* " " " " underflow. */
A=A + x.j * (y.jp - y.jm) /*compute a part of the area. */
end /*j*/
say '$polygon area of# ' n " points: " pts ' is ───► ' abs(A/2) /*stick a fork in it, we're done*/</lang>
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; when using the default input:}}
<pre>
Line 1,068 ⟶ 1,067:
 
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; $polygon = 'polygon area of ' /*obtainget optional argumentsargs from the CL.*/
if pts='' then pts= '"(3,4),(5,11),(12,8),(9,5),(5,6)'" /*Not specified? Use default. */
ptsA=space(pts, 0); @=pts @= space(pts, 0) /*elideinit extra blanksA; elide blanks savefrom pts.*/
do n#=1 until @=='' /*perform destructive parse on @*/
parse var @ '(' x.n# "," y.n# ')' "," @ /*obtain X and Y coördinates*/
end /*n*/
npe=n #+1; parse value y.1 y.# with y.e y.0 /*adefine variable toY.n+1 hold & N+1 Y.0 value points.*/
parse value x.1 y. do j=1 x.n y.nfor #; with x.npjm= y.npj x.0- 1; jp= j + 1 y.0 /*definecompute X.0J-1 & X.nJ+1 pointsindices.*/
A=0 A=A + x.j * (y.jm - y.jp) /*compute a portion /*initializeof the area to zero.*/
doend /*j=1*/ for n; jp=j+1; jm=j-1 /*adjust for[↓] use one half of J | forA overflow.| */
say '$polygon area of# ' n " points: " pts ' is ───► ' abs(A/2) /*stick a fork in it, we're done*/</lang>
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>