
The Fibonacci word may be represented as a fractal as described here:


(Clicking on the above website   (hal.archives-ouvertes.fr)    will leave a cookie.)



For F_wordm start with F_wordCharn=1
Draw a segment forward
If current F_wordChar is 0
Turn left if n is even
Turn right if n is odd
next n and iterate until end of F_word


Task

Create and display a fractal similar to Fig 1.


(Clicking on the above website   (hal.archives-ouvertes.fr)    will leave a cookie.)

import imageman
 
const
  Width = 1000
  Height = 1000
  LineColor = ColorRGBU [byte 64, 192, 96]
  Output = "fibword.png"
 
 
proc fibword(n: int): string =
  ## Return the nth fibword.
  var a = "1"
  result = "0"
  for _ in 1..n:
    a = result & a
    swap a, result
 
 
proc drawFractal(image: var Image; fw: string) =
  # Draw the fractal.
  var
    x = 0
    y = image.h - 1
    dx = 1
    dy = 0
 
  for i, ch in fw:
    let (nextx, nexty) = (x + dx, y + dy)
    image.drawLine((x, y), (nextx, nexty), LineColor)
    (x, y) = (nextx, nexty)
    if ch == '0':
      if (i and 1) == 0:
        (dx, dy) = (dy, -dx)
      else:
        (dx, dy) = (-dy, dx)
 
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
 
var image = initImage[ColorRGBU](Width, Height)
image.fill(ColorRGBU [byte 0, 0, 0])
image.drawFractal(fibword(23))
 
# Save into a PNG file.
image.savePNG(Output, compression = 9)