Cut a rectangle: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added.changed whitespace and comments, optimized the SOLVE functions, addhighlighting to the REXX section header.)
(→‎{{header|REXX}}: optimized the SOLVE functions, it is about 5% faster now.)
Line 2,720: Line 2,720:
s: if arg(1)=1 then return arg(3); return word( arg(2) 's', 1) /*pluralizer.*/
s: if arg(1)=1 then return arg(3); return word( arg(2) 's', 1) /*pluralizer.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
solve: procedure expose # dir. @. h len next. w; @.= 0 /*zero rectangle coördinates.*/
solve: procedure expose # @. dir. h len next. w; @.= 0 /*zero rectangle coördinates.*/
parse arg h,w,recur /*get values for some args. */
parse arg h,w,recur /*get values for some args. */
if h//2 then do; t= w; w= h; h= t; if h//2 then return 0
if h//2 then do; t= w; w= h; h= t; if h//2 then return 0
Line 2,731: Line 2,731:
next.0= '-1'; next.1= -wp; next.2= 1; next.3= wp /*direction & distance*/
next.0= '-1'; next.1= -wp; next.2= 1; next.3= wp /*direction & distance*/
if recur then #= 0
if recur then #= 0
do x=cx+1 to w-1; t= x + cy*wp; @.t= 1
cywp= cy * wp /*shortcut calculation*/
do x=cx+1 to w-1; t= cywp + x; @.t= 1
_= len - t; @._= 1; call walk cy - 1, x
_= len - t; @._= 1; call walk cy - 1, x
end /*x*/
end /*x*/
Line 2,739: Line 2,740:
return #
return #
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
walk: procedure expose # dir. @. h len next. w wp; parse arg y,x
walk: procedure expose # @. dir. h len next. w wp; parse arg y,x
if y==h | x==0 | x==w | y==0 then do; #= # + 2; return; end
if y==h | x==0 | x==w | y==0 then do; #= # + 2; return; end
t= x + y*wp; @.t= @.t + 1; _= len - t
t= y*wp + x; @.t= @.t + 1; _= len - t
@._= @._ + 1
@._= @._ + 1
do j=0 for 4; _= t + next.j /*try each of 4 directions.*/
do j=0 for 4; _= t + next.j /*try each of 4 directions.*/
Line 2,833: Line 2,834:
next.0= '-1'; next.1= -wp; next.2= 1; next.3= wp /*direction & distance*/
next.0= '-1'; next.1= -wp; next.2= 1; next.3= wp /*direction & distance*/
if recur then #= 0
if recur then #= 0
do x=cx+1 to w-1; t= x + cy*wp; @.t= 1
cywp= cy * wp /*shortcut calculation*/
do x=cx+1 to w-1; t= cywp + x; @.t= 1
_= len - t; @._= 1; call walk cy - 1, x
_= len - t; @._= 1; call walk cy - 1, x
end /*x*/
end /*x*/
Line 2,846: Line 2,848:
if x==w then do; #= #+2; return; end /* ◄──┤ " " " */
if x==w then do; #= #+2; return; end /* ◄──┤ " " " */
if y==0 then do; #= #+2; return; end /* ◄──┤ " " " */
if y==0 then do; #= #+2; return; end /* ◄──┤ " " " */
t= x + y*wp; @.t= @.t + 1; _= len - t /* │ordered by most likely ►──┐*/
t= y*wp + x; @.t= @.t + 1; _= len - t /* │ordered by most likely ►──┐*/
@._= @._ + 1 /* └──────────────────────────┘*/
@._= @._ + 1 /* └──────────────────────────┘*/
do j=0 for 4; _= t + next.j /*try each of the four directions.*/
do j=0 for 4; _= t + next.j /*try each of the four directions.*/