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:
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()