2

I created a program to draw a dragon curve using turtle graphics.. but my result doesn't really look like the picture does in the link:

enter image description here

One problem I noticed is that I want to save the produced string into the variable newWord.. but I can't use newWord as a parameter in my function drawit, which actually draws lines based on the string. When I try to do that I get the error "global variable newWord not defined." So in my code I just copied the output of newWord to be drawn, without actually passing the variable that I wanted to pass.

I'm not sure if the problem is with my createWord function or if I'm just not 'drawing enough' in drawit.

import turtle

def createWord(max_it, axiom, proc_rules):

    word = axiom
    t = 1

    while (t < max_it):
        word = rewrite(word, proc_rules)
        t=t+1

    newWord = word

def rewrite(word, proc_rules):

    wordList = list(word)

    for i in range(len(wordList)):
        curChar = wordList[i]
        if curChar in proc_rules:
            wordList[i] = proc_rules[curChar]

    return "".join(wordList)

def drawit(newWord, d, angle):

    newWordLs = list(newWord)
    for i in range(len(newWordLs)):
        cur_Char = newWordLs[i]
        if cur_Char == 'F':
            turtle.forward(d)
        elif cur_Char == '+':
            turtle.right(angle)
        elif cur_Char == '-':
            turtle.left(angle)
        else:
            i = i+1

#sample test of dragon curve

def main():
    createWord(10, 'FX', {'X':'X+YF','Y':'FX-Y'})
    drawit('FX+YF+FX-YF+FX+YF-FX-YF+FX+YF+FX-YF-FX+YF-FX-YF', 20, 90)

if __name__=='__main__': main()
mdegges
  • 963
  • 3
  • 18
  • 39

1 Answers1

4

newWord is locally scoped inside of createWord(), so after createWord() is finished, newWord disappears.

Consider creating newWord in the global scope so you can modify it with createWord - or better yet, let createWord() return a value, and set newWord to that value.

I would think that printing "word" and then using it as a parameter in drawit would result in the same thing as using a variable.

It does, but if you want to change the length of your dragon curve, you'll have to copy/paste the string every time instead of simply changing the value of max_it.

Edit: My solution with some sexy recursion (=

import turtle

def dragon_build(turtle_string, n):
    """ Recursively builds a draw string. """
    """ defining f, +, -, as additional rules that don't do anything """
    rules = {'x':'x+yf', 'y':'fx-y','f':'f', '-':'-', '+':'+'}
    turtle_string = ''.join([rules[x] for x in turtle_string])
    if n > 1: return dragon_build(turtle_string, n-1)
    else: return turtle_string

def dragon_draw(size):
    """ Draws a Dragon Curve of length 'size'. """
    turtle_string = dragon_build('fx', size)
    for x in turtle_string:
        if x == 'f': turtle.forward(20)
        elif x == '+': turtle.right(90)
        elif x == '-': turtle.left(90)

def main():
    n = input("Size of Dragon Curve (int): ")
    dragon_draw(n)

if __name__ == '__main__': main()
Cody Hess
  • 1,777
  • 15
  • 19
  • 1
    Thanks, I see what you mean.. but is that the only reason why my picture is so sad looking? I would think that printing "word" and then using it as a parameter in drawit would result in the same thing as using a variable – mdegges Sep 29 '11 at 18:01
  • @Michele `I'm not sure if the problem is with my createWord function or if I'm just not 'drawing enough' in drawit.` That's almost certainly the problem - your dragon curve is just a baby! It looks like [this one](http://upload.wikimedia.org/wikipedia/commons/9/97/Dragon_curve_iterations_%282%29.svg). Try passing a longer string into drawit(). – Cody Hess Sep 30 '11 at 14:42
  • I was able to figure it out! I was passing drawit just one string, when it should be passed word & called in my createWord() function. It just took like 2 lines to fix! Arrrgh – mdegges Sep 30 '11 at 17:49
  • Congratulations! (= Now that you've solved it I'm going to edit in the solution I made up; fun problem! BTW, do you mind voting up or selecting my answer? – Cody Hess Sep 30 '11 at 18:53