Add currently solved exerises from Chapter 16
This commit is contained in:
parent
0dde0c524d
commit
1a90502946
6 changed files with 269 additions and 0 deletions
55
thinkcspy/Chapter-16-Exercises/kochsnowflake.py
Normal file
55
thinkcspy/Chapter-16-Exercises/kochsnowflake.py
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
|
||||
import turtle
|
||||
|
||||
def createLSystem(numIters, axiom):
|
||||
if numIters == 0:
|
||||
return axiom
|
||||
else:
|
||||
return createLSystem(numIters - 1, processString(axiom))
|
||||
|
||||
|
||||
def processString(oldStr):
|
||||
newstr = ""
|
||||
for ch in oldStr:
|
||||
newstr = newstr + applyRules(ch)
|
||||
|
||||
return newstr
|
||||
|
||||
|
||||
def applyRules(ch):
|
||||
newstr = ""
|
||||
if ch == 'F':
|
||||
newstr = 'F-F++F-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(4, "F--F--F")
|
||||
t = turtle.Turtle()
|
||||
wn = turtle.Screen()
|
||||
|
||||
t.up()
|
||||
t.back(200)
|
||||
t.down()
|
||||
t.speed(9)
|
||||
drawLsystem(t, inst, 60, 5)
|
||||
|
||||
wn.exitonclick()
|
||||
|
||||
|
||||
main()
|
||||
Loading…
Reference in a new issue