Fibonacci word/fractal: Difference between revisions

m
syntax highlighting fixup automation
m (J: clearer description)
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].
<langsyntaxhighlight AutoHotkeylang="autohotkey">#NoEnv
SetBatchLines, -1
p := 0.3 ; Segment length (pixels)
Line 98:
Gdip_DeleteGraphics(G)
Gdip_Shutdown(pToken)
ExitApp</langsyntaxhighlight>
 
=={{header|C}}==
Writes an EPS file that has the 26th fractal. This is probably cheating.
<langsyntaxhighlight lang="c">#include <stdio.h>
 
int main(void)
Line 118:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|C++}}==
 
<langsyntaxhighlight lang="cpp">
#include <windows.h>
#include <string>
Line 291:
return system( "pause" );
}
</syntaxhighlight>
</lang>
 
=={{header|D}}==
This uses the turtle module from the Dragon Curve Task, and the module from the Grayscale Image task.
{{trans|Python}}
<langsyntaxhighlight lang="d">import std.range, grayscale_image, turtle;
 
void drawFibonacci(Color)(Image!Color img, ref Turtle t,
Line 317:
img.drawFibonacci(t, w, 1);
img.savePGM("fibonacci_word_fractal.pgm");
}</langsyntaxhighlight>
It prints the level 25 word as the Python entry.
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{libheader| Vcl.Graphics}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Fibonacci_word;
 
Line 410:
end.
 
</syntaxhighlight>
</lang>
=={{header|Elixir}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="elixir">defmodule Fibonacci do
def fibonacci_word, do: Stream.unfold({"1","0"}, fn{a,b} -> {a, {b, b<>a}} end)
Line 445:
end
 
Fibonacci.word_fractal(16)</langsyntaxhighlight>
Output is same as Ruby.
 
Line 452:
<p>Points to note:</p>
<ul>
<li>Rather than using the "usual" Fibonacci catamorphismen <langsyntaxhighlight lang="fsharp">Seq.unfold(fun (f1, f2) -> Some(f1, (f2, f2+f1))) ("1", "0")</langsyntaxhighlight> we use the morphism &sigma;: 0 &rarr; 01, 1 &rarr; 0, starting with a single 1, described in the referenced PDF in the task description.</li>
<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 &cap; formed or &sup; 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>
<langsyntaxhighlight lang="fsharp">let sigma s = seq {
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</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight lang="factor">USING: accessors arrays combinators fry images images.loader
kernel literals make match math math.vectors pair-rocket
sequences ;
Line 578:
save-graphic-image ;
MAIN: main</langsyntaxhighlight>
{{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.
<langsyntaxhighlight FreeBASIClang="freebasic">' version 23-06-2015
' compile with: fbc -s console "filename".bas
 
Line 691:
Sleep
ImageDestroy(img_ptr) ' free memory holding the image
Loop</langsyntaxhighlight>
 
=={{header|Fōrmulæ}}==
Line 704:
{{libheader|Go Graphics}}
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 760:
dc.Stroke()
dc.SavePNG("fib_wordfractal.png")
}</langsyntaxhighlight>
 
{{out}}
Line 772:
a single pixel.
 
<langsyntaxhighlight lang="unicon">global width, height
 
procedure main(A)
Line 828:
DrawLine(p.x,p.y, p1.x,p1.y)
return p1
end</langsyntaxhighlight>
 
=={{header|Haskell}}==
 
<langsyntaxhighlight lang="haskell">import Data.List (unfoldr)
import Data.Bool (bool)
import Data.Semigroup (Sum(..), Min(..), Max(..))
Line 862:
, "</svg>"]
 
main = writeFile "test.html" $ toSVG $ fibonacciWord True False !! 21</langsyntaxhighlight>
 
=={{header|J}}==
Line 868:
Plotting the fractal as a parametric equation, this looks reasonably nice:
 
<langsyntaxhighlight Jlang="j">require 'plot'
plot }:+/\ 0,*/\(^~ 0j_1 0j1 $~ #)'0'=_1{::F_Words 20</langsyntaxhighlight>
 
Note that we need the definition of F_Words from the [[Fibonacci_word#J|Fibonacci word]] page:
 
<langsyntaxhighlight Jlang="j">F_Words=: (,<@;@:{~&_1 _2)@]^:(2-~[)&('1';'0')</langsyntaxhighlight>
 
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}}
<langsyntaxhighlight lang="java">import java.awt.*;
import javax.swing.*;
 
Line 950:
});
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
Line 958:
[[File:FiboWFractal1.png|200px|right|thumb|Output FiboWFractal1.png]]
 
<langsyntaxhighlight lang="javascript">
// Plot Fibonacci word/fractal
// FiboWFractal.js - 6/27/16 aev
Line 988:
return(fw)
}
</langsyntaxhighlight>
 
'''Executing:'''
<langsyntaxhighlight lang="html">
<!-- FiboWFractal2.html -->
<html>
Line 1,015:
</body>
</html>
</langsyntaxhighlight>
 
{{Output}}
Line 1,027:
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">using Luxor, Colors
 
function fwfractal!(word::AbstractString, t::Turtle)
Line 1,049:
fwfractal!(word, t)
finish()
preview()</langsyntaxhighlight>
 
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
import java.awt.*
Line 1,122:
}
}
}</langsyntaxhighlight>
 
=={{header|Logo}}==
Line 1,128:
 
{{works with|UCB Logo}}
<langsyntaxhighlight Logolang="logo">; Return the low 1-bits of :n
; For example if n = binary 10110111 = 183
; then return binary 111 = 7
Line 1,156:
 
setheading 0 ; initial line North
fibonacci.word.fractal 377</langsyntaxhighlight>
 
=={{header|Lua}}==
===L&Ouml;VE===
Needs L&Ouml;VE 2D Engine
<langsyntaxhighlight lang="lua">
RIGHT, LEFT, UP, DOWN = 1, 2, 4, 8
function drawFractals( w )
Line 1,210:
love.graphics.draw( canvas )
end
</syntaxhighlight>
</lang>
===ASCII===
Uses the Bitmap class [[Bitmap#Lua|here]], with an ASCII pixel representation, then extending..
<langsyntaxhighlight lang="lua">function Bitmap:fiboword(n)
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()</langsyntaxhighlight>
{{out}}
<pre style="font-size:50%">|
Line 1,323:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
 
<langsyntaxhighlight lang="mathematica">(*note, this usage of Module allows us to memoize FibonacciWord
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]]]]];</langsyntaxhighlight>
 
=={{header|Nim}}==
{{libheader|imageman}}
<langsyntaxhighlight Nimlang="nim">import imageman
 
const
Line 1,386:
 
# Save into a PNG file.
image.savePNG(Output, compression = 9)</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
Line 1,401:
{{Works with|PARI/GP|2.7.4 and above}}
 
<langsyntaxhighlight lang="parigp">
\\ Fibonacci word/fractals
\\ 4/25/16 aev
Line 1,448:
plotfibofract(21,430,2); \\ Fibofrac2.png
}
</langsyntaxhighlight>
 
{{Output}}
Line 1,471:
{{Works with|PARI/GP|2.7.4 and above}}
 
<langsyntaxhighlight lang="parigp">
\\ Fibonacci word/fractals 2nd version
\\ 4/26/16 aev
Line 1,499:
plotfibofract1(21,600,1); \\ Fibofrac4.png
}
</langsyntaxhighlight>
 
{{Output}}
Line 1,513:
=={{header|Perl}}==
Creates file fword.png containing the Fibonacci Fractal.
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use GD;
Line 1,549:
print $out $im->png;
close $out;
</syntaxhighlight>
</lang>
 
===Using Tk===
This draws a segment at a time so you can watch it grow :)
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Fibonacci_word
Line 1,594:
}
}
</syntaxhighlight>
</lang>
 
=={{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)
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
 
=={{header|Processing}}==
Written in Processing ([http://www.processing.org Processing])
 
<langsyntaxhighlight lang="processing">
int n = 18;
String f1 = "1";
Line 1,696:
}
}
</syntaxhighlight>
</lang>
 
=={{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.
<langsyntaxhighlight lang="python">from functools import wraps
from turtle import *
 
Line 1,758:
 
if __name__ == '__main__':
main()</langsyntaxhighlight>
The output image is probably the same.
 
Line 1,767:
[[File:FiboFractR25.png|right|thumb|Output FiboFractR25.png]]
 
<syntaxhighlight lang="r">
<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")
</langsyntaxhighlight>
 
{{Output}}
Line 1,822:
we do not ''generate'' the words here.
 
<langsyntaxhighlight lang="racket">#lang racket
(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)))))</langsyntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>constant @fib-word = '1', '0', { $^b ~ $^a } ... *;
 
sub MAIN($m = 17, $scale = 3) {
Line 1,901:
print "\n";
}
}</langsyntaxhighlight>
{{out}}
<small><small><pre>⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣇⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
Line 1,987:
 
The output of this REXX program is always written to a disk file &nbsp; (named &nbsp; FIBFRACT.OUT).
<langsyntaxhighlight lang="rexx">/*REXX program generates a Fibonacci word, then (normally) displays the fractal curve.*/
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 " " */</langsyntaxhighlight>
{{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.)
Line 2,114:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">def fibonacci_word(n)
words = ["1", "0"]
(n-1).times{ words << words[-1] + words[-2] }
Line 2,139:
 
word = fibonacci_word(16)
print_fractal(word)</langsyntaxhighlight>
 
{{out}}
Line 2,286:
=={{header|Rust}}==
The output of this program is a file in SVG format.
<langsyntaxhighlight lang="rust">// [dependencies]
// svg = "0.8.0"
 
Line 2,384:
fn main() {
FibwordFractal::save("fibword_fractal.svg", 22).unwrap();
}</langsyntaxhighlight>
 
{{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.
<langsyntaxhighlight lang="scala">
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>
</lang>
 
{{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');</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">var(m=17, scale=3) = ARGV.map{.to_i}...
 
(var world = Hash.new){0}{0} = 1
Line 2,583:
}
 
braille_graphics(world)</langsyntaxhighlight>
{{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…) -->
<langsyntaxhighlight lang="tcl">package require Tk
 
# 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]</langsyntaxhighlight>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|DOME}}
<langsyntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color
import "dome" for Window
 
Line 2,702:
}
 
var Game = FibonacciWordFractal.new(450, 620, 23)</langsyntaxhighlight>
 
=={{header|zkl}}==
Line 2,708:
{{trans|D}}
[[File:Fibonacci word fractal.zkl.jpg|250px|thumb|right]]
<langsyntaxhighlight lang="zkl">fcn drawFibonacci(img,x,y,word){ // word is "01001010...", 75025 characters
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"));</langsyntaxhighlight>
10,333

edits