Fibonacci word/fractal: Difference between revisions
m
→{{header|Fōrmulæ}}
No edit summary |
|||
(9 intermediate revisions by 5 users not shown) | |||
Line 748:
=={{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}}==
Line 1,076 ⟶ 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}}==
Line 1,579 ⟶ 1,697:
my $size = 3000;
my $im =
my $white = $im->colorAllocate(255,255,255);
my $black = $im->colorAllocate(0,0,0);
Line 1,813 ⟶ 1,931:
main()</syntaxhighlight>
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 2,709 ⟶ 2,861:
{{trans|Go}}
{{libheader|DOME}}
<syntaxhighlight lang="
import "dome" for Window
|