#JustForFun i created this #fibonacci spiral using #Python's #turtle module

import logging
import turtle
import time

SPEED_INT = 10  # 1-10 where 0 is as fast as possible
DEFAULT_HEADING = 180
DEFAULT_SCALE = 2.5
START_X = -480
START_Y = -230
logging.basicConfig(level=logging.DEBUG)

# Turtle setup
screen = turtle.getscreen()
# screen.screensize(1900, 1000)
t = turtle.getturtle()
turtle.title("Fibonacci spiral")
t.shape("turtle")
t.speed(SPEED_INT)
t.pensize(3)
center_pos = t.pos()

turtle.setheading(DEFAULT_HEADING)
t.penup()
turtle.setx(START_X)
turtle.sety(START_Y)
t.pendown()
start_pos = t.pos()

time.sleep(1)


def fibonacci(n):
    if n < 0:  Exception("Fibonacci function cannot take negative numbers")
    if n == 0: return 0
    if n == 1: return 1
    return fibonacci(n - 1) + fibonacci(n - 2)


def draw_fib_spiral(
        i_scale: float = DEFAULT_SCALE,
        i_color: str = "black",
        i_nr_of_circles: int = 5,
        i_write_fib_nr: bool = True):
    t.pencolor(i_color)
    t.penup()
    t.setpos(start_pos)
    turtle.setheading(DEFAULT_HEADING)
    t.pendown()
    for i in range(0, 4 * i_nr_of_circles):
        fibonacci_number = fibonacci(2 + i)
        radius = fibonacci_number * i_scale
        old_pos = t.pos()
        t.circle(radius, -90)
        new_pos = t.pos()

        old_pen_size = t.pensize()
        t.pensize(1)
        t.setpos(new_pos[0], old_pos[1])
        t.setpos(old_pos)
        t.setpos(old_pos[0], new_pos[1])
        t.setpos(new_pos)
        t.pensize(old_pen_size)
        delta_x = new_pos[0] - old_pos[0]
        delta_y = new_pos[1] - old_pos[1]

        if i_write_fib_nr:
            font_size = int(4 + i ** 1.7)
            font = ("Arial", font_size, "normal")
            t.pencolor((0, 0.7, 0))
            old_pos = t.pos()
            t.penup()
            t.setpos(old_pos[0] - delta_x / 2, old_pos[1] - delta_y / 2 - font_size / 2)
            t.write(fibonacci_number, font=font, align="center")
            t.setpos(old_pos)
            t.pendown()
            t.pencolor(i_color)


"""
COLOR_LIST = [
    (0, 0, 0),
    (1.0, 0, 0), (0, 1.0, 0), (0, 0, 1.0),
    (1.0, 0, 1.0), (1.0, 1.0, 0), (0, 1.0, 1.0),
    (0.5, 0, 0), (0, 0.5, 0), "pink"
]
for i in range(0, 9):
    ratio = 1 - 0.1 * i
    draw_fib_spiral(i_scale=ratio, i_color=COLOR_LIST[i])
"""
draw_fib_spiral(i_color="black", i_write_fib_nr=True)

t.hideturtle()
turtle.done()

There are no comments yet.