Fibonacci word/fractal: Difference between revisions

m
(Replaced "nimPNG" and "bitmap" module by "imageman" which simplifies the code.)
 
(18 intermediate revisions by 12 users not shown)
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|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
_window = 1
begin enum 1
_fractalView
end enum
 
local fn BuildWindow
window _window, @"Rosetta Code Fibonacci Word/Fractal", ( 0, 0, 640, 480 )
imageview _fractalView,,, ( 20, 20, 600, 440 ), NSImageScaleAxesIndependently, NSImageAlignCenter, NSImageFrameGrayBezel, _window
ViewSetWantsLayer( _fractalView, YES )
CALayerRef layer = fn ViewLayer( _fractalView )
CALayerSetBackgroundColor( layer, fn ColorWhite )
ViewRotateByAngle( _fractalView, 90 )
end fn
 
local fn CreateEPSFile as CFDataRef
NSUInteger i
CFStringRef header = @"%%!PS-Adobe-3.0 EPSF\n%%%%BoundingBox: 0 0 400 565\n¬
/a{0 0 moveto 0 .4 translate 0 0 lineto stroke -1 1 scale}def\n/b{a 90 rotate}def"
CFMutableStringRef mutStr = fn MutableStringWithString( header )
_cASCII = 99 : _zASCII = 122
for i = _cASCII to _zASCII
MutableStringAppendString( mutStr, fn StringWithFormat( @"/%c{%c %c}def\n", i, i - 1, i - 2 ) )
next
MutableStringAppendString( mutStr, @"0 setlinewidth z showpage\n%%EOF" )
CFDataRef epsData = fn StringData( mutStr, NSASCIIStringEncoding )
end fn = epsData
 
void local fn EPSDataToImageToView
CFDataRef epsData = fn CreateEPSFile
ImageRef epsImage = fn ImageWithData( epsData )
CGSize size = fn CGSizeMake( 405, 560 )
ImageRef cropImage = fn ImageWithSize( size )
ImageLockFocus( cropImage )
CGRect r = fn CGRectMake( 0, 0, size.width, size.height )
ImageDrawInRectFromRect( epsImage, r, fn CGRectMake( 0, 0, size.width, size.height ), NSCompositeCopy, 1.0 )
ImageUnlockFocus( cropImage )
ImageViewSetImage( _fractalView, cropImage )
end fn
 
fn BuildWindow
fn EPSDataToImageToView
 
HandleEvents
</syntaxhighlight>
{{output}}
[[File:Fibonacci Word Fractal2.png]]
 
 
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Fibonacci_word}}
 
'''Solution'''
 
The following function generate the Fibonacci word of a given order:
 
[[File:Fōrmulæ - Fibonacci word 01.png]]
 
'''Drawing a Fibonacci word fractal'''
 
It can be done using an [[wp:L-system|L-system]]. There are generic functions written in Fōrmulæ to compute an L-system in the page [[L-system#Fōrmulæ | L-system]].
 
The program to draw a Fibonacci word fractal is:
 
[[File:Fōrmulæ - Fibonacci word 11.png]]
 
[[File:Fōrmulæ - Fibonacci word 12.png]]
 
=={{header|Go}}==
{{libheader|Go Graphics}}
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 752 ⟶ 825:
dc.Stroke()
dc.SavePNG("fib_wordfractal.png")
}</langsyntaxhighlight>
 
{{out}}
Line 764 ⟶ 837:
a single pixel.
 
<langsyntaxhighlight lang="unicon">global width, height
 
procedure main(A)
Line 820 ⟶ 893:
DrawLine(p.x,p.y, p1.x,p1.y)
return p1
end</langsyntaxhighlight>
 
=={{header|Haskell}}==
 
<syntaxhighlight lang="haskell">import Data.List (unfoldr)
import Data.Bool (bool)
import Data.Semigroup (Sum(..), Min(..), Max(..))
import System.IO (writeFile)
 
fibonacciWord :: a -> a -> [[a]]
fibonacciWord a b = unfoldr (\(a,b) -> Just (a, (b, a <> b))) ([a], [b])
 
toPath :: [Bool] -> ((Min Int, Max Int, Min Int, Max Int), String)
toPath = foldMap (\p -> (box p, point p)) .
scanl (<>) mempty .
scanl (\dir (turn, s) -> bool dir (turn dir) s) (1, 0) .
zip (cycle [left, right])
where
box (Sum x, Sum y) = (Min x, Max x, Min y, Max y)
point (Sum x, Sum y) = show x ++ "," ++ show y ++ " "
left (x,y) = (-y, x)
right (x,y) = (y, -x)
 
toSVG :: [Bool] -> String
toSVG w =
let ((Min x1, Max x2, Min y1, Max y2), path) = toPath w
in unwords
[ "<svg xmlns='http://www.w3.org/2000/svg'"
, "width='500' height='500'"
, "stroke='black' fill='none' strokeWidth='2'"
, "viewBox='" ++ unwords (show <$> [x1,y1,x2-x1,y2-y1]) ++ "'>"
, "<polyline points='" ++ path ++ "'/>"
, "</svg>"]
 
main = writeFile "test.html" $ toSVG $ fibonacciWord True False !! 21</syntaxhighlight>
 
=={{header|J}}==
Line 826 ⟶ 933:
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 837 ⟶ 944:
Instead, I'll just describe the algorithm:
 
This draws a discrete parametric curve. Right turn is 0j_1, left turn is 0j1 (negative and positive square roots of negative 1), straight ahead is 1. So: build a list of alternating 0j_1 and 0j1 and raise them to the first power for the 0s in the fibonacci word list and raise them to the 0th power for the 1s in that list. Then compute the running product,sum shift aof 0 ontofollowed the front ofby the list ofrunning products andof computethat the running sumlist. (Of course, this would translate to a rather simple loop, also, once you see the pattern.)
 
=={{header|Java}}==
[[File:fib_word_fractal_java.gif|300px|thumb|right]]
{{works with|Java|8}}
<langsyntaxhighlight lang="java">import java.awt.*;
import javax.swing.*;
 
Line 908 ⟶ 1,015:
});
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
Line 916 ⟶ 1,023:
[[File:FiboWFractal1.png|200px|right|thumb|Output FiboWFractal1.png]]
 
<langsyntaxhighlight lang="javascript">
// Plot Fibonacci word/fractal
// FiboWFractal.js - 6/27/16 aev
Line 946 ⟶ 1,053:
return(fw)
}
</langsyntaxhighlight>
 
'''Executing:'''
<langsyntaxhighlight lang="html">
<!-- FiboWFractal2.html -->
<html>
Line 973 ⟶ 1,080:
</body>
</html>
</langsyntaxhighlight>
 
{{Output}}
Line 981 ⟶ 1,088:
Page with FiboWFractal1.png
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq, and with fq.'''
 
The SVG output is suitable for viewing in a browser.
<syntaxhighlight lang=jq>
def max(s): reduce s as $x (-infinite; if $x > . then $x else . end);
def min(s): reduce s as $x ( infinite; if $x < . then $x else . end);
 
# An unbounded stream
def fibonacci_words:
"1",
(["0","1"] | recurse( [add, .[0]]) | .[0]);
 
def turnLeft:
{"R": "U",
"U": "L",
"L": "D",
"D": "R"};
 
def turnRight:
{"R": "D",
"D": "L",
"L": "U",
"U": "R"};
 
# Input: the remaining Fibonacci word
# $n should initially be 1 and represents
# 1 plus the count of the letters already processed.
#
# Emit a stream of single-letter directions ("1" for forward),
# namely, if current char is 0
# - turn left if $n is even
# - turn right if $n is odd
def directions($n):
if length == 0 then empty
else .[0:1] as $c
| (if $c == "0"
then if $n % 2 == 0 then "L" else "R" end
else "1"
end),
(.[1:] | directions($n+1))
end;
 
# $current is the direction in which we are currently pointing.
# output: the direction in which the next step should be taken
def next_step($current; $turn):
if $turn == "1" then $current
elif $turn == "L" then turnLeft[$current]
elif $turn == "R" then turnRight[$current]
else error
end;
 
# input: a Fibonacci word
# output: a stream of directions for turning, or "1" for not turning,
# i.e. a stream of: U, D, L, R, or 1
# Initially, we are facing R
def steps:
foreach directions(1) as $turn ("R";
next_step(.; $turn) );
# output a stream of [x,y] co-ordinates corresponding to the specified
# stream of steps of the given size.
# So we could for example call: points( nth(5; fibonacci_words) | steps; $size)
def points(steps; $size):
foreach steps as $step ([0,0];
. as [$x, $y]
| if $step == "R" then [$x + $size, $y]
elif $step == "D" then [$x, $y + $size]
elif $step == "L" then [$x - $size, $y]
elif $step == "U" then [$x, $y - $size]
else error
end );
 
# svg header boilerplate
# viewBox = '<min-x> <min-y> <width> <height>'
def svg($minX; $minY; $width; $height):
"<?xml version='1.0' standalone='no'?>",
"<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>",
"<svg viewBox='\($minX - 10) \($minY - 10) \($width + 20) \($height + 20)' version='1.1' xmlns='http://www.w3.org/2000/svg'>";
 
# input: array of [x,y] co-ordinates
# output: "<polyline .... />"
def polyline:
{a:1, b:1} as $p
| "<polyline points='\(map(join(","))|join(" "))'",
" style='fill:none; stroke:black; stroke-width:1' transform='translate(\($p.a), \($p.b))' />";
 
# Output the svg for the $n-th Fibonacci word counting from 0
def fibonacci_word_svg($n):
[points( nth($n; fibonacci_words) | steps; 10)]
| min( .[] | .[0]) as $minx
| max( .[] | .[0]) as $maxx
| min( .[] | .[1]) as $miny
| max( .[] | .[1]) as $maxy
| (($maxx-$minx)|length) as $width
| (($maxy-$miny)|length) as $height
| svg( $minx; $miny; $width; $height),
polyline,
"</svg>" ;
fibonacci_word_svg(22) # the Rust entry uses 22
</syntaxhighlight>
{{output}}
Essentially as for [[#Rust|Rust]] - [[Media:Fibword_fractal_rust.svg]]
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">using Luxor, Colors
 
function fwfractal!(word::AbstractString, t::Turtle)
Line 1,007 ⟶ 1,220:
fwfractal!(word, t)
finish()
preview()</langsyntaxhighlight>
 
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
import java.awt.*
Line 1,080 ⟶ 1,293:
}
}
}</langsyntaxhighlight>
 
=={{header|Logo}}==
Line 1,086 ⟶ 1,299:
 
{{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,114 ⟶ 1,327:
 
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,168 ⟶ 1,381:
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,194 ⟶ 1,407:
bitmap:clear(" ")
bitmap:fiboword(14)
bitmap:render()</langsyntaxhighlight>
{{out}}
<pre style="font-size:50%">|
Line 1,281 ⟶ 1,494:
=={{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,296 ⟶ 1,509:
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,344 ⟶ 1,557:
 
# Save into a PNG file.
image.savePNG(Output, compression = 9)</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
Line 1,359 ⟶ 1,572:
{{Works with|PARI/GP|2.7.4 and above}}
 
<langsyntaxhighlight lang="parigp">
\\ Fibonacci word/fractals
\\ 4/25/16 aev
Line 1,406 ⟶ 1,619:
plotfibofract(21,430,2); \\ Fibofrac2.png
}
</langsyntaxhighlight>
 
{{Output}}
Line 1,429 ⟶ 1,642:
{{Works with|PARI/GP|2.7.4 and above}}
 
<langsyntaxhighlight lang="parigp">
\\ Fibonacci word/fractals 2nd version
\\ 4/26/16 aev
Line 1,457 ⟶ 1,670:
plotfibofract1(21,600,1); \\ Fibofrac4.png
}
</langsyntaxhighlight>
 
{{Output}}
Line 1,471 ⟶ 1,684:
=={{header|Perl}}==
Creates file fword.png containing the Fibonacci Fractal.
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use GD;
Line 1,484 ⟶ 1,697:
 
my $size = 3000;
my $im = new GD::Image->new($size,$size);
my $white = $im->colorAllocate(255,255,255);
my $black = $im->colorAllocate(0,0,0);
Line 1,507 ⟶ 1,720:
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,552 ⟶ 1,765:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/fibonaccifractal.htm here].
Output matches Fig 1 (at the top of the page)
<!--<syntaxhighlight lang="phix">(phixonline)-->
{{libheader|Phix/pGUI}}
<span style="color: #000080;font-style:italic;">--
<lang Phix>-- demo\rosetta\FibonacciFractal.exw
-- demo\rosetta\FibonacciFractal.exw
include pGUI.e
--</span>
 
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Ihandle dlg, canvas
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
cdCanvas cddbuffer, cdcanvas
 
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">canvas</span>
procedure drawFibonacci(integer x, y, dx, dy, n)
<span style="color: #004080;">cdCanvas</span> <span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span>
string prev = "1", word = "0"
for i=3 to n do {prev,word} = {word,word&prev} end for
<span style="color: #008080;">procedure</span> <span style="color: #000000;">drawFibonacci</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">dx</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">dy</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
for i=1 to length(word) do
<span style="color: #004080;">string</span> <span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"1"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">word</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"0"</span>
cdCanvasLine(cddbuffer, x, y, x+dx, y+dy)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">prev</span><span style="color: #0000FF;">,</span><span style="color: #000000;">word</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">word</span><span style="color: #0000FF;">,</span><span style="color: #000000;">word</span><span style="color: #0000FF;">&</span><span style="color: #000000;">prev</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
x += dx y += dy
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
if word[i]=='0' then
<span style="color: #7060A8;">cdCanvasLine</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">+</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">+</span><span style="color: #000000;">dy</span><span style="color: #0000FF;">)</span>
{dx,dy} = iff(remainder(i,2)?{dy,-dx}:{-dy,dx})
<span style="color: #000000;">x</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">dx</span> <span style="color: #000000;">y</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">dy</span>
end if
<span style="color: #008080;">if</span> <span style="color: #000000;">word</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]==</span><span style="color: #008000;">'0'</span> <span style="color: #008080;">then</span>
end for
<span style="color: #0000FF;">{</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dy</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)?{</span><span style="color: #000000;">dy</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">}:{-</span><span style="color: #000000;">dy</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">})</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
function redraw_cb(Ihandle /*ih*/, integer /*posx*/, integer /*posy*/)
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
cdCanvasActivate(cddbuffer)
cdCanvasClear(cddbuffer)
<span style="color: #008080;">function</span> <span style="color: #000000;">redraw_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000080;font-style:italic;">/*posx*/</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">/*posy*/</span><span style="color: #0000FF;">)</span>
drawFibonacci(20, 20, 0, 1, 23)
<span style="color: #7060A8;">cdCanvasActivate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
cdCanvasFlush(cddbuffer)
<span style="color: #7060A8;">cdCanvasClear</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
return IUP_DEFAULT
<span style="color: #000000;">drawFibonacci</span><span style="color: #0000FF;">(</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">20</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">23</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #7060A8;">cdCanvasFlush</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
 
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
function map_cb(Ihandle ih)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
cdcanvas = cdCreateCanvas(CD_IUP, ih)
cddbuffer = cdCreateCanvas(CD_DBUFFER, cdcanvas)
<span style="color: #008080;">function</span> <span style="color: #000000;">map_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
cdCanvasSetBackground(cddbuffer, CD_WHITE)
<span style="color: #000000;">cdcanvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_IUP</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
cdCanvasSetForeground(cddbuffer, CD_GREEN)
<span style="color: #000000;">cddbuffer</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_DBUFFER</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span><span style="color: #0000FF;">)</span>
return IUP_DEFAULT
<span style="color: #7060A8;">cdCanvasSetBackground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CD_WHITE</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #7060A8;">cdCanvasSetForeground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CD_GREEN</span><span style="color: #0000FF;">)</span>
 
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
procedure main()
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
IupOpen()
 
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
canvas = IupCanvas(NULL)
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
IupSetAttribute(canvas, "RASTERSIZE", "620x450")
<span style="color: #000000;">canvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupCanvas</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"RASTERSIZE=620x450"</span><span style="color: #0000FF;">)</span>
IupSetCallback(canvas, "MAP_CB", Icallback("map_cb"))
<span style="color: #7060A8;">IupSetCallbacks</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"MAP_CB"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"map_cb"</span><span style="color: #0000FF;">),</span>
 
<span style="color: #008000;">"ACTION"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"redraw_cb"</span><span style="color: #0000FF;">)})</span>
dlg = IupDialog(canvas, "RESIZE=NO")
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">`RESIZE=NO, TITLE="Fibonacci Fractal"`</span><span style="color: #0000FF;">)</span>
IupSetAttribute(dlg, "TITLE", "Fibonacci Fractal")
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
IupSetCallback(canvas, "ACTION", Icallback("redraw_cb"))
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
 
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
IupMap(dlg)
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
IupShowXY(dlg,IUP_CENTER,IUP_CENTER)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
IupMainLoop()
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
IupClose()
end procedure
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
 
<!--</syntaxhighlight>-->
main()</lang>
 
=={{header|Processing}}==
Written in Processing ([http://www.processing.org Processing])
 
<langsyntaxhighlight lang="processing">
int n = 18;
String f1 = "1";
Line 1,651 ⟶ 1,867:
}
}
</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,713 ⟶ 1,929:
 
if __name__ == '__main__':
main()</langsyntaxhighlight>
The output image is probably the same.
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ $ "turtleduck.qky" loadfile ] now!
 
[ [ ' [ 1 ]
' [ 0 ]
rot dup 1 = iff
2drop done
dup 2 = iff
[ drop nip ] done
2 - times
[ dup rot join ] ]
nip witheach
[ 3 2 walk
0 = if
[ i^ 1 &
2 * 1 -
4 turn ] ] ] is fibofractal ( n --> )
 
turtle 0 frames
450 1 fly
1 4 turn
300 1 fly
1 2 turn
23 fibofractal
frame</syntaxhighlight>
 
{{out}}
 
Animation (slowed down to 32 seconds). https://youtu.be/Ap3i2S3gpkc
 
Finished image.
[[File:Quackery Fibonacci word fractal.png|thumb|center]]
 
=={{header|R}}==
Line 1,722 ⟶ 1,972:
[[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,755 ⟶ 2,005:
pfibofractal(23, 1000, 1000, 1, "navy")
pfibofractal(25, 2300, 1000, 1, "red")
</langsyntaxhighlight>
 
{{Output}}
Line 1,777 ⟶ 2,027:
we do not ''generate'' the words here.
 
<langsyntaxhighlight lang="racket">#lang racket
(require "Fibonacci-word.rkt")
(require graphics/value-turtles)
Line 1,797 ⟶ 2,047:
((_ #\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,856 ⟶ 2,106:
print "\n";
}
}</langsyntaxhighlight>
{{out}}
<small><small><pre>⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣇⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
Line 1,942 ⟶ 2,192:
 
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 1,988 ⟶ 2,238:
!.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,069 ⟶ 2,319:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">def fibonacci_word(n)
words = ["1", "0"]
(n-1).times{ words << words[-1] + words[-2] }
Line 2,094 ⟶ 2,344:
 
word = fibonacci_word(16)
print_fractal(word)</langsyntaxhighlight>
 
{{out}}
Line 2,241 ⟶ 2,491:
=={{header|Rust}}==
The output of this program is a file in SVG format.
<langsyntaxhighlight lang="rust">// [dependencies]
// svg = "0.8.0"
 
Line 2,339 ⟶ 2,589:
fn main() {
FibwordFractal::save("fibword_fractal.svg", 22).unwrap();
}</langsyntaxhighlight>
 
{{out}}
[[Media:Fibword_fractal_rust.svg]]
See: [https://slack-files.com/T0CNUL56D-F018887LSM7-74f1564b43 fibword_fractal.svg] (offsite SVG image)
 
=={{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,394 ⟶ 2,644:
 
drawSVG(0,25,550,530,fibIt.drop(18).next,3,"000")
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,403 ⟶ 2,653:
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,473 ⟶ 2,723:
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,538 ⟶ 2,788:
}
 
braille_graphics(world)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,563 ⟶ 2,813:
{{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,606 ⟶ 2,856:
 
pack [canvas .c -width 500 -height 500]
drawFW .c [fibword 23]</langsyntaxhighlight>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|DOME}}
<syntaxhighlight lang="wren">import "graphics" for Canvas, Color
import "dome" for Window
 
class FibonacciWordFractal {
construct new(width, height, n) {
Window.title = "Fibonacci Word Fractal"
Window.resize(width, height)
Canvas.resize(width, height)
_fore = Color.green
_wordFractal = wordFractal(n)
}
 
init() {
drawWordFractal(20, 20, 1, 0)
}
 
wordFractal(i) {
if (i < 2) return (i == 1) ? "1" : ""
var f1 = "1"
var f2 = "0"
for (j in i-2...0) {
var tmp = f2
f2 = f2 + f1
f1 = tmp
}
return f2
}
 
drawWordFractal(x, y, dx, dy) {
for (i in 0..._wordFractal.count) {
Canvas.line(x, y, x + dx, y + dy, _fore)
x = x + dx
y = y + dy
if (_wordFractal[i] == "0") {
var tx = dx
dx = (i % 2 == 0) ? -dy : dy
dy = (i % 2 == 0) ? tx : - tx
}
}
}
 
update() {}
 
draw(alpha) {}
}
 
var Game = FibonacciWordFractal.new(450, 620, 23)</syntaxhighlight>
 
=={{header|zkl}}==
Line 2,612 ⟶ 2,913:
{{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,628 ⟶ 2,929:
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>
2,120

edits