Reputation: 89
I have a homework assignment that I just cant grasp. It makes no sense to me, and I dont even know if im going in the right direction at this point.
here is my code so far:
import turtle
def draw_rectangle(center_x, center_y, width, height, myTurtle):
myTurtle.penup()
myTurtle.goto(center_x - width/2, center_y - height/2)
myTurtle.pendown()
myTurtle.goto(center_x - width/2, center_y + height/2)
myTurtle.goto(center_x + width/2, center_y + height/2)
myTurtle.goto(center_x + width/2, center_y - height/2)
myTurtle.goto(center_x - width/2, center_y - height/2)
def smallerRec(width, height, level, myTurtle):
rectangle_art( width/2, -height/2, width/2 , height/2, level-2, myTurtle)
rectangle_art(-width/2, height/2, width/2 , height/2, level-2, myTurtle)
rectangle_art(-width/2, -height/2, width/2 , height/2, level-2, myTurtle)
rectangle_art( width/2, height/2, width/2 , height/2, level-2, myTurtle)
def smallerRec3(width, height, level, myTurtle):
bottom_y = 25
top_y = 75
left_x = 50
right_x = 150
rectangle_art( right_x, top_y, width/level , height/level, level-3, myTurtle)
rectangle_art( right_x, bottom_y, width/level , height/level, level-3, myTurtle)
rectangle_art( left_x, top_y, width/level , height/level, level-3, myTurtle)
rectangle_art( left_x, bottom_y, width/level , height/level, level-3, myTurtle)
def rectangle_art(center_x, center_y, width, height, level, myTurtle):
if (level <= 1):
draw_rectangle(center_x, center_y, width, height, myTurtle)
elif(level == 2):
draw_rectangle(center_x, center_y, width, height, myTurtle)
smallerRec(width, height, level, myTurtle)
elif(level == 3):
draw_rectangle(center_x, center_y, width, height, myTurtle)
smallerRec (width, height, level, myTurtle)
smallerRec3(width, height, level, myTurtle)
def main():
myTurtle = turtle.Turtle()
myWindow = turtle.Screen()
rectangle_art(0, 0, 200, 100, 3, myTurtle)
myWindow.exitonclick()
main()
and this is the output of that code: outout of the code above http://puu.sh/5j491.png
and here is what it should look like:
the assignment http://puu.sh/5j4ds.png
Am I headed in the right direction, in terms of recursion?
Upvotes: 3
Views: 358
Reputation: 4795
You're on the right track.
Looking at it I think you should get rid of your 2 smallerRec
methods (although the code in smallerRec
will still be used). You should have only 1 method that calls draw_rectangle()
- your rectangle_art
method.
This method should take the same parameters you are using (center_x, center_y, width, height, level, myTurtle)
.
It should draw a rectangle at x,y
of width,height
. Then if the level
is greater than 0, it should call itself 4 times, to draw a rectangle at each of its corners with the width and height reduced and the level reduced by 1 (like you are doing).
This way if you start by calling rectangle_art
with level=2
it will:
Draw rectangle, level > 0? yes its 2, draw rectangle at each of my corners
At each level 2 corner:
Draw rectangle, level > 0? yes its 1, draw rectangle at each of my corners
At each level 1 corner:
Draw rectangle, level > 0? no its 0, youre done.
Hint:
I'd be tempted to make the x,y refer to the center of the rectangle in your drawRectangle method. Just because then you can calculate the coordinates of the corner rectangles without knowing the width and height of the smaller rectangle you are going to draw.
If x,y
refer to the top left corner. To draw the bottom right corner rectangle you'd call
drawRectangle(x+width-(newWidth/2), y+height-(newHeight/2), newWidth, newHeight)
If you changed your drawRectangle method so that x,y
refer to the center of a rectangle then to draw the bottom right corner rectangle you'd call
drawRectangle(x+width, y+height, newWidth, newHeight)
Either way would work though.
Upvotes: 1