Fibonacci word/fractal: Difference between revisions
m
syntax highlighting fixup automation
m (J: clearer description) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 24:
Prints F_Word<sub>30</sub> currently. Segment length and F_Word<sub>n</sub> can be adjusted.
{{libheader|GDIP}}Also see the [http://www.autohotkey.com/board/topic/29449-gdi-standard-library-145-by-tic/ Gdip examples].
<
SetBatchLines, -1
p := 0.3 ; Segment length (pixels)
Line 98:
Gdip_DeleteGraphics(G)
Gdip_Shutdown(pToken)
ExitApp</
=={{header|C}}==
Writes an EPS file that has the 26th fractal. This is probably cheating.
<
int main(void)
Line 118:
return 0;
}</
=={{header|C++}}==
<
#include <windows.h>
#include <string>
Line 291:
return system( "pause" );
}
</syntaxhighlight>
=={{header|D}}==
This uses the turtle module from the Dragon Curve Task, and the module from the Grayscale Image task.
{{trans|Python}}
<
void drawFibonacci(Color)(Image!Color img, ref Turtle t,
Line 317:
img.drawFibonacci(t, w, 1);
img.savePGM("fibonacci_word_fractal.pgm");
}</
It prints the level 25 word as the Python entry.
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{libheader| Vcl.Graphics}}
<syntaxhighlight lang="delphi">
program Fibonacci_word;
Line 410:
end.
</syntaxhighlight>
=={{header|Elixir}}==
{{trans|Ruby}}
<
def fibonacci_word, do: Stream.unfold({"1","0"}, fn{a,b} -> {a, {b, b<>a}} end)
Line 445:
end
Fibonacci.word_fractal(16)</
Output is same as Ruby.
Line 452:
<p>Points to note:</p>
<ul>
<li>Rather than using the "usual" Fibonacci catamorphismen <
<li>The outer dimension of the SVG is computed. For a simplification we compute bounding boxes for fractals with number 3*k+2 only. These are ∩ formed or ⊃ formed. For 3*k and 3*k+1 fractals the bounding box for the next 3*k+2 fractal is taken. (c/f PDF; Theorem 3, Theorem 4)</li>
</ul>
<
for c in s do if c = '1' then yield '0' else yield '0'; yield '1'
}
Line 500:
Sorry, your browser does not support inline SVG.
</svg></body></html>""" (viewboxHeight-1)
0</
{{out}}
<p>Since file upload to the Wiki is not possible, the raw output for F<sub>11</sub> is given:</p>
Line 510:
=={{header|Factor}}==
<
kernel literals make match math math.vectors pair-rocket
sequences ;
Line 578:
save-graphic-image ;
MAIN: main</
{{out}} Similar to fig. 1 from the paper and the image at the top of this page.
=={{header|FreeBASIC}}==
On a Windows 32bit system F_word35 is the biggest that can be drawn.
<
' compile with: fbc -s console "filename".bas
Line 691:
Sleep
ImageDestroy(img_ptr) ' free memory holding the image
Loop</
=={{header|Fōrmulæ}}==
Line 704:
{{libheader|Go Graphics}}
{{trans|Kotlin}}
<
import (
Line 760:
dc.Stroke()
dc.SavePNG("fib_wordfractal.png")
}</
{{out}}
Line 772:
a single pixel.
<
procedure main(A)
Line 828:
DrawLine(p.x,p.y, p1.x,p1.y)
return p1
end</
=={{header|Haskell}}==
<
import Data.Bool (bool)
import Data.Semigroup (Sum(..), Min(..), Max(..))
Line 862:
, "</svg>"]
main = writeFile "test.html" $ toSVG $ fibonacciWord True False !! 21</
=={{header|J}}==
Line 868:
Plotting the fractal as a parametric equation, this looks reasonably nice:
<
plot }:+/\ 0,*/\(^~ 0j_1 0j1 $~ #)'0'=_1{::F_Words 20</
Note that we need the definition of F_Words from the [[Fibonacci_word#J|Fibonacci word]] page:
<
However, image uploads are currently disabled, and rendering images of this sort as wikitext gets bulky.
Line 884:
[[File:fib_word_fractal_java.gif|300px|thumb|right]]
{{works with|Java|8}}
<
import javax.swing.*;
Line 950:
});
}
}</
=={{header|JavaScript}}==
Line 958:
[[File:FiboWFractal1.png|200px|right|thumb|Output FiboWFractal1.png]]
<
// Plot Fibonacci word/fractal
// FiboWFractal.js - 6/27/16 aev
Line 988:
return(fw)
}
</
'''Executing:'''
<
<!-- FiboWFractal2.html -->
<html>
Line 1,015:
</body>
</html>
</
{{Output}}
Line 1,027:
{{works with|Julia|0.6}}
<
function fwfractal!(word::AbstractString, t::Turtle)
Line 1,049:
fwfractal!(word, t)
finish()
preview()</
=={{header|Kotlin}}==
{{trans|Java}}
<
import java.awt.*
Line 1,122:
}
}
}</
=={{header|Logo}}==
Line 1,128:
{{works with|UCB Logo}}
<
; For example if n = binary 10110111 = 183
; then return binary 111 = 7
Line 1,156:
setheading 0 ; initial line North
fibonacci.word.fractal 377</
=={{header|Lua}}==
===LÖVE===
Needs LÖVE 2D Engine
<
RIGHT, LEFT, UP, DOWN = 1, 2, 4, 8
function drawFractals( w )
Line 1,210:
love.graphics.draw( canvas )
end
</syntaxhighlight>
===ASCII===
Uses the Bitmap class [[Bitmap#Lua|here]], with an ASCII pixel representation, then extending..
<
local function fw(n) return n==1 and "1" or n==2 and "0" or fw(n-1)..fw(n-2) end
local word, x, y, dx, dy = fw(n), 0, self.height-1, 0, -1
Line 1,236:
bitmap:clear(" ")
bitmap:fiboword(14)
bitmap:render()</
{{out}}
<pre style="font-size:50%">|
Line 1,323:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
without exposing it to the global scope*)
Module[{FibonacciWord, step},
Line 1,338:
steps = MapIndexed[step, Characters[FibonacciWord[n]]];
dirs = ComposeList[steps, {0, 1}];
Graphics[Line[FoldList[Plus, {0, 0}, dirs]]]]];</
=={{header|Nim}}==
{{libheader|imageman}}
<
const
Line 1,386:
# Save into a PNG file.
image.savePNG(Output, compression = 9)</
=={{header|PARI/GP}}==
Line 1,401:
{{Works with|PARI/GP|2.7.4 and above}}
<
\\ Fibonacci word/fractals
\\ 4/25/16 aev
Line 1,448:
plotfibofract(21,430,2); \\ Fibofrac2.png
}
</
{{Output}}
Line 1,471:
{{Works with|PARI/GP|2.7.4 and above}}
<
\\ Fibonacci word/fractals 2nd version
\\ 4/26/16 aev
Line 1,499:
plotfibofract1(21,600,1); \\ Fibofrac4.png
}
</
{{Output}}
Line 1,513:
=={{header|Perl}}==
Creates file fword.png containing the Fibonacci Fractal.
<
use warnings;
use GD;
Line 1,549:
print $out $im->png;
close $out;
</syntaxhighlight>
===Using Tk===
This draws a segment at a time so you can watch it grow :)
<
use strict; # https://rosettacode.org/wiki/Fibonacci_word
Line 1,594:
}
}
</syntaxhighlight>
=={{header|Phix}}==
Line 1,601:
You can run this online [http://phix.x10.mx/p2js/fibonaccifractal.htm here].
Output matches Fig 1 (at the top of the page)
<!--<
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\FibonacciFractal.exw
Line 1,653:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</
=={{header|Processing}}==
Written in Processing ([http://www.processing.org Processing])
<
int n = 18;
String f1 = "1";
Line 1,696:
}
}
</syntaxhighlight>
=={{header|Python}}==
{{trans|Unicon}}
Note that for Python 3, [http://docs.python.org/py3k/library/functools.html#functools.lru_cache functools.lru_cache] could be used instead of the memoize decorator below.
<
from turtle import *
Line 1,758:
if __name__ == '__main__':
main()</
The output image is probably the same.
Line 1,767:
[[File:FiboFractR25.png|right|thumb|Output FiboFractR25.png]]
<syntaxhighlight lang="r">
## Fibonacci word/fractal 2/20/17 aev
## Create Fibonacci word order n
Line 1,800:
pfibofractal(23, 1000, 1000, 1, "navy")
pfibofractal(25, 2300, 1000, 1, "red")
</
{{Output}}
Line 1,822:
we do not ''generate'' the words here.
<
(require "Fibonacci-word.rkt")
(require graphics/value-turtles)
Line 1,842:
((_ #\1) (draw 1 T))
(((? even?) #\0) (turn -90 (draw 1 T)))
((_ #\0) (turn 90 (draw 1 T)))))</
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
sub MAIN($m = 17, $scale = 3) {
Line 1,901:
print "\n";
}
}</
{{out}}
<small><small><pre>⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣇⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
Line 1,987:
The output of this REXX program is always written to a disk file (named FIBFRACT.OUT).
<
parse arg order . /*obtain optional arguments from the CL*/
if order=='' | order=="," then order= 23 /*Not specified? Then use the default*/
Line 2,033:
!.k= !.k1 || !.k2 /*construct the next Fibonacci word. */
end /*k*/ /* [↑] generate a " " */
return !.x /*return the Xth " " */</
{{out|output|text= when using the input of: <tt> 17 </tt>}}
<br><br>(The output is shown <sup>1</sup>/<sub>8</sub> size.)
Line 2,114:
=={{header|Ruby}}==
<
words = ["1", "0"]
(n-1).times{ words << words[-1] + words[-2] }
Line 2,139:
word = fibonacci_word(16)
print_fractal(word)</
{{out}}
Line 2,286:
=={{header|Rust}}==
The output of this program is a file in SVG format.
<
// svg = "0.8.0"
Line 2,384:
fn main() {
FibwordFractal::save("fibword_fractal.svg", 22).unwrap();
}</
{{out}}
Line 2,391:
=={{header|Scala}}==
'''Note:''' will be computing an SVG image - not very efficient, but very cool. worked for me in the scala REPL with ''-J-Xmx2g'' argument.
<
def fibIt = Iterator.iterate(("1","0")){case (f1,f2) => (f2,f1+f2)}.map(_._1)
Line 2,439:
drawSVG(0,25,550,530,fibIt.drop(18).next,3,"000")
</syntaxhighlight>
{{out}}
Line 2,448:
This script uses Scilab's [[Fibonacci_word#Iterative_method|iterative solution]] to generate Fibonacci words, and the interpreting the words to generate the fractal is similar to [[Langton's_ant#Scilab|Langton's ant]]. The result is displayed in a graphic window.
<syntaxhighlight lang="text">final_length = 37;
word_n = '';
Line 2,518:
scf(0); clf();
plot2d(fractal(:,1),fractal(:,2));
set(gca(),'isoview','on');</
=={{header|Sidef}}==
{{trans|Raku}}
<
(var world = Hash.new){0}{0} = 1
Line 2,583:
}
braille_graphics(world)</
{{out}}
<pre>
Line 2,608:
{{libheader|Tk}}
<!-- I've tested this up to F_37; it required a *lot* of memory (good thing I'm using a 64-bit build…) -->
<
# OK, this stripped down version doesn't work for n<2…
Line 2,651:
pack [canvas .c -width 500 -height 500]
drawFW .c [fibword 23]</
=={{header|Wren}}==
{{trans|Go}}
{{libheader|DOME}}
<
import "dome" for Window
Line 2,702:
}
var Game = FibonacciWordFractal.new(450, 620, 23)</
=={{header|zkl}}==
Line 2,708:
{{trans|D}}
[[File:Fibonacci word fractal.zkl.jpg|250px|thumb|right]]
<
dx:=0; dy:=1; // turtle direction
foreach i,c in ([1..].zip(word)){ // Walker.zip(list)-->Walker of zipped list
Line 2,724:
fibWord:=L("1","0"); do(23){ fibWord.append(fibWord[-1] + fibWord[-2]); }
drawFibonacci(img,20,20,fibWord[-1]);
img.write(File("foo.ppm","wb"));</
|