How to resolve the algorithm Fibonacci word/fractal step by step in the Python programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Fibonacci word/fractal step by step in the Python programming language

Table of Contents

Problem Statement

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.)

Create and display a fractal similar to Fig 1. (Clicking on the above website   (hal.archives-ouvertes.fr)   will leave a cookie.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Fibonacci word/fractal step by step in the Python programming language

This Python code generates and draws a Fibonacci word fractal.

  • The memoize decorator is used to optimize the fibonacci_word function by caching its results, improving its performance.
  • The fibonacci_word function recursively generates the Fibonacci word based on the given n value.
  • The draw_fractal function takes the Fibonacci word and a step size as input and draws the fractal using Turtle graphics.
  • The main function sets up the drawing environment, generates the Fibonacci word, and draws the fractal.
  • The code includes error checking and turtle graphics setup to ensure smooth drawing and allows saving the output as a PostScript image.

Overall, this code demonstrates the use of memoization and Turtle graphics to create and display visually interesting fractals based on the Fibonacci word.

Source code in the python programming language

from functools import wraps
from turtle import *

def memoize(obj):
    cache = obj.cache = {}
    @wraps(obj)
    def memoizer(*args, **kwargs):
        key = str(args) + str(kwargs)
        if key not in cache:
            cache[key] = obj(*args, **kwargs)
        return cache[key]
    return memoizer

@memoize
def fibonacci_word(n):
    assert n > 0
    if n == 1:
        return "1"
    if n == 2:
        return "0"
    return fibonacci_word(n - 1) + fibonacci_word(n - 2)

def draw_fractal(word, step):
    for i, c in enumerate(word, 1):
        forward(step)
        if c == "0":
            if i % 2 == 0:
                left(90)
            else:
                right(90)

def main():
    n = 25 # Fibonacci Word to use.
    step = 1 # Segment length.
    width = 1050 # Width of plot area.
    height = 1050 # Height of plot area.
    w = fibonacci_word(n)

    setup(width=width, height=height)
    speed(0)
    setheading(90)
    left(90)
    penup()
    forward(500)
    right(90)
    backward(500)
    pendown()
    tracer(10000)
    hideturtle()

    draw_fractal(w, step)

    # Save Poscript image.
    getscreen().getcanvas().postscript(file="fibonacci_word_fractal.eps")
    exitonclick()

if __name__ == '__main__':
    main()


  

You may also check:How to resolve the algorithm Combinations with repetitions step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Arithmetic evaluation step by step in the Jsish programming language
You may also check:How to resolve the algorithm Additive primes step by step in the Julia programming language
You may also check:How to resolve the algorithm Filter step by step in the Maxima programming language
You may also check:How to resolve the algorithm Sphenic numbers step by step in the jq programming language