Professional Documents
Culture Documents
Fundamentals of Programming I
Advanced Turtle Graphics
Level 1: A Single
Filled Rectangle
Level 4
Level 5
Level 6
Level 7
Level 8
Level 9
Program Structure
from turtle import Turtle
import random
def drawRectangle(t, x1, y1, x2, y2):
red = random.randint(0, 255)
green = random.randint(0, 255)
blue = random.randint(0, 255)
t.pencolor(red, green, blue)
# Code for drawing goes here
# Definition of the recursive mondrian function goes here
t = Turtle()
x = 50
y = 50
mondrian(t, -x, y, x, -y, 3)
else:
# Horizontal split
The C-curve
A C-curve is a fractal pattern
A level 0 C-curve is a vertical line segment
A level 1 C-curve is obtained by bisecting a level
0 C-curve and joining the sections at right angles
A level N C-curve is obtained by joining two
level N - 1 C-curves at right angles
(50,50)
(0,0)
(50,-50)
(50,-50)
(50,50)
(0,0)
(50,-50)
(50,-50)
Generalizing
(50,50)
(50,50)
(0,0)
(50,-50)
(50,-50)
y2) // 2
x1) // 2
ym)
y2)
Recursing
(50,50)
(50,50)
(0,0)
(50,-50)
Base case
(50,-50)
xm = (x1 +
ym = (x2 +
cCurve(x1,
CCurve(xm,
x2 + y1
y1 + y2
y1, xm,
ym, x2,
Recursive step
- y2) // 2
- x1) // 2
ym)
y2)
Program Structure
from turtle import Turtle
ccurve
ccurve
ccurve
ccurve
ccurve
ccurve
ccurve
ccurve
ccurve
ccurve
ccurve
ccurve
ccurve
ccurve
ccurve
ccurve
ccurve
ccurve
ccurve
ccurve
ccurve
Summary
A recursive algorithm passes the buck repeatedly to the
same function
Recursive algorithms are well-suited for solving problems
in domains that exhibit recursive patterns
Recursive strategies can be used to simplify complex
solutions to difficult problems