Question

In: Computer Science

To the Dragon Curve, use the 90 degree turns and then the L-System rules: ------------------------------------------------------- FX...

To the Dragon Curve, use the 90 degree turns and then the L-System rules:
-------------------------------------------------------
FX
X -> X+YF+
Y -> -FX-Y
------------------------------------------------------
the rules code have
to be in the go in the applyRules function so complete!!!

Here the starter code
import turtle

def createLSystem(numIters, axiom):
startString = axiom
endString = ""
for i in range(numIters):
endString = processString(startString)
startString = endString

return endString

def processString(oldStr):
newstr = ""
for ch in oldStr:
newstr = newstr + applyRules(ch)

return newstr

def applyRules(ch):
newstr = ""
  
#Your code here

return newstr

def drawLsystem(aTurtle, instructions, angle, distance):
for cmd in instructions:
if cmd == 'F':
aTurtle.forward(distance)
elif cmd == 'B':
aTurtle.backward(distance)
elif cmd == '+':
aTurtle.right(angle)
elif cmd == '-':
aTurtle.left(angle)

def main():
inst = createLSystem(10, "FX") # create the string
print(inst)
t = turtle.Turtle() # create the turtle
wn = turtle.Screen()

t.up()
t.back(200)
t.down()
t.speed(100)
drawLsystem(t, inst, 90, 5) # draw the picture
# angle 90, segment length 5
wn.exitonclick()

main()

Solutions

Expert Solution

Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks

Code:

import turtle

def createLSystem(numIters,axiom):
startString = axiom
endString = ""
for i in range(numIters):
endString = processString(startString)
startString = endString

return endString

def processString(oldStr):
newstr = ""
for ch in oldStr:
newstr = newstr + applyRules(ch)

return newstr

def applyRules(ch):
newstr = ""
if ch == 'X':
newstr = 'X+YF+' # Rule 1
elif ch=='Y':
newstr = '-FX-F' # Rule 1
else:
newstr = ch # no rules apply so keep the character

return newstr

def drawLsystem(aTurtle, instructions, angle, distance):
for cmd in instructions:
if cmd == 'F':
aTurtle.forward(distance)
elif cmd == 'B':
aTurtle.backward(distance)
elif cmd == '+':
aTurtle.right(angle)
elif cmd == '-':
aTurtle.left(angle)

def main():
inst = createLSystem(10, "FX") # create the string
print(inst)
t = turtle.Turtle() # create the turtle
wn = turtle.Screen()

t.up()
t.back(200)
t.down()
t.speed(100)
drawLsystem(t, inst, 90, 5) # draw the picture
# angle 90, segment length 5
wn.exitonclick()

main()

Output:

Code Screenshot:


Related Solutions

Although 90% of all desktop computers use Windows as their operating system, there are two popular...
Although 90% of all desktop computers use Windows as their operating system, there are two popular alternatives - Apple's Mac OS and the Linux open-source operating system. In this assignment, you will investigate an operating system other than the one you usually use to see how it handles common operating system functions. Note: A popular Linux OS is Ubuntu (As an open-source operating system it is FREE). If you choose to test Linux you can use this link for easy...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT