Our plan for today
To get started, download thelab6_programs
folder, rename it to include your name,
and save all your python code into this folder. Create a new file
called lab6.py
. You will be writing many functions in
this lab. Be sure to use lots of comments. You can also create new
files, too, if you find that easier to manage.
- recursive triangles
- recursive box patterns (turtle graphics)
- recursive box patterns (cs1graphics)
- more recursive box patterns (cs1graphics)
asteriskLine(len)
that creates
a line of asterisks of length len
.
def asteriskLine(len): return '*' * lenHere are some examples of how
asteriskLine()
works:
Task 1a. Recursive triangles In this task, you will write a python function called
asteriskTriangle()
that will take
a length
parameter and draw a triangle
pattern composed of lines of asterisks that get
successively smaller with each line.
The asteriskTriangle()
function must be
recursive.
You can use the given
asteriskLine()
function
to create the lines in the triangular pattern.
Task 1b. Recursive lines with any symbol Write a new function called
lineSymbol()
(by modifying
the asteriskLine()
function) so that it takes two parameters. The first parameter is the length, and the second is the symbol used to create the line. Here are some examples:
Task 1c. Recursive triangles with any symbol Write a new function called
triangleSymbol()
(by
modifying your asteriskTriangle()
function) to create a triangular pattern of the user-supplied symbol. Here are some examples:Click here for some notes that you might find helpful with respect to thinking about recursive problems Hint:
print
statements can be really helpful both for understanding how your function works and also for debugging your code.
Task 2. Recursive boxes with turtle graphics
In this task, you will use this square()
function. Note that
square()
uses a for loop and is not recursive.
In most of the screen shots that are shown below, the turtle's pen
was set to red with this line of code: pencolor('red')
.
def square(length):
"""draws a square of size length maintains heading
and position invariant"""
for i in range(1,5): # or for i in range(4)
fd(length)
lt(90)
Task 2a: row(number, size) Write a recursive method called
row(number,size)
that draws a horizontal
row of number
boxes of size size
. This
function must be recursive and maintain a position and heading invariant.
row(3,50) |
row(5,25) |
spacedRow(number,size)
that draws a horizontal
row of number
boxes of size size
with a constant space of size/4
between each square. This
function must be recursive and maintain a position and heading invariant.
spacedRow(3,50) |
spacedRow(5,25) |
decreasingRow(number,size)
that draws a horizontal
row of number
boxes. The first box has size size
and each subsequent box
is half the size of the previous one. This
function must be recursive and maintain a position and heading invariant.
decreasingRow(3,100) |
decreasingRow(5,100) |
nestedSquares(number,size)
that
draws number
nested squares. The largest square has
size size
and each subsequent box is half the size of the
previous one. The boxes are anchored in the lower left corner.
This
function must be recursive and maintain a position and heading invariant.
nestedSquares(3,90) |
nestedSquares(5,130) |
diagonal(number,size)
that
draws number
decreasing diagonal squares. The largest square has
size size
and each subsequent square is half the size of the
previous one. This
function must be recursive and maintain a position and heading invariant.
diagonal(1,100) |
diagonal(2,100) |
diagonal(3,100) |
superDiagonal(number,size)
that
draws number
decreasing diagonal squares. The largest square has
size size
and each subsequent square is half the size of the
previous one. In superDiagonal
, squares are placed on the diagonal
at both the upper right corner and the lower left corner. This
function must be recursive and maintain a position and heading invariant.
superDiagonal(1,125) |
superDiagonal(2,125) |
superDiagonal(3,125) |
superDiagonal(4,125) |
box(x,y,size,color1,canvas)
that draws a single box of side
length size
, centered at (x,y)
, filled with
the color color1
, drawn on the given canvas.
def box(x,y,size,color1,canvas): b = Rectangle(size,size,Point(x,y)) b.setFillColor(color1) canvas.add(b)Here are some sample invocations of the
box
function. Before
invoking box
, this code is executed to set up the canvas:
labcanvas = Canvas(500,500,'ivory','labcanvas')
Your task is to write a recursive function called
makeNestedBoxes(x,y,size,color1,color2,canvas)
that
produces nested boxes of alternating colors. The largest box is drawn first,
filled with color1
, and then the next box has a side length
that is 10 smaller and is filled with color2
, etc. This
alternating pattern continues until the box has a size that is less than 10.
Your makeNestedBoxes()
function must be recursive.
Invocations to create the boxes above are as follows:
# first, create and name the canvas paper = Canvas(500,500,'darkolivegreen') # makeNestedBoxes(x,y,size,color1,color2,paper) makeNestedBoxes(200,400,100,'red','ivory',paper) makeNestedBoxes(150,100,200,'purple','gold',paper) makeNestedBoxes(400,200,180,'navy','lightskyblue',paper)Task 4. Recursive pictures using cs1graphics: box pattern Building upon the
makeNestedBoxes()
function above, this task adds
smaller versions of nested boxes at each of the four corners of every box.
The ratio of a box size to the corner box size is 1:3. The corner boxes
can only be drawn when they have a size greater than or equal to 10. Your nestedBoxPattern()
function must be recursive.
Hint: start
with only one corner box (e.g. upper left), and when that works, go back and add in the
other 3 corner boxes..
The patterns above were generated by the following code:
paper2 = Canvas(600,600,'white','paper2') nestedBoxPattern(300,300,400,'navy','ivory',paper2)and
paper3 = Canvas(1500,1500,'black,'paper3') nestedBoxPattern(750,750,500,'deeppink','slategray',paper3)
nestedBoxPattern()
takes 6 parameters: the x
and y
point where the box is centered,
the size
of the largest box, the
outermost color1
, the alternating color2
,
and the canvas
where the drawing should appear.
Note: here is a chart of the colors available to you through
cs1graphics.py
.