Search code examples
pythonpython-3.xturtle-graphicsfractals

Turtle Graphics window not responding


I am attempting to translate a Julia set generator that I made previously to Python code. However, when the code is run, the turtle graphics window stops responding immediately and draws nothing. Have I done something horribly wrong or is there something I'm missing? Perhaps I'm asking too much of python to do in 1 frame. Please explain what is causing this to happen and how I can fix it. Thanks!

import turtle
import time

y_set = []
map_output = 0
iterations = 0
#turtle.hideturtle()
#turtle.speed(1)

generate a list of y-values

def y_set (r):
    global y_set
    y_set = []
    for n in range ((360*2)+1):
        y_set.append(n)

create a color value

def color (i, n):
    output = map(i, 2, 10000, 0, 2500)
    if output < 0:
        output = 0
    if output > 0:
        output = 255

iterate on the x's

def repeat (n, r, i):
    global iterations
    global x
    global y
    aa = 0
    ba = 0
    ab = 0
    a = 0
    b = 0
    for j in range (n):
        iterations += 1
        aa = a * a
        bb = b * b
        ab = 2 * a * b
        a = ((aa - bb) + float(r))
        b = (ab + float(i))
        if (ab + bb) > 4:
            break
    turtle.setx(100 * x)
    turtle.sety(100 * y)
    color(iterations, n)
    turtle.pendown()
    turtle.penup()

Iterate on the y's

def Julia (s, r, i, d):
    global iterations
    global y_set
    global x
    global y
    global a
    global b
    y_set(s)
    while len(y_set) > 0:
        y = y_set[0]/360
        del y_set[0]
        x = -1.5
        for n in range (round((700/(float(r)+1))+1)):
            a = x
            b = y
            iterations = 0
            repeat(10**d, r, i)
            x += ((1/240)*s)

user input

real = input('Real: ')
imag = input('Imaginary: ')

Julia (1, real, imag, 100)
turtle.done()

Solution

  • There are too many problems with this code to focus on an algorithm error. When I try to run it, I get, TypeError: 'int' object is not iterable. Specific issues:

    The i argument here is being passed a number:

        iterations += 1
    ...
    color(iterations, n)
    ...
    
    def color(i, n):
        output = map(i, 2, 10000, 0, 2500)
    

    but Python's map function (and Julia's) expects a function as its first argument:

    map(func, *iterables)
    

    and it returns a list of the results of applying func to iterables but you treat the result as a scalar value:

    output = map(i, 2, 10000, 0, 2500)
    if output < 0:
        output = 0
    if output > 0:
        output = 255
    

    The color() function never uses its second argument, and never returns anything!

    The variables a & b here are being treated as globals, set but not used, as if prepared for use by repeat():

    global a
    global b
    
    ...
    
    a = x
    b = y
    iterations = 0
    repeat(10 ** d, r, i)
    

    but the a & b used by repeat() are locals initialized to zero:

    a = 0
    b = 0
    

    You have a function and global variable with the same name y_set!

    And your globals are out of control.