Fibonacci word/fractal: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: reduced the scale (font-size) of the output.)
(→‎{{header|REXX}}: added/changed comments and whitespace, used a template for the output section, provided a mechanism for not displaying the graph to the terminal, split compound statements, optimized the inner DO loop for the output.)
Line 1,902: Line 1,902:
About half of the REXX program is dedicated to plotting the appropriate characters.
About half of the REXX program is dedicated to plotting the appropriate characters.


The output of this REXX program is written to the screen as well as a disk file.
If the order of the graph is negative,   the graph isn't displayed to the terminal screen.

<lang rexx>/*REXX program generates a Fibonacci word, then displays the fractal curve. */
The output of this REXX program is always written to a disk file &nbsp; (named &nbsp; FIBFRACT.OUT).
parse arg ord . /*obtain optional arguments from the CL*/
<lang rexx>/*REXX program generates a Fibonacci word, then (normally) displays the fractal curve.*/
if ord=='' then ord=23 /*Not specified? Then use the default*/
s=FibWord(ord) /*obtain the order of Fibonacci word.*/
parse arg order . /*obtain optional arguments from the CL*/
if order=='' | order=="," then order= 23 /*Not specified? Then use the default*/
x=0; maxX=0; dx=0; b=' '; @.=b; xp=0
y=0; maxY=0; dy=1; @.0.0=.; yp=0
tell= order>=0 /*Negative order? Then don't display. */
do n=1 for length(s); x=x+dx; y=y+dy /*advance the plot for the next point. */
s= FibWord( abs(order) ) /*obtain the order of Fibonacci word.*/
x= 0; maxX= 0; dx= 0; b= ' '; @. = b; xp= 0
maxX=max(maxX,x); maxY=max(maxY,y) /*set the maximums for displaying plot.*/
c='│'; if dx\==0 then c="─"; if n==1 then c='┌' /*is this the first plot?*/
y= 0; maxY= 0; dy= 1; @.0.0= .; yp= 0
do n=1 for length(s); x= x + dx; y= y + dy /*advance the plot for the next point. */
@.x.y=c /*assign a plotting character for curve*/
if @(xp-1,yp)\==b then if @(xp,yp-1)\==b then call @ xp,yp,'┐' /*fix─up a corner.*/
maxX= max(maxX, x); maxY= max(maxY, y) /*set the maximums for displaying plot.*/
if @(xp-1,yp)\==b then if @(xp,yp+1)\==b then call @ xp,yp,'' /* " " " */
c= '' /*glyph (character) used for the plot. */
if @(xp+1,yp)\==b then if @(xp,yp+1)\==b then call @ xp,yp,'└' /* " " " */
if dx\==0 then c= "─" /*if x+dx isn't zero, use this char.*/
if @(xp+1,yp)\==b then if @(xp,yp-1)\==b then call @ xp,yp,'┌' /* " " " */
if n==1 then c= '┌' /*is this the first part to be graphed?*/
xp=x; yp=y; z=substr(s,n,1) /*save old x,y; assign plot character.*/
@.x.y= c /*assign a plotting character for curve*/
if @(xp-1, yp)\==b then if @(xp, yp-1)\==b then call @ xp,yp,'┐' /*fix─up a corner*/
if @(xp-1, yp)\==b then if @(xp, yp+1)\==b then call @ xp,yp,'┘' /* " " " */
if @(xp+1, yp)\==b then if @(xp, yp-1)\==b then call @ xp,yp,'┌' /* " " " */
if @(xp+1, yp)\==b then if @(xp, yp+1)\==b then call @ xp,yp,'└' /* " " " */
xp= x; yp= y /*save the old x & y coördinates.*/
z= substr(s, n, 1) /*assign a plot character for the graph*/
if z==1 then iterate /*Is Z equal to unity? Then ignore it.*/
if z==1 then iterate /*Is Z equal to unity? Then ignore it.*/
ox=dx; oy=dy; dx=0; dy=0 /*save DX,DY as the old versions. */
ox= dx; oy= dy /*save DX,DY as the old versions. */
d=-n//2; if d==0 then d=1 /*determine the sign for the chirality.*/
dx= 0; dy= 0 /*define DX,DY " " new " */
if oy\==0 then dx=-sign(oy)*d /*Going north|south? Go east|west */
d= -n//2; if d==0 then d= 1 /*determine the sign for the chirality.*/
if ox\==0 then dy= sign(ox)*d /* " east|west? " south|north */
if oy\==0 then dx= -sign(oy) * d /*Going north│south? Go east|west */
if ox\==0 then dy= sign(ox) * d /* " east│west? " south|north */
end /*n*/
end /*n*/


call @ x, y, '∙' /*set the last point that was plotted. */
call @ x, y, '∙' /*set the last point that was plotted. */


do r=maxY to 0 by -1; _= /*show single row at a time, top first.*/
do r=maxY to 0 by -1; _= /*show single row at a time, top first.*/
do c=0 to maxX; _=_ || @.c.r; end /*c*/; _=strip(_, 'T') /*build a line.*/
do c=0 for maxX+1; _= _ || @.c.r /*add a plot character (glyph) to line.*/
if _=='' then iterate /*if the line is blank, then ignore it.*/
end /*c*/ /* [↑] construct a line char by char. */
say _; call lineout "FIBFRACT.OUT", _ /*display the line; also write to disk.*/
_= strip(_, 'T') /*construct a line of the graph. */
end /*r*/ /* [↑] only display the non-blank rows*/
if _=='' then iterate /*Is the line blank? Then ignore it. */
exit /*stick a fork in it, we're all done. */
if tell then say _ /*Display the line to the terminal ? */
call lineout "FIBFRACT.OUT", _ /*write graph to disk (FIBFRACT.OUT). */
end /*r*/ /* [↑] only display the non-blank rows*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
@: parse arg xx,yy,p; if arg(3)=='' then return @.xx.yy; @.xx.yy=p; return
@: parse arg xx,yy,p; if arg(3)=='' then return @.xx.yy; @.xx.yy= p; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
FibWord: procedure; parse arg x; !.=0; !.1=1 /*obtain the order of Fibonacci word. */
FibWord: procedure; parse arg x; !.= 0; !.1= 1 /*obtain the order of Fibonacci word. */
do k=3 to x; k1=k-1; k2=k-2 /*generate the Kth " " */
do k=3 to x /*generate the Kth " " */
!.k=!.k1 || !.k2 /*construct the next " " */
k1= k-1; k2= k - 2 /*calculate the K-1 & K-2 shortcut.*/
!.k= !.k1 || !.k2 /*construct the next Fibonacci word. */
end /*k*/ /* [↑] generate a " " */
end /*k*/ /* [↑] generate a " " */
return !.x /*return the Xth " " */</lang>
return !.x /*return the Xth " " */</lang>
'''output''' &nbsp; when using the input: &nbsp; <tt> 17 </tt>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 17 </tt>}}
<br><br>(The output is shown <sup>1</sup>/<sub>8</sub> size.)
<br><br>(The output is shown <sup>1</sup>/<sub>8</sub> size.)
<b>
<b>